|
@@ -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()
|
|
|
|