Browse Source

Make db session interactive during transaction entry

Daniel Sheffield 3 years ago
parent
commit
43f2514752
2 changed files with 33 additions and 19 deletions
  1. 31 11
      grocery_transactions.py
  2. 2 8
      txn_view.py

+ 31 - 11
grocery_transactions.py

@@ -9,6 +9,7 @@ import pandas as pd
 from txn_view import get_statement as get_session_transactions_statement
 from dateutil.parser import parse as parse_time
 from datetime import datetime
+import sys
 try:
     import psycopg2
     from psycopg2.sql import SQL
@@ -456,7 +457,7 @@ def _set_focus_path(container, path):
     raise IndexError
 
 class GroceryTransactionEditor(urwid.WidgetPlaceholder):
-    def __init__(self, fields):
+    def __init__(self, log, fields):
         super(GroceryTransactionEditor, self).__init__(urwid.SolidFill(u'/'))
         self.organic_checkbox = NoTabCheckBox(
             u"Organic",
@@ -465,7 +466,14 @@ class GroceryTransactionEditor(urwid.WidgetPlaceholder):
         self._init_data(fields)
         self.widgets = dict()
         self.show('transaction')
+        with open(log, 'r') as f:
+            for line in f.readlines():
+                #print(cur.mogrify(line))
+                #input()
+                cur.execute(line)
 
+        self.log = self.open(log)
+        
     def iter_focus_paths(self):
         initial = [2,0,0,0]
         container = self.original_widget.original_widget.original_widget
@@ -613,10 +621,21 @@ class GroceryTransactionEditor(urwid.WidgetPlaceholder):
         top.keypress = keypress
         return urwid.AttrMap(top, 'banner')
     
+    def _open(self, log):
+        with open(log, 'a') as f:
+            yield f
+    
+    def open(self, log):
+        self._to_close = self._open(log)
+        return next(self._to_close)
+    
+    def close(self):
+        if self._to_close is not None:
+            self._to_close.close()
+            self._to_close = None
+        
     def save(self):
-        fmt = '%Y-%m-%dT%H%M'
         ts = self.data['ts']
-        ts_log = time.strptime(ts, '%Y-%m-%d %H:%M')
         store = self.data['store']
         description = self.data['description']
         quantity = self.data['quantity']
@@ -624,13 +643,12 @@ class GroceryTransactionEditor(urwid.WidgetPlaceholder):
         price = self.data['price']
         product = self.data['product']
         organic = self.data['organic'] if self.data['organic'] else 'false'
-        log = f"{time.strftime(fmt, ts_log)}-{store}.txn"
-        with open(log, 'a') as f:
-            f.write(
-                f"CALL insert_transaction('{ts}', $store${store}$store$, "
-                f"$descr${description}$descr$, {quantity}, $unit${unit}$unit$, "
-                f"{price}, $produ${product}$produ$, {organic});\n"
-            )
+        statement = \
+            f"CALL insert_transaction('{ts}', $store${store}$store$, " \
+            f"$descr${description}$descr$, {quantity}, $unit${unit}$unit$, " \
+            f"{price}, $produ${product}$produ$, {organic});\n"
+        self.log.write(statement)
+        cur.execute(statement)
 
     def clear(self):
         for k in self.data:
@@ -731,7 +749,9 @@ class GroceryTransactionEditor(urwid.WidgetPlaceholder):
 #screen.set_terminal_properties(colors=256, has_underline=True)
 #screen.register_palette(palette)
 
-app = GroceryTransactionEditor(cols)
+args = sys.argv
+log = args[1]
+app = GroceryTransactionEditor(log, cols)
 loop = urwid.MainLoop(app, palette, unhandled_input=show_or_exit)
 loop.run()
 

+ 2 - 8
txn_view.py

@@ -34,20 +34,14 @@ def get_where(date, store, full_name=False, exact_time=False):
             SQL('='),
             Literal(store)
         ]))
-    tz_hack_local = SQL("::timestamp with time zone")
-    tz_hack = SQL("||'+00'::text")
     where.append(
-        SQL("({ts}{tz_hack}){tz_hack_local} BETWEEN {date}{tz_hack_local}::date AND {date}{tz_hack_local}::date + {interval}::interval").format(
+        SQL("{ts} at time zone 'utc' BETWEEN {date}::date AND {date}::date + {interval}::interval").format(
             ts=Identifier('ts'),
             date=Literal(str(date)),
             interval=Literal('23 hours 59 minutes 59 seconds'),
-            tz_hack=tz_hack,
-            tz_hack_local=tz_hack_local,
-        ) if not exact_time else SQL("({ts}{tz_hack}){tz_hack_local} = {date}{tz_hack_local}").format(
+        ) if not exact_time else SQL("{ts} at time zone 'utc' = {date}").format(
             ts=Identifier('ts'),
             date=Literal(str(date)),
-            tz_hack=tz_hack,
-            tz_hack_local=tz_hack_local,
         )
     )
     return SQL('').join([