Skip to content

Commit

Permalink
[FIX] product_variant_default_code: recurrent prefix
Browse files Browse the repository at this point in the history
With a non automatic reference mask that has a code prefix, when that
reference mask is recomputed, we could be repeating the prefix without
noticing it. For instance, when we change an product.attribute name and
that triggers all the default codes recomputations.

In a product template with variants (i.e.: Conference Chair)

- You'll have a reference mask ([Legs])
- Add a prefix: cc

--> The prefix is duplicated in the reference mask: cccc[Legs]

- Now you can delete de prefix manually from the mask and the resulting mask
  will be right: cc[Legs]
- Now change the name of the attribute Legs to something else.

--> The mask computation is retriggered and we get a duplicated prefix again cccc[Legs].

An extra issue would be that the mask isn't recomputed with the new attribute
name, although that would need some extra work.

TT48322
  • Loading branch information
chienandalu committed Mar 15, 2024
1 parent acd9b3e commit adbfa0e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
8 changes: 7 additions & 1 deletion product_variant_default_code/models/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,13 @@ def _compute_reference_mask(self):
if automask or not rec.reference_mask:
rec.reference_mask = rec._get_default_mask()
elif not automask and rec.code_prefix:
rec.reference_mask = rec.code_prefix + rec.reference_mask
reference_mask = rec.reference_mask
# Avoid prefixing the mask twice (or more).
# TODO: This needs a better design with a third field that sums both
# or using the sum of them in the variant default code computation
if reference_mask.startswith(rec.code_prefix):
reference_mask = reference_mask.lstrip(rec.code_prefix)
rec.reference_mask = rec.code_prefix + reference_mask

def _inverse_reference_mask(self):
self._compute_reference_mask()
Expand Down
30 changes: 17 additions & 13 deletions product_variant_default_code/tests/test_variant_default_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,17 +368,21 @@ def test_18_both_prefix_and_mask_changing(self):
"reference_mask": "fix-[TColor]/[TSize]",
}
)

for product in self.template1.mapped("product_variant_ids"):
expected_code = (
self.template1.code_prefix
+ "fix-"
+ product.product_template_attribute_value_ids.filtered(
lambda x: x.product_attribute_value_id.attribute_id == self.attr2
).name[0:2]
+ "/"
+ product.product_template_attribute_value_ids.filtered(
lambda x: x.product_attribute_value_id.attribute_id == self.attr1
).name[0:2]
)
self.assertEqual(product.default_code, expected_code)
attr1 = product.product_template_attribute_value_ids.filtered(
lambda x: x.product_attribute_value_id.attribute_id == self.attr2
).name[0:2]
attr2 = product.product_template_attribute_value_ids.filtered(
lambda x: x.product_attribute_value_id.attribute_id == self.attr1
).name[0:2]
self.assertEqual(product.default_code, f"pre/fix-{attr1}/{attr2}")
# The reference_mask stays the same even if recomputed
self.template1._compute_reference_mask()
for product in self.template1.mapped("product_variant_ids"):
attr1 = product.product_template_attribute_value_ids.filtered(
lambda x: x.product_attribute_value_id.attribute_id == self.attr2
).name[0:2]
attr2 = product.product_template_attribute_value_ids.filtered(
lambda x: x.product_attribute_value_id.attribute_id == self.attr1
).name[0:2]
self.assertEqual(product.default_code, f"pre/fix-{attr1}/{attr2}")

0 comments on commit adbfa0e

Please sign in to comment.