Quellcode durchsuchen

add per serve cost and fix calculation of exact/approximate price

Daniel Sheffield vor 2 Jahren
Ursprung
Commit
ba0aa98996
1 geänderte Dateien mit 27 neuen und 13 gelöschten Zeilen
  1. 27 13
      app/activities/RecipeEditor.py

+ 27 - 13
app/activities/RecipeEditor.py

@@ -199,7 +199,6 @@ class RecipeEditor(FocusWidget):
         self.instructions.set_edit_text('')
         self.fname.set_edit_text('')
         self.notice.set_text('')
-        self.feeds.edit_text = ''
         return self.update()
 
     def init_ingredients(self):
@@ -271,10 +270,9 @@ class RecipeEditor(FocusWidget):
             lambda x: None not in map(lambda x: x or None, x), [
                 list(map(lambda x: x.get_edit_text(), x)) for x in self.ingredients
         ])))
-        serves = self.feeds.value()
         if serves:
-            n, d = serves.as_integer_ratio()
-            yml['feeds'] = float(self.feeds.value()) if d != 1 else n
+            n, d = self.feeds.as_integer_ratio()
+            yml['feeds'] = float(self.feeds) if d != 1 else n
         else:
             yml['feeds'] = None
         yml['instructions'] = literal_str('\n'.join(map(
@@ -292,7 +290,6 @@ class RecipeEditor(FocusWidget):
         data = self.data
         organic = None if data['organic'] == 'mixed' else data['organic']
         sort = 'ts'
-        not_found = '='
         for r in filter(
             lambda x: next(filter(lambda x: x is widget or widget is None, x), None),
             self.ingredients
@@ -308,15 +305,16 @@ class RecipeEditor(FocusWidget):
                 continue
 
             if None in (sort or None, product or None, unit or None, quantity or None):
-                not_found = '>'
+                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:
-                not_found = '~' if not_found == '=' else not_found
+                self.found[product] = '~'
                 df = self.query_manager.get_historic_prices_data(unit, sort=sort, product=product, organic=None)
                 if df.empty:
-                    not_found = '>'
+                    self.found[product] = '>'
                     continue
 
             assert len(df['avg'].unique()) == 1, f"There should be only one average price: {df['avg'].unique()}"
@@ -324,13 +322,19 @@ class RecipeEditor(FocusWidget):
             _last, _avg, _min, _max = list(
                 map(Decimal,df[['last', 'avg','min','max']].iloc[0])
             )
-            self.prices[r[0].get_edit_text()] = [
+            self.prices[product] = [
                 i*quantity for i in (_last, _min, _avg, _max)
             ]
 
         for k in list(self.prices):
             if k not in map(lambda x: x[0].get_edit_text(), self.ingredients):
                 del self.prices[k]
+        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]
 
         price = [
             sum([self.prices[p][i] for p in self.prices]) for i in range(4)
@@ -339,8 +343,16 @@ class RecipeEditor(FocusWidget):
             lambda x: self.price_as[x[1]].state,
             enumerate(self.price_as)
         ))[0]
+        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())
+                )
+            )
+        )
         self.price.set_text(
             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)))
@@ -381,6 +393,7 @@ class RecipeEditor(FocusWidget):
             connect_signal(b, 'postchange', lambda w,_: self.update(w))
         self.fname = Edit('', fname)
         self.prices = dict()
+        self.found = dict()
         self.components = dict()
         self.buttons = {
           'clear': Button(('streak', 'Clear')),
@@ -398,15 +411,16 @@ class RecipeEditor(FocusWidget):
 
         self.organic = NoTabCheckBox(('bg', "Organic"), state='mixed')
         self.instructions = Edit('', edit_text=recipe['instructions'] or u'', multiline=True, allow_tab=True)
-        self.feeds = FloatEdit(f'Serves: ', f"{recipe['feeds']}" or '')
-        self.price = Text(f"Cost: 0")
+        self.feeds = Decimal(recipe['feeds'])
+        self.price = Text('')
         self.notice = Text('')
+        feeds = FloatEdit(f'Serves: ', self.feeds)
 
         bottom_pane = [
             self.organic,
             LineBox(self.instructions, title=f'Instructions'),
-            self.feeds,
-            Columns([self.price, *[(9, w) for _,w in self.price_as.items()], Divider()]),
+            feeds,
+            Columns([('weight', 2, self.price), *[(9, w) for _,w in self.price_as.items()], Divider()]),
             LineBox(self.notice, title='Errors', title_align='left'),
         ]
         self.activity_manager = activity_manager