浏览代码

fix printing of messages

Daniel Sheffield 1 年之前
父节点
当前提交
525fe2fb9d
共有 1 个文件被更改,包括 28 次插入19 次删除
  1. 28 19
      app/activities/RecipeEditor.py

+ 28 - 19
app/activities/RecipeEditor.py

@@ -290,23 +290,21 @@ class RecipeEditor(FocusWidget):
         data = self.data
         organic = None if data['organic'] == 'mixed' else data['organic']
         sort = 'ts'
-        notice = ''
+        self.notices[None] = set()
         for r in filter(
             lambda x: next(filter(lambda x: x is widget or widget is None, x), None),
             self.ingredients
         ):
             product, quantity, unit = map(lambda x: x.get_edit_text(), r)
-            self.found[product] = '='
+            if product not in self.notices:
+                self.notices[product] = set()
+            self.found[product] = '>'
 
             try:
                 quantity = Decimal(quantity)
             except InvalidOperation:
                 quantity = None
 
-            if (product or None, unit or None, quantity or None) == (None, None, None):
-                continue
-
-
             if None in (sort or None, product or None, unit or None, quantity or None):
                 continue
 
@@ -314,29 +312,27 @@ class RecipeEditor(FocusWidget):
             if not df.empty:
                 distinct = df['quantity'].unique()
                 if len(distinct) == 1 and None in distinct:
-                    notice += f"Product '{product}' has no conversion to '{unit}'\n"
-                    self.found[product] = '>'
+                    self.notices[product].add(f"Product '{product}' has no conversion to '{unit}'")
                     continue
                 df = df.dropna()
+                self.found[product] = '='
 
             else:
                 if organic is None:
-                    notice += f"Product '{product}' not found in database\n"
+                    self.notices[product].add(f"Product '{product}' not found in database")
                     continue
 
-                self.found[product] = '~'
                 df = self.query_manager.get_historic_prices_data(unit, sort=sort, product=product, organic=None)
                 if df.empty:
-                    self.found[product] = '>'
-                    notice += f"Product '{product}' not found in database\n"
+                    self.notices[product].add(f"Product '{product}' not found in database")
                     continue
                 distinct = df['quantity'].unique()
                 if len(distinct) == 1 and None in distinct:
-                    notice += f"Product '{product}' has no conversion to '{unit}'\n"
-                    self.found[product] = '>'
+                    self.notices[product].add(f"Product '{product}' has no conversion to '{unit}'")
                     continue
                 df = df.dropna()
-                notice += f"Using {'non-' if organic else ''}organic {product}\n"
+                self.notices[product].add(f"Using {'non-' if organic else ''}organic {product}")
+                self.found[product] = '~'
 
             assert len(df['avg'].unique()) == 1, f"There should be only one average price: {df['avg'].unique()}"
 
@@ -347,6 +343,12 @@ class RecipeEditor(FocusWidget):
                 i*quantity for i in (_last, _min, _avg, _max)
             ]
 
+        for k in list(self.notices):
+            if k not in [
+                *map(lambda x: x[0].get_edit_text(), self.ingredients),
+                None
+            ]:
+                del self.notices[k]
         for k in list(self.prices):
             if k not in map(lambda x: x[0].get_edit_text(), self.ingredients):
                 del self.prices[k]
@@ -387,13 +389,19 @@ class RecipeEditor(FocusWidget):
             return self
         products = set(parsed_products)
         for product in products - set(ingredients):
-            notice += f"Product '{product}' not found in list of ingredients\n"
+            if product not in self.notices:
+                self.notices[product] = set()
+            self.notices[product].add(f"Product '{product}' not found in list of ingredients")
         for ingredient in set(ingredients) - products:
-            notice += f"Ingredient '{ingredient}' is not used\n"
+            if product not in self.notices:
+                self.notices[product] = set()
+            self.notices[ingredient].add(f"Ingredient '{ingredient}' is not used")
         if len(set(ingredients)) != len(ingredients):
-            notice += f"Some ingredients listed more than once\n"
+            self.notices[None].add(f"Some ingredients listed more than once")
 
-        self.notice.set_text(notice or 'None')
+        self.notice.set_text('\n'.join(sorted(filter(lambda x: x, [
+            '\n'.join(sorted(v)) for v in self.notices.values()
+        ]))) or 'None')
 
         return self
 
@@ -403,6 +411,7 @@ class RecipeEditor(FocusWidget):
         fname: str,
         recipe: dict,
     ):
+        self.notices = dict()
         button_group = []
         self.price_as = OrderedDict([
           ('last', RadioButton(button_group, ('streak', 'Last'), state="first True")),