[FIX] product: name search on dynamic product template

Have a product template, assign a dynamic attribute to
(the corresponding variant will be created when used in a SO)

Try to name_search for it
e.g.: in the product configurator modal, type some letters to catch it in theory

Before this commit, the product template was not found by the name search
this is because the template doesn't have any variant yet

After this commit, it is found by the name search

OPW 2060603

closes odoo/odoo#36215

Signed-off-by: Lucas Perais (lpe) <lpe@odoo.com>
This commit is contained in:
Lucas Perais (lpe)
2019-08-29 10:19:47 +00:00
parent 3950702c86
commit 21ae503cf4
2 changed files with 30 additions and 1 deletions
+8 -1
View File
@@ -407,13 +407,20 @@ class ProductTemplate(models.Model):
Product = self.env['product.product']
templates = self.browse([])
domain_no_variant = [('product_variant_ids', '=', False)]
while True:
domain = templates and [('product_tmpl_id', 'not in', templates.ids)] or []
args = args if args is not None else []
products_ns = Product._name_search(name, args+domain, operator=operator, name_get_uid=name_get_uid)
products = Product.browse([x[0] for x in products_ns])
templates |= products.mapped('product_tmpl_id')
if (not products) or (limit and (len(templates) > limit)):
current_round_templates = self.browse([])
if not products:
domain_template = args + domain_no_variant + (templates and [('id', 'not in', templates.ids)] or [])
template_ns = super(ProductTemplate, self)._name_search(name=name, args=domain_template, operator=operator, limit=limit, name_get_uid=name_get_uid)
current_round_templates |= self.browse([ns[0] for ns in template_ns])
templates |= current_round_templates
if (not products and not current_round_templates) or (limit and (len(templates) > limit)):
break
# re-apply product.template order + name_get
+22
View File
@@ -817,3 +817,25 @@ class TestVariantsArchive(common.TestProductCommon):
"We should have re-activated one variant only.")
Product._revert_method('unlink')
def test_name_search_dynamic_attributes(self):
dynamic_attr = self.env['product.attribute'].create({
'name': 'Dynamic',
'create_variant': 'dynamic',
'value_ids': [(0, False, {'name': 'ValueDynamic'})],
})
template = self.env['product.template'].create({
'name': 'cimanyd'
})
self.assertEqual(len(template.product_variant_ids), 1)
template.write({
'attribute_line_ids': [(0, False, {
'attribute_id': dynamic_attr.id,
'value_ids': [(4, dynamic_attr.value_ids[0].id, False)],
})]
})
self.assertEqual(len(template.product_variant_ids), 0)
name_searched = self.env['product.template'].name_search(name='cima')
self.assertIn(template.id, [ng[0] for ng in name_searched])