1
1
# -*- coding: utf-8 -*-
2
2
3
3
from openerp import api , fields , models
4
- import threading
5
4
import openerp
6
5
import logging
7
6
import urllib
8
7
import urllib2
9
8
import xml .etree .ElementTree
10
- import base64
11
9
import re
12
10
import string
13
- from bs4 import BeautifulSoup as bs
14
- from openerp import SUPERUSER_ID
15
11
16
12
17
13
_logger = logging .getLogger ("# " + __name__ )
18
14
_logger .setLevel (logging .DEBUG )
19
15
20
- unlockbase_url = 'http://www.unlockbase.com/xml/api/v3' # TODO place in ir config parameter
21
- unlockbase_key = '(C8C7-4533-06AE-3151)' # TODO place in ir config parameter
22
-
23
16
24
17
class ProductProduct (models .Model ):
25
18
_inherit = 'product.product'
@@ -69,8 +62,8 @@ def action_load_from_unlockbase(self):
69
62
_logger .info ('Loading of unlockbase.com mobiles database started' )
70
63
all_good = True
71
64
while all_good :
72
- # all_good = self.create_brands_and_mobiles() # create, brand, mobile and mobile tools category
73
- # all_good = self.create_tools() # create tools with bound mobiles to it
65
+ all_good = self .create_brands_and_mobiles () # create, brand, mobile and mobile tools category
66
+ all_good = self .create_tools () # create tools with bound mobiles to it
74
67
all_good = self .create_mobiles_tools () # for each mobile we create available unlock tools
75
68
_logger .info ('Data from unlockbase.com loaded successfully' )
76
69
return
@@ -91,7 +84,7 @@ def create_brands_and_mobiles(self):
91
84
all_brand_mobiles = [r .mobile_tech_name for r in self .env ['product.product' ].search ([('mobile_brand' , '=' , brand_name )])]
92
85
old_brand = self .env ['product.category' ].search ([('name' , '=' , brand_name )])
93
86
if brand_name not in all_brands_names :
94
- # TEMP
87
+ # TODO TEMP
95
88
continue
96
89
vals = {'brand_id' : brand_id , 'name' : brand_name , 'parent_id' : mobiles_cat .id }
97
90
# Create brand
@@ -107,12 +100,12 @@ def create_brands_and_mobiles(self):
107
100
mobile_name = make_tech_name (mobile .find ('Name' ).text )
108
101
mobile_photo = mobile .find ('Photo' ).text .replace ('https' , 'http' )
109
102
if mobile_name not in all_brand_mobiles :
110
- # TEMP
103
+ # TODO TEMP
111
104
continue
112
105
resp = urllib .urlopen (mobile_photo )
113
106
photo = None
114
- # if resp.code == 200:
115
- # photo = base64.b64encode(resp.read())
107
+ if resp .code == 200 :
108
+ photo = base64 .b64encode (resp .read ())
116
109
vals = {'unlock_mobile_id' : unlock_mobile_id ,
117
110
'name' : brand_name_orig + ' ' + mobile .find ('Name' ).text ,
118
111
'image' : photo ,
@@ -179,6 +172,7 @@ def create_mobiles_tools(self):
179
172
vals = {'name' : mobile .name + ' ' + tool .name ,
180
173
'unlockbase_tool_ids' : [(4 , tool .id ,)],
181
174
'type' : 'service' ,
175
+ 'list_price' : tool .credits ,
182
176
'image' : open (image_path , 'rb' ).read ().encode ('base64' ),
183
177
'categ_id' : mobile_tools_cat .id }
184
178
if len (found_tools ) == 0 :
@@ -189,17 +183,7 @@ def create_mobiles_tools(self):
189
183
found_tools .update (vals )
190
184
_logger .info ('Old unlockbase tool product updated: %s' % found_tools .name )
191
185
192
- def send_action (self , values ):
193
- values ['Key' ] = unlockbase_key
194
- data = urllib .urlencode (values )
195
- req = urllib2 .Request (unlockbase_url , data )
196
- response = urllib2 .urlopen (req )
197
- the_page = response .read ()
198
- if 'Unauthorized IP address' in the_page :
199
- _logger .error ('Unauthorized IP address ERROR. Please check security configuration in unlockbase.com settings.' )
200
- return False
201
- res = xml .etree .ElementTree .fromstring (the_page )
202
- return res
186
+ """ unlockbase.com API v3 representation """
203
187
204
188
def get_all_data (self ):
205
189
values = {'Action' : 'GetMobiles' }
@@ -216,6 +200,19 @@ def get_tool_mobiles(self, tool_id):
216
200
res = self .send_action (values )
217
201
return res
218
202
203
+ def send_action (self , values ):
204
+ unlockbase_url = self .env ['ir.config_parameter' ].sudo ().get_param ('unlockbase.url' )
205
+ values ['Key' ] = self .env ['ir.config_parameter' ].sudo ().get_param ('unlockbase.key' )
206
+ data = urllib .urlencode (values )
207
+ req = urllib2 .Request (unlockbase_url , data )
208
+ response = urllib2 .urlopen (req )
209
+ the_page = response .read ()
210
+ if 'Unauthorized IP address' in the_page :
211
+ _logger .error ('Unauthorized IP address ERROR. Please check security configuration in unlockbase.com settings.' )
212
+ return False
213
+ res = xml .etree .ElementTree .fromstring (the_page )
214
+ return res
215
+
219
216
220
217
class UnlockBaseTool (models .Model ):
221
218
_name = 'unlockbase.tool'
@@ -279,4 +276,4 @@ def dumpclean(obj):
279
276
else :
280
277
print v
281
278
else :
282
- print obj
279
+ print obj
0 commit comments