From e7093827d2892df23d6bc5dd91343d656e995058 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Theys?= Date: Wed, 9 Oct 2019 15:44:54 +0000 Subject: [PATCH] [FIX] product: make copy of variants just copy template Variants are generated depending on the configuration of attributes and values on the template, so copying them does not make sense. For convenience the template is copied instead and its first variant is returned. closes #38151 X-original-commit: 259f3b7fcdb88d1f458ee7ea3667964412b88ea1 --- addons/product/models/product.py | 15 ++++++--------- addons/product/tests/test_variants.py | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/addons/product/models/product.py b/addons/product/models/product.py index 1ba5110b1a9..c13fa083c0f 100644 --- a/addons/product/models/product.py +++ b/addons/product/models/product.py @@ -393,16 +393,13 @@ class ProductProduct(models.Model): @api.returns('self', lambda value: value.id) def copy(self, default=None): - # TDE FIXME: clean context / variant brol - if default is None: - default = {} - if self._context.get('variant'): - # if we copy a variant or create one, we keep the same template - default['product_tmpl_id'] = self.product_tmpl_id.id - elif 'name' not in default: - default['name'] = self.name + """Variants are generated depending on the configuration of attributes + and values on the template, so copying them does not make sense. - return super(ProductProduct, self).copy(default=default) + For convenience the template is copied instead and its first variant is + returned. + """ + return self.product_tmpl_id.copy(default=default).product_variant_id @api.model def _search(self, args, offset=0, limit=None, order=None, count=False, access_rights_uid=None): diff --git a/addons/product/tests/test_variants.py b/addons/product/tests/test_variants.py index 8c092ecfefe..fb73867c2bb 100644 --- a/addons/product/tests/test_variants.py +++ b/addons/product/tests/test_variants.py @@ -186,6 +186,30 @@ class TestVariants(common.TestProductCommon): })] }) + def test_variants_copy(self): + template = self.env['product.template'].create({ + 'name': 'Test Copy', + 'attribute_line_ids': [(0, 0, { + 'attribute_id': self.size_attr.id, + 'value_ids': [(4, self.size_attr_value_s.id), (4, self.size_attr_value_m.id)], + })] + }) + self.assertEqual(len(template.product_variant_ids), 2) + self.assertEqual(template.name, 'Test Copy') + + # test copy of template + template_copy = template.copy() + self.assertEqual(template.name, 'Test Copy') + self.assertEqual(template_copy.name, 'Test Copy (copy)') + self.assertEqual(len(template_copy.product_variant_ids), 2) + + # test copy of variant (actually just copying template) + variant_copy = template_copy.product_variant_ids[0].copy() + self.assertEqual(template.name, 'Test Copy') + self.assertEqual(template_copy.name, 'Test Copy (copy)') + self.assertEqual(variant_copy.name, 'Test Copy (copy) (copy)') + self.assertEqual(len(variant_copy.product_variant_ids), 2) + class TestVariantsNoCreate(common.TestProductCommon):