Skip to content

Commit

Permalink
[FIX] product: update variants when modifying exclusions
Browse files Browse the repository at this point in the history
Before this commit, when importing product template attribute exclusions,
variants were not updated accordingly.

Now, variants will be created, archived, or deleted when creating,
modifying, or removing exclusions.

opw-3693065

closes #155103

Signed-off-by: Victor Feyens (vfe) <vfe@odoo.com>
  • Loading branch information
chevalierv committed Mar 1, 2024
1 parent 7ba07df commit a20da96
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
21 changes: 21 additions & 0 deletions addons/product/models/product_attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,27 @@ class ProductTemplateAttributeExclusion(models.Model):
'product.template.attribute.value', relation="product_attr_exclusion_value_ids_rel",
string='Attribute Values', domain="[('product_tmpl_id', '=', product_tmpl_id), ('ptav_active', '=', True)]")

@api.model_create_multi
def create(self, vals_list):
exclusions = super().create(vals_list)
exclusions.product_tmpl_id._create_variant_ids()
return exclusions

def unlink(self):
# Keep a reference to the related templates before the deletion.
templates = self.product_tmpl_id
res = super().unlink()
templates._create_variant_ids()
return res

def write(self, values):
templates = self.env['product.template']
if 'product_tmpl_id' in values:
templates = self.product_tmpl_id
res = super().write(values)
(templates | self.product_tmpl_id)._create_variant_ids()
return res


class ProductAttributeCustomValue(models.Model):
_name = "product.attribute.custom.value"
Expand Down
30 changes: 30 additions & 0 deletions addons/product/tests/test_variants.py
Original file line number Diff line number Diff line change
Expand Up @@ -1401,6 +1401,7 @@ def get_ptav(template, ptav):
)

cls.smartphone_s = get_ptav(cls.smartphone, cls.size_attribute_s)
cls.smartphone_l = get_ptav(cls.smartphone, cls.size_attribute_l)
cls.smartphone_256 = get_ptav(cls.smartphone, cls.storage_attr_value_256)
cls.smartphone_128 = get_ptav(cls.smartphone, cls.storage_attr_value_128)

Expand Down Expand Up @@ -1471,3 +1472,32 @@ def test_variants_2_exclusions_different_lines(self):
'exclude_for': [(2, self.smartphone_s.exclude_for.ids[0], 0)]
})
self.assertEqual(len(self.smartphone.product_variant_ids), 3, 'With one exclusion, the smartphone should have 3 active different variants')

@mute_logger('odoo.models.unlink')
def test_exclusions_crud(self):
""" Make sure that exclusions creation, update & delete are correctly handled.
Exclusions updates are not necessarily done from a specific template.
"""
PTAE = self.env['product.template.attribute.exclusion']

exclude = PTAE.create({
'product_tmpl_id': self.smartphone.id,
'product_template_attribute_value_id': self.smartphone_s.id,
'value_ids': [Command.set(self.smartphone_256.ids)]
})
self.assertEqual(len(self.smartphone.product_variant_ids), 3)
self.assertNotIn(
self.smartphone_s + self.smartphone_256,
[product.product_template_attribute_value_ids for product in self.smartphone.product_variant_ids],
)

exclude.value_ids = [Command.set(self.smartphone_128.ids)]
self.assertEqual(len(self.smartphone.product_variant_ids), 3)
self.assertNotIn(
self.smartphone_s + self.smartphone_128,
[product.product_template_attribute_value_ids for product in self.smartphone.product_variant_ids],
)

exclude.unlink()
self.assertEqual(len(self.smartphone.product_variant_ids), 4)

0 comments on commit a20da96

Please sign in to comment.