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

X-original-commit: a20da96
  • Loading branch information
chevalierv committed Mar 4, 2024
1 parent 15d6798 commit 4113328
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
23 changes: 22 additions & 1 deletion addons/product/models/product_template_attribute_exclusion.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import fields, models
from odoo import api, fields, models


class ProductTemplateAttributeExclusion(models.Model):
Expand All @@ -24,3 +24,24 @@ class ProductTemplateAttributeExclusion(models.Model):
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
30 changes: 30 additions & 0 deletions addons/product/tests/test_variants.py
Original file line number Diff line number Diff line change
Expand Up @@ -1391,6 +1391,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 @@ -1461,3 +1462,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 4113328

Please sign in to comment.