diff --git a/stock_packaging_calculator/models/product.py b/stock_packaging_calculator/models/product.py index 88332de81..b81ac2107 100644 --- a/stock_packaging_calculator/models/product.py +++ b/stock_packaging_calculator/models/product.py @@ -72,10 +72,14 @@ class Product(models.Model): Use ctx key `_packaging_filter` to pass a function to filter packaging 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) + name_getter = self.env.context.get("_packaging_name_getter", lambda x: x.name) 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) # Exclude the ones w/ zero qty as they are useless for the math if x.qty diff --git a/stock_packaging_calculator/tests/test_packaging_calc.py b/stock_packaging_calculator/tests/test_packaging_calc.py index fcee8dd5a..7a6b643b3 100644 --- a/stock_packaging_calculator/tests/test_packaging_calc.py +++ b/stock_packaging_calculator/tests/test_packaging_calc.py @@ -117,6 +117,25 @@ class TestCalc(SavepointCase): 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): """Test contained packaging behavior 1.""" expected = [