Skip to content

Commit

Permalink
[IMP]product_packaging_level: recover name_get policy flow
Browse files Browse the repository at this point in the history
  • Loading branch information
sonhd91 committed Mar 5, 2024
1 parent 6c77af9 commit ea55c00
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
24 changes: 21 additions & 3 deletions product_packaging_level/models/product_packaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class ProductPackaging(models.Model):
_inherit = "product.packaging"
_order = "product_id, level_sequence"

name = fields.Char(compute="_compute_name", store=True, readonly=False)
packaging_level_id = fields.Many2one(
"product.packaging.level",
required=True,
Expand Down Expand Up @@ -126,11 +127,28 @@ def _onchange_package_type(self):
if package_type_level_id:
self.packaging_level_id = package_type_level_id

@api.onchange("packaging_level_id", "package_type_id", "name", "name_policy")
def _onchange_name(self):
@api.depends(
"package_type_id",
"name_policy",
"packaging_level_id",
)
def _compute_name(self):
for rec in self:
rec.name = rec.change_name_policy()

# Keep this method to respect translations on level name
@api.depends("product_id", "packaging_level_id", "name_policy")
def _compute_display_name(self):
for record in self:
if record.product_id and record.packaging_level_id:
record.display_name = record.change_name_policy()

Check warning on line 144 in product_packaging_level/models/product_packaging.py

View check run for this annotation

Codecov / codecov/patch

product_packaging_level/models/product_packaging.py#L144

Added line #L144 was not covered by tests
else:
return super()._compute_display_name()

Check warning on line 146 in product_packaging_level/models/product_packaging.py

View check run for this annotation

Codecov / codecov/patch

product_packaging_level/models/product_packaging.py#L146

Added line #L146 was not covered by tests

def change_name_policy(self):
new_name = self.name
if self.name_policy == "by_package_level":
new_name = self.packaging_level_id.display_name
elif self.name_policy == "by_package_type":
new_name = self.package_type_id.name
self.name = new_name
return new_name
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_display_name(self):

def test_name_by_level(self):
self.packaging.name_policy = "by_package_level"
self.assertEqual(self.packaging.name, "Packaging Test")
self.assertEqual(self.packaging.name, "Packaging Level Test (TEST2)")

def test_name_by_package_type(self):
# Required for `package_type_id` to be visible in the view
Expand All @@ -48,7 +48,6 @@ def test_name_by_package_type(self):
)
self.packaging.name_policy = "by_package_type"
self.packaging.package_type_id = self.package_type_box
self.packaging._onchange_name()
self.assertEqual(self.packaging.name, "Box")

def test_name_by_user_defined(self):
Expand All @@ -61,12 +60,10 @@ def test_name_by_user_defined(self):
self.packaging.name = packaging_name
# try to change package_type_id
self.packaging.package_type_id = self.package_type_box
self.packaging._onchange_name()
self.assertEqual(self.packaging.name, packaging_name)

# try to change packaging level
self.packaging.packaging_level_id = self.level
self.packaging._onchange_name()
self.assertEqual(self.packaging.name, packaging_name)

def test_name_by_package_type_without_group_tracking_lot(self):
Expand Down
6 changes: 6 additions & 0 deletions product_packaging_level/views/product_packaging_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
<field name="model">product.packaging</field>
<field name="inherit_id" ref="stock.product_packaging_form_view" />
<field name="arch" type="xml">
<field name="name" position="before">
<field name="display_name" string="Packaging" />
</field>
<field name="name" position="attributes">
<attribute name="invisible">1</attribute>
</field>
<xpath expr="//field[@name='product_id']" position="before">
<field name="name_policy" invisible="1" />
</xpath>
Expand Down

0 comments on commit ea55c00

Please sign in to comment.