Skip to content

Commit

Permalink
[FIX] product_print_category: Improve computation of product_variant_…
Browse files Browse the repository at this point in the history
…id for inactive templates

Signed-off-by: Carmen Bianca BAKKER <[email protected]>
  • Loading branch information
carmenbianca committed Sep 27, 2024
1 parent a00f0c6 commit 93ce600
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
24 changes: 24 additions & 0 deletions product_print_category/models/product_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,30 @@ 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)
Expand Down
1 change: 1 addition & 0 deletions product_print_category/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import test_product_print_category
from . import test_product_template
21 changes: 21 additions & 0 deletions product_print_category/tests/test_product_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# SPDX-FileCopyrightText: 2024 Coop IT Easy SC
#
# SPDX-License-Identifier: AGPL-3.0-or-later

from odoo.tests.common import SavepointCase


class TestProductTemplate(SavepointCase):
def test_product_variant_id(self):
"""Test that product_variant_id is correctly populated for cloned
archived products.
"""
product = self.env["product.template"].create({"name": "Test"})
breakpoint()
product.active = False
new_product = product.copy()
new_variants = new_product.with_context(active_test=False).product_variant_ids
self.assertFalse(new_product.active)
self.assertEqual(new_product.product_variant_id, new_variants[0])
new_product.active = True
self.assertEqual(new_product.product_variant_id, new_variants[0])

0 comments on commit 93ce600

Please sign in to comment.