@@ -25,7 +25,7 @@ class ProductProduct(models.Model):
25
25
_inherit = 'product.product'
26
26
27
27
unlock_mobile_id = fields .Char (string = 'unlock_mobile_id' )
28
- unlockbase_tool_id = fields .Many2many ('unlockbase.tool' )
28
+ unlockbase_tool_ids = fields .Many2many ('unlockbase.tool' , 'unlock_tool_product_rel' , 'product_id' , 'unlockbase_tool_id ' )
29
29
30
30
31
31
class ProductCategory (models .Model ):
@@ -37,7 +37,7 @@ class ProductCategory(models.Model):
37
37
class UnlockBase (models .Model ):
38
38
_name = 'unlockbase'
39
39
40
- api_dict = {'tool_id' : 'ID' ,
40
+ unlock_tool_dict = {'tool_id' : 'ID' ,
41
41
'tool_name' : 'Name' ,
42
42
'credits' : 'Credits' ,
43
43
'type' : 'Type' ,
@@ -70,8 +70,8 @@ def action_load_from_unlockbase(self):
70
70
all_good = True
71
71
while all_good :
72
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
74
- # all_good = self.create_mobiles_tools() # for each mobile we create available unlock tools
73
+ # all_good = self.create_tools() # create tools with bound mobiles to it
74
+ all_good = self .create_mobiles_tools () # for each mobile we create available unlock tools
75
75
_logger .info ('Data from unlockbase.com loaded successfully' )
76
76
return
77
77
_logger .info ('Errors occurred while unlockbase.com data loading' )
@@ -121,15 +121,22 @@ def create_brands_and_mobiles(self):
121
121
# Create mobile
122
122
new_mobile = self .env ['product.product' ].create (vals )
123
123
_logger .info ('New mobile created: %s %s' % (brand_name , new_mobile .name ))
124
- # Create category for unlock tools for this phone
125
- vals = {'brand_id' : brand_id , 'name' : mobile_name , 'parent_id' : old_brand .id }
126
- unlock_cat = self .env ['product.category' ].create (vals )
127
- _logger .info ('New unlock mobile tools category created: %s' % unlock_cat .name )
128
124
else :
129
125
old_mobile = self .env ['product.product' ].search ([('mobile_tech_name' , '=' , mobile_name )])
130
126
if not old_mobile .unlock_mobile_id or old_mobile .unlock_mobile_id != unlock_mobile_id :
131
127
old_mobile .unlock_mobile_id = unlock_mobile_id
132
128
_logger .info ('Old mobile updated: %s' % old_mobile .name )
129
+ # Create category for unlock tools for this phone
130
+ old_mobile_cat = self .env ['product.category' ].search ([('name' , '=' , brand_name_orig + ' ' + mobile .find ('Name' ).text )])
131
+ vals = {'brand_id' : brand_id ,
132
+ 'name' : brand_name_orig + ' ' + mobile .find ('Name' ).text ,
133
+ 'parent_id' : old_brand .id }
134
+ if len (old_mobile_cat ) == 1 :
135
+ old_mobile_cat .update (vals )
136
+ _logger .info ('Old unlock mobile tools category updated: %s' % unlock_cat .name )
137
+ elif len (old_mobile_cat ) == 0 :
138
+ unlock_cat = self .env ['product.category' ].create (vals )
139
+ _logger .info ('New unlock mobile tools category created: %s' % unlock_cat .name )
133
140
self .env .cr .commit ()
134
141
return True
135
142
@@ -140,13 +147,15 @@ def create_tools(self):
140
147
for tool in group .findall ('Tool' ):
141
148
tool_mobiles = self .get_tool_mobiles (tool .find ('ID' ).text )
142
149
mobiles_ids = [int (r .find ('ID' ).text ) for r in tool_mobiles .findall ('Mobile' )]
143
- found_mobiles = self .env ['product.product' ].search ([('mobile_id ' , 'in' , mobiles_ids )])
150
+ found_mobiles = self .env ['product.product' ].search ([('unlock_mobile_id ' , 'in' , mobiles_ids )])
144
151
if len (found_mobiles ) < 1 :
145
152
continue
146
- vals = {'group_id' : group .find ('ID' ), 'group_name' : group .find ('Name' ), 'product_ids' : found_mobiles }
147
- for key , val in self .api_dict .iteritems ():
153
+ vals = {'group_id' : group .find ('ID' ).text , # must have
154
+ 'group_name' : group .find ('Name' ).text , # must have
155
+ 'product_ids' : [(6 , 0 , found_mobiles .ids or [])]}
156
+ for key , val in self .unlock_tool_dict .iteritems ():
148
157
try :
149
- vals [key ] = tool .find (val )
158
+ vals [key ] = tool .find (val ). text # some might absent
150
159
except :
151
160
pass
152
161
vals ['name' ] = tool .find ('Name' ).text
@@ -161,17 +170,24 @@ def create_tools(self):
161
170
162
171
@api .model
163
172
def create_mobiles_tools (self ):
164
- tools = self .env ['unlockbase.tool' ].browse ()
165
- for tool in tools :
166
- for mobile_id in tool .product_ids :
167
- mobile_tools_cat = self .env ['product.product' ].search ([('name' , '=' , mobile_id .mobile_tech_name )])
168
- found_tools = self .env ['product.product' ].search ([('unlockbase_tool_id' , '=' , tool .id ), ('categ_id' , '=' , mobile_tools_cat .id )])
173
+ image_path = openerp .modules .get_module_resource ('unlockbase' , 'static/src/img' , 'lock.png' )
174
+ mobiles = self .env ['product.product' ].search ([('unlock_mobile_id' , '!=' , '' )])
175
+ for mobile in mobiles :
176
+ mobile_tools_cat = self .env ['product.category' ].search ([('name' , '=' , mobile .name )])
177
+ for tool in mobile .unlockbase_tool_ids :
178
+ found_tools = self .env ['product.product' ].search ([('unlockbase_tool_ids' , 'in' , tool .id ), ('categ_id' , '=' , mobile_tools_cat .id )])
179
+ vals = {'name' : mobile .name + ' ' + tool .name ,
180
+ 'unlockbase_tool_ids' : [(4 , tool .id ,)],
181
+ 'type' : 'service' ,
182
+ 'image' : open (image_path , 'rb' ).read ().encode ('base64' ),
183
+ 'categ_id' : mobile_tools_cat .id }
169
184
if len (found_tools ) == 0 :
170
- vals = {'name' : tool .name , 'unlockbase_tool_id' : tool .id , 'categ_id' : mobile_tools_cat .id }
171
185
new_tool_product = self .env ['product.product' ].create (vals )
172
- _logger .info ('New tool product created: %s' % new_tool_product .name )
186
+ _logger .info ('New unlockbase tool product created: %s' % new_tool_product .name )
173
187
elif len (found_tools ) == 1 :
188
+ continue # TODO option to update old
174
189
found_tools .update (vals )
190
+ _logger .info ('Old unlockbase tool product updated: %s' % found_tools .name )
175
191
176
192
def send_action (self , values ):
177
193
values ['Key' ] = unlockbase_key
@@ -204,7 +220,7 @@ def get_tool_mobiles(self, tool_id):
204
220
class UnlockBaseTool (models .Model ):
205
221
_name = 'unlockbase.tool'
206
222
207
- product_ids = fields .One2many ('product.product' , 'unlockbase_tool_id' )
223
+ product_ids = fields .Many2many ('product.product' , 'unlock_tool_product_rel' , ' unlockbase_tool_id' , 'product_id ' )
208
224
name = fields .Char ()
209
225
group_id = fields .Char ()
210
226
group_name = fields .Char ()
@@ -235,7 +251,6 @@ class UnlockBaseTool(models.Model):
235
251
requires_locks = fields .Char ()
236
252
237
253
238
-
239
254
def make_tech_name (name ):
240
255
res = name .strip ().lower ()
241
256
res = res .replace (' ' , '' )
0 commit comments