From 21ae503cf401afe4dddae6225fd346f89a210aeb Mon Sep 17 00:00:00 2001 From: "Lucas Perais (lpe)" Date: Thu, 29 Aug 2019 08:25:18 +0000 Subject: [PATCH] [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) --- addons/product/models/product_template.py | 9 ++++++++- addons/product/tests/test_variants.py | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/addons/product/models/product_template.py b/addons/product/models/product_template.py index 1b219a179e3..24c3c44bd44 100644 --- a/addons/product/models/product_template.py +++ b/addons/product/models/product_template.py @@ -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 diff --git a/addons/product/tests/test_variants.py b/addons/product/tests/test_variants.py index 251996a2526..97323a0cee3 100644 --- a/addons/product/tests/test_variants.py +++ b/addons/product/tests/test_variants.py @@ -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])