From 7389345696720255a9d3c72ca1d9c2f4e4ecd7b8 Mon Sep 17 00:00:00 2001 From: Jigar Vaghela Date: Tue, 24 Oct 2023 23:40:45 +0530 Subject: [PATCH] [IMP] product*: create product and variant by xml file before this commit: =================== you can not create xml record for product.template and product.product and link them for same product if you like to achieve it you need to do it like ``` Box . . . 8000.0 ``` after this commit: ================== you can create product.tempalte and product.product from xml file and link it manully by attribute value like ``` Box . . . 8000.0 . . . . . . ``` closes odoo/odoo#139669 Signed-off-by: Pierre Masereel (pim) --- addons/product/models/product_product.py | 2 +- addons/product/models/product_template.py | 5 ++--- addons/product/models/product_template_attribute_line.py | 3 ++- addons/product/models/product_template_attribute_value.py | 2 +- addons/product/tests/test_variants.py | 4 ++++ addons/website_sale/tests/test_website_sale_image.py | 6 +++--- 6 files changed, 13 insertions(+), 9 deletions(-) diff --git a/addons/product/models/product_product.py b/addons/product/models/product_product.py index 99d6453c85b..be2537b293e 100644 --- a/addons/product/models/product_product.py +++ b/addons/product/models/product_product.py @@ -356,7 +356,7 @@ class ProductProduct(models.Model): def create(self, vals_list): for vals in vals_list: self.product_tmpl_id._sanitize_vals(vals) - products = super(ProductProduct, self.with_context(create_product_product=True)).create(vals_list) + products = super(ProductProduct, self.with_context(create_product_product=False)).create(vals_list) # `_get_variant_id_for_combination` depends on existing variants self.env.registry.clear_cache() return products diff --git a/addons/product/models/product_template.py b/addons/product/models/product_template.py index 1cb759ac91d..4d95b8a98c2 100644 --- a/addons/product/models/product_template.py +++ b/addons/product/models/product_template.py @@ -462,7 +462,7 @@ class ProductTemplate(models.Model): for vals in vals_list: self._sanitize_vals(vals) templates = super(ProductTemplate, self).create(vals_list) - if "create_product_product" not in self._context: + if self._context.get("create_product_product", True): templates._create_variant_ids() # This is needed to set given values to first variant after creation @@ -484,7 +484,7 @@ class ProductTemplate(models.Model): if uom_id and uom_po_id and uom_id.category_id != uom_po_id.category_id: vals['uom_po_id'] = uom_id.id res = super(ProductTemplate, self).write(vals) - if 'attribute_line_ids' in vals or (vals.get('active') and len(self.product_variant_ids) == 0): + if self._context.get("create_product_product", True) and 'attribute_line_ids' in vals or (vals.get('active') and len(self.product_variant_ids) == 0): self._create_variant_ids() if 'active' in vals and not vals.get('active'): self.with_context(active_test=False).mapped('product_variant_ids').write({'active': vals.get('active')}) @@ -672,7 +672,6 @@ class ProductTemplate(models.Model): def _create_variant_ids(self): if not self: return - self.env.flush_all() Product = self.env["product.product"] diff --git a/addons/product/models/product_template_attribute_line.py b/addons/product/models/product_template_attribute_line.py index 10143cca7f7..bdeb2bcac49 100644 --- a/addons/product/models/product_template_attribute_line.py +++ b/addons/product/models/product_template_attribute_line.py @@ -103,7 +103,8 @@ class ProductTemplateAttributeLine(models.Model): else: create_values.append(value) res = activated_lines + super().create(create_values) - res._update_product_template_attribute_values() + if self._context.get("create_product_product", True): + res._update_product_template_attribute_values() return res def write(self, values): diff --git a/addons/product/models/product_template_attribute_value.py b/addons/product/models/product_template_attribute_value.py index 5e7bd6b6489..936bf7d1c60 100644 --- a/addons/product/models/product_template_attribute_value.py +++ b/addons/product/models/product_template_attribute_value.py @@ -71,7 +71,7 @@ class ProductTemplateAttributeValue(models.Model): @api.constrains('attribute_line_id', 'product_attribute_value_id') def _check_valid_values(self): for ptav in self: - if ptav.product_attribute_value_id not in ptav.attribute_line_id.value_ids: + if ptav.ptav_active and ptav.product_attribute_value_id not in ptav.attribute_line_id.value_ids: raise ValidationError(_( "The value %(value)s is not defined for the attribute %(attribute)s" " on the product %(product)s.", diff --git a/addons/product/tests/test_variants.py b/addons/product/tests/test_variants.py index 0096a7d61c9..9a480101c90 100644 --- a/addons/product/tests/test_variants.py +++ b/addons/product/tests/test_variants.py @@ -229,6 +229,10 @@ class TestVariants(ProductVariantsCommon): self.assertEqual(template_dyn.name, 'Test Dynamical') variant_dyn = template_dyn._create_product_variant(template_dyn._get_first_possible_combination()) + if 'create_product_product' in variant_dyn._context: + new_context = dict(variant_dyn._context) + new_context.pop('create_product_product') + variant_dyn = variant_dyn.with_context(new_context) self.assertEqual(len(template_dyn.product_variant_ids), 1) variant_dyn_copy = variant_dyn.copy() diff --git a/addons/website_sale/tests/test_website_sale_image.py b/addons/website_sale/tests/test_website_sale_image.py index 472f8ccf26f..778b3b5fdde 100644 --- a/addons/website_sale/tests/test_website_sale_image.py +++ b/addons/website_sale/tests/test_website_sale_image.py @@ -91,7 +91,7 @@ class TestWebsiteSaleImage(odoo.tests.HttpCase): image_png = base64.b64encode(f.read()) # create the template, without creating the variants - template = self.env['product.template'].with_context(create_product_product=True).create({ + template = self.env['product.template'].with_context(create_product_product=False).create({ 'name': 'A Colorful Image', 'product_template_image_ids': [(0, 0, {'name': 'image 1', 'image_1920': image_gif}), (0, 0, {'name': 'image 4', 'image_1920': image_svg})], }) @@ -301,7 +301,7 @@ class TestWebsiteSaleImage(odoo.tests.HttpCase): }]) # create the template, without creating the variants - template = self.env['product.template'].with_context(create_product_product=True).create({ + template = self.env['product.template'].with_context(create_product_product=False).create({ 'name': 'Test subject', }) @@ -380,7 +380,7 @@ class TestEnvironmentWebsiteSaleImage(odoo.tests.HttpCase): f.seek(0) blue_image = base64.b64encode(f.read()) - self.template = self.env['product.template'].with_context(create_product_product=True).create({ + self.template = self.env['product.template'].with_context(create_product_product=False).create({ 'name': 'Test Remove Image', 'image_1920': blue_image, })