stock_packaging_calculator: add support for custom packaging name

This commit is contained in:
Simone Orsi
2020-06-30 14:29:58 +02:00
committed by Sébastien BEAU
parent 423cd926c6
commit 311c50025a
2 changed files with 24 additions and 1 deletions

View File

@@ -72,10 +72,14 @@ class Product(models.Model):
Use ctx key `_packaging_filter` to pass a function to filter packaging Use ctx key `_packaging_filter` to pass a function to filter packaging
to be considered. to be considered.
Use ctx key `_packaging_name_getter` to pass a function to change
the display name of the packaging.
""" """
custom_filter = self.env.context.get("_packaging_filter", lambda x: x) custom_filter = self.env.context.get("_packaging_filter", lambda x: x)
name_getter = self.env.context.get("_packaging_name_getter", lambda x: x.name)
packagings = [ packagings = [
Packaging(x.id, x.name, x.qty, False) Packaging(x.id, name_getter(x), x.qty, False)
for x in self.packaging_ids.filtered(custom_filter) for x in self.packaging_ids.filtered(custom_filter)
# Exclude the ones w/ zero qty as they are useless for the math # Exclude the ones w/ zero qty as they are useless for the math
if x.qty if x.qty

View File

@@ -117,6 +117,25 @@ class TestCalc(SavepointCase):
expected, expected,
) )
def test_calc_name_get(self):
"""Test custom name getter."""
expected = [
{"id": self.pkg_pallet.id, "qty": 1, "name": "FOO " + self.pkg_pallet.name},
{
"id": self.pkg_big_box.id,
"qty": 3,
"name": "FOO " + self.pkg_big_box.name,
},
{"id": self.pkg_box.id, "qty": 1, "name": "FOO " + self.pkg_box.name},
{"id": self.uom_unit.id, "qty": 5, "name": self.uom_unit.name},
]
self.assertEqual(
self.product_a.with_context(
_packaging_name_getter=lambda x: "FOO " + x.name
).product_qty_by_packaging(2655),
expected,
)
def test_calc_sub1(self): def test_calc_sub1(self):
"""Test contained packaging behavior 1.""" """Test contained packaging behavior 1."""
expected = [ expected = [