Browse Source

Include current price in badge

Daniel Sheffield 3 years ago
parent
commit
8b4b7b761e
2 changed files with 46 additions and 17 deletions
  1. 37 13
      app/activities/PriceCheck.py
  2. 9 4
      price_check.py

+ 37 - 13
app/activities/PriceCheck.py

@@ -57,6 +57,9 @@ class PriceCheck(FocusWidget):
                 self.data[k] = 'mixed'
                 continue
             self.data[k] = ''
+        self.text_fields['rating'].set_text('')
+        self.text_fields['spread'].set_text('')
+        self.text_fields['marker'].set_text('')
         self.update()
         return self
     
@@ -72,7 +75,7 @@ class PriceCheck(FocusWidget):
         
         return self
     
-    def update_rating(self, _avg, _min, _max, price=None, quantity=None):
+    def update_rating(self, _avg, _min, _max, unit, price=None, quantity=None):
         if None in (_avg, _min, _max):
             return
         current = None if None in (price, quantity or None) else float(price/quantity)
@@ -91,10 +94,18 @@ class PriceCheck(FocusWidget):
             
             if current is not None and e <= current < a:
                 rating[idx] = '^'
-        if current == _max:
-            rating[-1] = '^'
-        self.text_fields['spread'].set_text(f"{_min:>5.2f}{''.join(chars)}{_max:<5.2f}")
-        self.text_fields['rating'].set_text(f"{' '*5}{''.join(rating)}{' '*5}")
+        
+        if current is not None:
+            self.text_fields['rating'].set_text(f"{current:>5.2f}/{unit} {current/_avg - 1:>4.0%}")
+        
+        if _min != _max:
+            if current == _max:
+                rating[-1] = '^'
+            self.text_fields['spread'].set_text(f"{_min:>5.2f}{''.join(chars)}{_max:<5.2f}")
+            self.text_fields['marker'].set_text(f"{' '*5}{''.join(rating)}{' '*5}")
+        else:
+            self.text_fields['spread'].set_text('')
+            self.text_fields['marker'].set_text('')
         
     
     def update_historic_prices(self):
@@ -114,12 +125,12 @@ class PriceCheck(FocusWidget):
         if None not in (sort, product, unit):
             self.text_fields['dbview'].set_text(
                 self.query_manager.get_historic_prices(
-                    lambda *args: self.update_rating(*args, price=price, quantity=quantity),
+                    lambda *args: self.update_rating(*args, unit, price=price, quantity=quantity),
                     sort, product, unit, organic=organic) 
             )
 
     def __init__(self, query_manager, fields,
-        top_pane, left_pane, right_pane, bottom_pane,
+        top_pane, left_pane, right_pane, bottom_pane, badge,
         autocomplete_cb):
         super().__init__([2,0,1], [
                 [2,0,0,], [2,0,2], [2,2,0], [2,2,2],
@@ -133,6 +144,7 @@ class PriceCheck(FocusWidget):
         self.left_pane = left_pane
         self.right_pane = right_pane
         self.bottom_pane = bottom_pane
+        self.badge = badge
         self.edit_fields = OrderedDict()
         self.text_fields = OrderedDict()
         self.data = OrderedDict()
@@ -170,12 +182,13 @@ class PriceCheck(FocusWidget):
         urwid.connect_signal(self.buttons['clear'], 'click', lambda x: self.clear().update())
         urwid.connect_signal(self.buttons['exit'], 'click', lambda x: show_or_exit('esc'))
         
-        dbview, spread, rating = [ urwid.Text('') for i in range(0,3) ]
+        dbview, spread, rating, marker = [ urwid.Text('') for i in range(0,4) ]
         
         self.text_fields.update({
             'dbview': dbview,
             'spread': spread,
             'rating': rating,
+            'marker': marker,
         })
         
         self.clear()
@@ -191,16 +204,16 @@ class PriceCheck(FocusWidget):
         fields = dict([
             (k, urwid.LineBox(urwid.AttrMap(self.edit_fields[k], 'streak'), title=k.title(), title_align='left')) for k in self.edit_fields
         ])
-        fields['organic'] = urwid.LineBox(urwid.AttrMap(self.organic_checkbox, 'bg'))
+        fields['organic'] = urwid.AttrMap(self.organic_checkbox, 'bg')
         fields.update({
             'dbview': urwid.LineBox(
                 urwid.AttrMap(dbview, 'streak'),
                 title="Historic Prices",
                 title_align='center',
             ),
-            'div': urwid.Divider(),
             'spread': spread,
             'rating': rating,
+            'marker': marker,
         })
 
         right_pane_widget = (16, urwid.Pile(map(
@@ -209,8 +222,19 @@ class PriceCheck(FocusWidget):
         )))
         left_pane_widget = (urwid.Pile(map(
             lambda x: fields[x] if x is not None else urwid.Divider(),
-            self.left_pane
+            self.left_pane,
         )))
+        left_pane_widget = (urwid.Pile([
+            left_pane_widget,
+            urwid.LineBox(urwid.AttrMap(
+                urwid.Pile(map(
+                    lambda x: fields[x] if x is not None else urwid.Divider(),
+                    self.badge,
+                )), 'badge',),
+                title="Current Price",
+                title_align='left',
+            )
+        ]))
         
         widget = urwid.Pile([
             banner,
@@ -233,10 +257,10 @@ class PriceCheck(FocusWidget):
                         urwid.Divider(),
                     ]))
                 ],
-                dividechars=2,
+                dividechars=1,
             ),
             urwid.Columns((left_pane_widget, right_pane_widget),
-                dividechars=2,
+                dividechars=1,
             ),
             *[ fields[c] if c is not None else urwid.Divider() for c in self.bottom_pane ],
         ])

+ 9 - 4
price_check.py

@@ -40,6 +40,9 @@ palette = [
     ('banner', 'light gray', 'dark red'),
     ('streak', 'light gray', 'black'),
     ('bg', 'dark red', 'black'),
+    ('badge', 'light gray', 'black'),
+    ('badge_green', 'light green', 'black',),
+    ('badge_red', 'light red', 'black',),
 ]
 
 top_pane = [
@@ -51,9 +54,11 @@ top_pane = [
 left_pane = [
     'product',
     'organic',
-    'div',
-    'spread',
+]
+badge = [
     'rating',
+    'spread',
+    'marker',
 ]
 right_pane = [
     'unit',
@@ -65,7 +70,7 @@ bottom_pane = [
 ]
 
 inputs = filter(
-    lambda x: x is not None and x not in ('rating','spread', 'div'),
+    lambda x: x is not None and x not in ('rating','spread', 'marker', 'div'),
     itertools.chain(
         left_pane, right_pane
     )
@@ -112,7 +117,7 @@ activity_manager = ActivityManager()
 query_manager = QueryManager(activity_manager, cur, display)
 
 activity_manager.create(PriceCheck, 'price_check',
-    query_manager, inputs, top_pane, left_pane, right_pane, bottom_pane,
+    query_manager, inputs, top_pane, left_pane, right_pane, bottom_pane, badge,
     lambda name, data: _autocomplete_callback(activity_manager, query_manager, name, data))
 
 app = GroceryPriceCheck(activity_manager)