Skip to content

Commit

Permalink
[IMP] product_print_category: Recompute instead of overriding the com…
Browse files Browse the repository at this point in the history
…pute method

Overriding methods is bad practice. By simply recomputing at the correct
stage in the write method, we achieve effectively the same.

Signed-off-by: Carmen Bianca BAKKER <carmen@coopiteasy.be>
  • Loading branch information
carmenbianca committed Oct 22, 2024
1 parent 589b6b4 commit d5d7e05
Showing 1 changed file with 5 additions and 24 deletions.
29 changes: 5 additions & 24 deletions product_print_category/models/product_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,14 @@ class ProductTemplate(models.Model):

to_print = fields.Boolean(related="product_variant_id.to_print", readonly=False)

@api.depends("product_variant_ids")
def _compute_product_variant_id(self):
# This is a very particular workaround for the following workflow:
#
# - Archive product.template.
# - Duplicate product.template. <-- This method is called in this step.
# - Unarchive duplicated product.template.
#
# If a product.template is archived, then its variants are also
# archived. This means that accessing `product_variant_ids` outputs an
# empty recordset, subsequently causing this compute function to wrongly
# populate `product_variant_id` with null. By disabling `active_test`,
# we prevent this problem.
#
# Upsteam bugfix in <https://github.com/odoo/odoo/pull/181811> targeted
# at v15+.
for p in self:
# We do with_context() on each individual product instead of on
# self, because doing it on self does not produce the desired result
# in Odoo 12, somehow.
p.product_variant_id = (
p.with_context(active_test=False).product_variant_ids[:1].id
)

@api.multi
def write(self, vals):
res = super().write(vals)
if self.env.context.get("update_to_print_category", True):
self._update_to_print_values(vals)
# Recompute product_variant_id to circumvent a bug where
# product_variant_ids cannot be accessed normally (empty recordset)
# because they are archived at time of computation.
if "active" in vals:
self._compute_product_variant_id()
return res

0 comments on commit d5d7e05

Please sign in to comment.