|
@@ -6,6 +6,7 @@
|
|
|
# THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
|
|
|
import itertools
|
|
|
from itertools import chain
|
|
|
+from decimal import Decimal, InvalidOperation
|
|
|
from typing import List, Tuple, Union, Iterable, Callable
|
|
|
from urwid import (
|
|
|
connect_signal,
|
|
@@ -92,7 +93,7 @@ class RecipeEditor(FocusWidget):
|
|
|
in_same_row(name),
|
|
|
map(to_numbered_field, self.data.items())
|
|
|
))
|
|
|
-
|
|
|
+
|
|
|
for k,v in data.items():
|
|
|
if f'{k[0]}#{k[1]}' == name or v:
|
|
|
continue
|
|
@@ -145,6 +146,40 @@ class RecipeEditor(FocusWidget):
|
|
|
return self.update()
|
|
|
|
|
|
def update(self):
|
|
|
+ data = self.data
|
|
|
+ organic = None if data['organic'] == 'mixed' else data['organic']
|
|
|
+ sort = 'ts'
|
|
|
+ not_found = '='
|
|
|
+ price = list(map(Decimal,[0,0,0]))
|
|
|
+ for r in self.ingredients:
|
|
|
+ product, quantity, unit = map(lambda x: x.get_edit_text(), r)
|
|
|
+
|
|
|
+ try:
|
|
|
+ quantity = Decimal(quantity)
|
|
|
+ except InvalidOperation:
|
|
|
+ quantity = None
|
|
|
+
|
|
|
+ if None in (sort, product, unit):
|
|
|
+ not_found = '>'
|
|
|
+ continue
|
|
|
+
|
|
|
+ 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
|
|
|
+ df = self.query_manager.get_historic_prices_data(unit, sort=sort, product=product, organic=None)
|
|
|
+ if df.empty:
|
|
|
+ not_found = '>'
|
|
|
+ continue
|
|
|
+
|
|
|
+ assert len(df['avg'].unique()) == 1, f"There should be only one average price: {df['avg'].unique()}"
|
|
|
+
|
|
|
+ _avg, _min, _max = list(
|
|
|
+ map(Decimal,df[['avg','min','max']].iloc[0])
|
|
|
+ )
|
|
|
+ price[0] += _min*quantity
|
|
|
+ price[1] += _avg*quantity
|
|
|
+ price[2] += _max*quantity
|
|
|
+ self.price.set_text(f'Cost: {not_found}{price[1]:.2f}')
|
|
|
return self
|
|
|
|
|
|
def __init__(self,
|
|
@@ -172,10 +207,14 @@ 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 = Text(f"Feeds: {recipe['feeds'] or ''}")
|
|
|
+ self.price = Text(f"Cost: 0")
|
|
|
|
|
|
bottom_pane = [
|
|
|
self.organic,
|
|
|
LineBox(self.instructions, title=f'Instructions'),
|
|
|
+ self.feeds,
|
|
|
+ self.price,
|
|
|
]
|
|
|
|
|
|
self.query_manager = query_manager
|
|
@@ -221,12 +260,12 @@ class RecipeEditor(FocusWidget):
|
|
|
ingredient[0],
|
|
|
self.apply_choice,
|
|
|
lambda: activity_manager.show(self.update())
|
|
|
- ), 'streak'), title=f'Ingredient {idx}', title_align='left'
|
|
|
+ ), 'streak'), title=f'Product', title_align='left'
|
|
|
) for idx, ingredient in enumerate(self.ingredients)
|
|
|
]
|
|
|
middle_pane = [
|
|
|
LineBox(
|
|
|
- ingredient[1], title=f'Quantity {idx}', title_align='left'
|
|
|
+ ingredient[1], title=f'Quantity', title_align='left'
|
|
|
) for idx, ingredient in enumerate(self.ingredients)
|
|
|
]
|
|
|
right_pane = [
|
|
@@ -235,7 +274,7 @@ class RecipeEditor(FocusWidget):
|
|
|
ingredient[2],
|
|
|
self.apply_choice,
|
|
|
lambda: activity_manager.show(self.update())
|
|
|
- ), 'streak'), title=f'Unit {idx}', title_align='left'
|
|
|
+ ), 'streak'), title=f'Unit', title_align='left'
|
|
|
) for idx, ingredient in enumerate(self.ingredients)
|
|
|
]
|
|
|
gutter = [
|
|
@@ -263,8 +302,8 @@ class RecipeEditor(FocusWidget):
|
|
|
]))
|
|
|
], dividechars=1),
|
|
|
'bottom_pane': Pile(bottom_pane),
|
|
|
- 'right_pane': (16, Pile(right_pane)),
|
|
|
- 'middle_pane': (16, Pile(middle_pane)),
|
|
|
+ 'right_pane': (9, Pile(right_pane)),
|
|
|
+ 'middle_pane': (12, Pile(middle_pane)),
|
|
|
'left_pane': Pile(left_pane),
|
|
|
'gutter': (8, Pile(gutter))
|
|
|
}
|
|
@@ -277,10 +316,9 @@ class RecipeEditor(FocusWidget):
|
|
|
components['left_pane'],
|
|
|
components['middle_pane'],
|
|
|
components['right_pane'],
|
|
|
- (1,Divider()),
|
|
|
- components['gutter']),
|
|
|
- dividechars=0,
|
|
|
- ),
|
|
|
+ #(1,Divider()),
|
|
|
+ #components['gutter'],
|
|
|
+ ), dividechars=0),
|
|
|
components['bottom_pane'],
|
|
|
])
|
|
|
widget = Filler(widget, 'top')
|
|
@@ -298,8 +336,9 @@ class RecipeEditor(FocusWidget):
|
|
|
) if x == n),
|
|
|
[
|
|
|
'instructions',
|
|
|
- 'ingredients', 'units', 'add',
|
|
|
+ 'ingredients', 'quantity', 'units', 'add',
|
|
|
'organic',
|
|
|
'clear', 'exit',
|
|
|
]
|
|
|
))
|
|
|
+ self.update()
|