From efab33ccc5ff317e42df93113d47fdbfab52c630 Mon Sep 17 00:00:00 2001 From: Michael Genson Date: Sun, 22 Feb 2026 19:17:07 +0000 Subject: [PATCH] find unit during shopping list item creation --- .../household_services/shopping_lists.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/mealie/services/household_services/shopping_lists.py b/mealie/services/household_services/shopping_lists.py index d8fb80ff9..831877ca2 100644 --- a/mealie/services/household_services/shopping_lists.py +++ b/mealie/services/household_services/shopping_lists.py @@ -42,8 +42,7 @@ class ShoppingListService: self.list_refs = repos.group_shopping_list_recipe_refs self.data_matcher = DataMatcher(self.repos, food_fuzzy_match_threshold=self.DEFAULT_FOOD_FUZZY_MATCH_THRESHOLD) - @staticmethod - def can_merge(item1: ShoppingListItemBase, item2: ShoppingListItemBase) -> bool: + def can_merge(self, item1: ShoppingListItemBase, item2: ShoppingListItemBase) -> bool: """Check to see if this item can be merged with another item""" if any( @@ -57,20 +56,22 @@ class ShoppingListService: # check if units match or if they're compatable if item1.unit_id != item2.unit_id: - if not (item1.unit and item1.unit.standard_unit): + item1_unit = item1.unit or self.data_matcher.units_by_id.get(item1.unit_id) + item2_unit = item2.unit or self.data_matcher.units_by_id.get(item2.unit_id) + if not (item1_unit and item1_unit.standard_unit): return False - if not (item2.unit and item2.unit.standard_unit): + if not (item2_unit and item2_unit.standard_unit): return False uc = UnitConverter() - if not uc.can_convert(item1.unit.standard_unit, item2.unit.standard_unit): + if not uc.can_convert(item1_unit.standard_unit, item2_unit.standard_unit): return False # if foods match, we can merge, otherwise compare the notes return bool(item1.food_id) or item1.note == item2.note - @staticmethod def merge_items( + self, from_item: ShoppingListItemCreate | ShoppingListItemUpdateBulk, to_item: ShoppingListItemCreate | ShoppingListItemUpdateBulk | ShoppingListItemOut, ) -> ShoppingListItemUpdate: @@ -80,9 +81,11 @@ class ShoppingListService: Attributes of the `to_item` take priority over the `from_item`, except extras with overlapping keys """ - if to_item.unit and to_item.unit.standard_unit and from_item.unit and from_item.unit.standard_unit: + to_item_unit = to_item.unit or self.data_matcher.units_by_id.get(to_item.unit_id) + from_item_unit = from_item.unit or self.data_matcher.units_by_id.get(from_item.unit_id) + if to_item_unit and to_item_unit.standard_unit and from_item_unit and from_item_unit.standard_unit: merged_qty, merged_unit = merge_quantity_and_unit( - from_item.quantity or 0, from_item.unit, to_item.quantity or 0, to_item.unit + from_item.quantity or 0, from_item_unit, to_item.quantity or 0, to_item_unit ) to_item.quantity = merged_qty to_item.unit_id = merged_unit.id