From 7be28d9f2f4a7abb9acc556bb3dcf8e80f4dce16 Mon Sep 17 00:00:00 2001 From: Simone Orsi Date: Mon, 3 Aug 2020 10:20:33 +0200 Subject: [PATCH] packaging_calculator: fix sorting Make sure unit is always the last element in the list. --- stock_packaging_calculator/models/product.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/stock_packaging_calculator/models/product.py b/stock_packaging_calculator/models/product.py index 4d670716f..89eadf5c5 100644 --- a/stock_packaging_calculator/models/product.py +++ b/stock_packaging_calculator/models/product.py @@ -79,12 +79,16 @@ class Product(models.Model): """ 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, 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 - ] + packagings = sorted( + [ + 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 + ], + reverse=True, + key=lambda x: x.qty, + ) # Add minimal unit packagings.append( # NOTE: the ID here could clash w/ one of the packaging's. @@ -92,7 +96,7 @@ class Product(models.Model): # You can use `is_unit` to check this. Packaging(self.uom_id.id, self.uom_id.name, self.uom_id.factor, True) ) - return sorted(packagings, reverse=True, key=lambda x: x.qty) + return packagings def _product_qty_by_packaging(self, pkg_by_qty, qty, with_contained=False): """Produce a list of dictionaries of packaging info."""