[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
This commit is contained in:
Sébastien Theys
2019-10-11 07:33:23 +00:00
committed by fw-bot
parent d499779e7e
commit e7093827d2
2 changed files with 30 additions and 9 deletions
+6 -9
View File
@@ -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):
+24
View File
@@ -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):