Parcourir la source

handle null unit conversion in frontend and display appropriate messages when products not found or unit conversion not available

Daniel Sheffield il y a 2 ans
Parent
commit
d4e3053518
3 fichiers modifiés avec 33 ajouts et 12 suppressions
  1. 29 8
      app/activities/RecipeEditor.py
  2. 1 1
      app/db_utils.py
  3. 3 3
      app/price_view.py

+ 29 - 8
app/activities/RecipeEditor.py

@@ -290,11 +290,13 @@ class RecipeEditor(FocusWidget):
         data = self.data
         organic = None if data['organic'] == 'mixed' else data['organic']
         sort = 'ts'
+        notice = ''
         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] = '='
 
             try:
                 quantity = Decimal(quantity)
@@ -304,18 +306,37 @@ class RecipeEditor(FocusWidget):
             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):
-                self.found[None] = '>'
                 continue
-            self.found[product] = '='
 
             df = self.query_manager.get_historic_prices_data(unit, sort=sort, product=product, organic=organic)
-            if df.empty:
+            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] = '>'
+                    continue
+                df = df.dropna()
+
+            else:
+                if organic is None:
+                    notice += f"Product '{product}' not found in database\n"
+                    continue
+
                 self.found[product] = '~'
+                notice += f"Product '{product} (organic={organic})' not found in database\n"
                 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"
                     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] = '>'
+                    continue
+                df = df.dropna()
 
             assert len(df['avg'].unique()) == 1, f"There should be only one average price: {df['avg'].unique()}"
 
@@ -332,7 +353,7 @@ class RecipeEditor(FocusWidget):
         for k in list(self.found):
             if k not in [
                 *map(lambda x: x[0].get_edit_text(), self.ingredients),
-                None
+                ''
             ]:
                 del self.found[k]
 
@@ -346,7 +367,8 @@ class RecipeEditor(FocusWidget):
         not_found = next(
             filter(lambda x: x == '>', self.found.values()), next(
                 filter(lambda x: x == '~', self.found.values()), next(
-                    filter(lambda x: x == '=', self.found.values())
+                    filter(lambda x: x == '=', self.found.values()),
+                    None,
                 )
             )
         )
@@ -354,7 +376,6 @@ class RecipeEditor(FocusWidget):
             f'Cost: {not_found}{price[price_as]}'
             f', Per Serve: {not_found}{price[price_as]/self.feeds}'
         )
-        notice = ''
         ingredients = list(filter(lambda x: x, map(lambda x: x[0].get_edit_text(), self.ingredients)))
         parsed_products = list(get_products_from_xhtml(markdown(self.instructions.get_edit_text())))
         fname = self.fname.get_edit_text()
@@ -366,9 +387,9 @@ 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";
+            notice += f"Product '{product}' not found in list of ingredients\n"
         for ingredient in set(ingredients) - products:
-            notice += f"Ingredient '{ingredient}' is not used\n";
+            notice += f"Ingredient '{ingredient}' is not used\n"
         if len(set(ingredients)) != len(ingredients):
             notice += f"Some ingredients listed more than once\n"
 

+ 1 - 1
app/db_utils.py

@@ -30,7 +30,7 @@ display_map = {
     'ts': lambda x: f"{time.strftime('%Y-%m-%d %H:%M', (x.year, x.month, x.day, x.hour, x.minute, 0, 0, 0, 0))}",
     '%d/%m/%y %_I%P': lambda x: f"{time.strftime('%d/%m/%y %_I%P', (x.year, x.month, x.day, x.hour, x.minute, 0, 0, 0, 0))}",
     'price': lambda x: f'{x:.4f}',
-    'quantity': lambda x: f'{x:.2f}',
+    'quantity': lambda x: f'{x:.2f}' if x is not None else None,
     'organic': lambda x: 'yes' if x else 'no',
 }
 display_mapper: Callable[

+ 3 - 3
app/price_view.py

@@ -48,9 +48,9 @@ def get_where(unit, product=None, category=None, group=None, organic=None, limit
                 interval=SQL("{literal}::interval").format(literal=Literal(limit))
             )
         )
-    where.append(SQL(
-        'convert_unit(units.name, {unit}, {product}) IS NOT NULL'
-    ).format(unit=Literal(unit), product=Literal(product)))
+    #where.append(SQL(
+    #    'convert_unit(units.name, {unit}, {product}) IS NOT NULL'
+    #).format(unit=Literal(unit), product=Literal(product)))
     return SQL('').join([
         SQL("WHERE"
             "\n      "),