Skip to content

Commit 3869ffb

Browse files
author
ilyasProgrammer
committed
tools mobiles ok
1 parent 3560ebe commit 3869ffb

File tree

3 files changed

+37
-22
lines changed

3 files changed

+37
-22
lines changed

gsmarena/gsmarena.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
class ProductProduct(models.Model):
2222
_inherit = 'product.product'
2323

24-
mobile_id = fields.Char(string='Mobile id')
24+
mobile_id = fields.Char(string='Mobile id') # gsmarena mobile id
2525
mobile_brand = fields.Char(string='Mobile brand')
2626
mobile_title = fields.Char(string='Mobile title')
2727
mobile_slug = fields.Char(string='Mobile slug')

unlockbase/static/src/img/lock.png

75.5 KB
Loading

unlockbase/unlockbase.py

+36-21
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class ProductProduct(models.Model):
2525
_inherit = 'product.product'
2626

2727
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')
2929

3030

3131
class ProductCategory(models.Model):
@@ -37,7 +37,7 @@ class ProductCategory(models.Model):
3737
class UnlockBase(models.Model):
3838
_name = 'unlockbase'
3939

40-
api_dict = {'tool_id': 'ID',
40+
unlock_tool_dict = {'tool_id': 'ID',
4141
'tool_name': 'Name',
4242
'credits': 'Credits',
4343
'type': 'Type',
@@ -70,8 +70,8 @@ def action_load_from_unlockbase(self):
7070
all_good = True
7171
while all_good:
7272
# 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
7575
_logger.info('Data from unlockbase.com loaded successfully')
7676
return
7777
_logger.info('Errors occurred while unlockbase.com data loading')
@@ -121,15 +121,22 @@ def create_brands_and_mobiles(self):
121121
# Create mobile
122122
new_mobile = self.env['product.product'].create(vals)
123123
_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)
128124
else:
129125
old_mobile = self.env['product.product'].search([('mobile_tech_name', '=', mobile_name)])
130126
if not old_mobile.unlock_mobile_id or old_mobile.unlock_mobile_id != unlock_mobile_id:
131127
old_mobile.unlock_mobile_id = unlock_mobile_id
132128
_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)
133140
self.env.cr.commit()
134141
return True
135142

@@ -140,13 +147,15 @@ def create_tools(self):
140147
for tool in group.findall('Tool'):
141148
tool_mobiles = self.get_tool_mobiles(tool.find('ID').text)
142149
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)])
144151
if len(found_mobiles) < 1:
145152
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():
148157
try:
149-
vals[key] = tool.find(val)
158+
vals[key] = tool.find(val).text # some might absent
150159
except:
151160
pass
152161
vals['name'] = tool.find('Name').text
@@ -161,17 +170,24 @@ def create_tools(self):
161170

162171
@api.model
163172
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}
169184
if len(found_tools) == 0:
170-
vals = {'name': tool.name, 'unlockbase_tool_id': tool.id, 'categ_id': mobile_tools_cat.id}
171185
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)
173187
elif len(found_tools) == 1:
188+
continue # TODO option to update old
174189
found_tools.update(vals)
190+
_logger.info('Old unlockbase tool product updated: %s' % found_tools.name)
175191

176192
def send_action(self, values):
177193
values['Key'] = unlockbase_key
@@ -204,7 +220,7 @@ def get_tool_mobiles(self, tool_id):
204220
class UnlockBaseTool(models.Model):
205221
_name = 'unlockbase.tool'
206222

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')
208224
name = fields.Char()
209225
group_id = fields.Char()
210226
group_name = fields.Char()
@@ -235,7 +251,6 @@ class UnlockBaseTool(models.Model):
235251
requires_locks = fields.Char()
236252

237253

238-
239254
def make_tech_name(name):
240255
res = name.strip().lower()
241256
res = res.replace(' ', '')

0 commit comments

Comments
 (0)