|
@@ -5,12 +5,11 @@
|
|
|
# All rights reserved
|
|
|
#
|
|
|
# THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
|
|
|
-import pandas as pd
|
|
|
+import time
|
|
|
import itertools
|
|
|
import sys
|
|
|
import urwid
|
|
|
-import time
|
|
|
-from txn_view import get_statement as get_session_transactions_statement
|
|
|
+import pandas as pd
|
|
|
from dateutil.parser import parse as parse_time
|
|
|
from widgets import (
|
|
|
NoTabCheckBox,
|
|
@@ -30,14 +29,19 @@ except:
|
|
|
password = ''
|
|
|
|
|
|
try:
|
|
|
+ from txn_view import (
|
|
|
+ get_transactions_statement,
|
|
|
+ get_session_transactions_statement,
|
|
|
+ )
|
|
|
+ from db_utils import cursor_as_dict
|
|
|
import psycopg2
|
|
|
from psycopg2.sql import SQL
|
|
|
- from db_utils import cursor_as_dict
|
|
|
import os
|
|
|
user = os.getenv('USER')
|
|
|
conn = psycopg2.connect(f"{host} dbname=das user={user} {password}")
|
|
|
cur = conn.cursor()
|
|
|
except:
|
|
|
+ print('Failed to set up db connection. Entering Mock mode')
|
|
|
from mock import *
|
|
|
|
|
|
palette = [
|
|
@@ -87,14 +91,11 @@ display_map = {
|
|
|
}
|
|
|
display = lambda data, name: display_map[name](data) if name in display_map else data
|
|
|
|
|
|
-cur.execute("BEGIN")
|
|
|
-
|
|
|
-def get_session_transactions(date, store):
|
|
|
- statement = get_session_transactions_statement(parse_time(date), store, full_name=True, exact_time=True)
|
|
|
+def get_session_transactions(cursor, statement, date, store):
|
|
|
#print(cur.mogrify(statement).decode("utf-8"))
|
|
|
#input()
|
|
|
- cur.execute(statement)
|
|
|
- df = pd.DataFrame(cursor_as_dict(cur))
|
|
|
+ cursor.execute(statement)
|
|
|
+ df = pd.DataFrame(cursor_as_dict(cursor))
|
|
|
if df.empty:
|
|
|
return ''
|
|
|
return df.drop(labels=[
|
|
@@ -102,16 +103,14 @@ def get_session_transactions(date, store):
|
|
|
], axis=1).to_string(header=[
|
|
|
'Description', 'Volume', 'Unit', 'Price', '$/unit', 'Total',
|
|
|
'Group', 'Category', 'Product', 'Organic',
|
|
|
- ], justify='justify-all', max_colwidth=38, index=False)
|
|
|
+ ], justify='justify-all', max_colwidth=60, index=False)
|
|
|
|
|
|
-def get_transactions_statement():
|
|
|
- return SQL("SELECT * FROM transaction_view")
|
|
|
|
|
|
-def get_transactions(cursor):
|
|
|
- cur.execute(get_transactions_statement())
|
|
|
+def get_transactions(cursor, statement):
|
|
|
+ cursor.execute(statement)
|
|
|
yield from map(lambda x: dict([
|
|
|
(k, display(v, k)) for k,v in x.items()
|
|
|
- ]), cursor_as_dict(cur))
|
|
|
+ ]), cursor_as_dict(cursor))
|
|
|
|
|
|
def record_matches(record, strict=None, **kwargs):
|
|
|
strict = strict or []
|
|
@@ -127,16 +126,16 @@ def record_matches(record, strict=None, **kwargs):
|
|
|
|
|
|
return True
|
|
|
|
|
|
-def unique_suggestions(name, exclude=NON_IDENTIFIER_COLUMNS, **kwargs):
|
|
|
+def unique_suggestions(cur, statement, name, exclude=NON_IDENTIFIER_COLUMNS, **kwargs):
|
|
|
exclude = filter(
|
|
|
lambda x: x != name or name == 'ts',
|
|
|
exclude,
|
|
|
)
|
|
|
[ kwargs.pop(k) for k in exclude if k in kwargs]
|
|
|
- items = suggestions(name, exclude=exclude, **kwargs)
|
|
|
+ items = suggestions(cur, statement, name, exclude=exclude, **kwargs)
|
|
|
return sorted(set(map(lambda x: x[name], items)))
|
|
|
|
|
|
-def suggestions(name, exclude=NON_IDENTIFIER_COLUMNS, **kwargs):
|
|
|
+def suggestions(cur, statement, name, exclude=NON_IDENTIFIER_COLUMNS, **kwargs):
|
|
|
exclude = filter(
|
|
|
lambda x: x != name or name == 'ts',
|
|
|
exclude,
|
|
@@ -144,7 +143,7 @@ def suggestions(name, exclude=NON_IDENTIFIER_COLUMNS, **kwargs):
|
|
|
[ kwargs.pop(k) for k in exclude if k in kwargs]
|
|
|
yield from filter(lambda x: record_matches(
|
|
|
x, strict=[ k for k in kwargs if k != name ], **kwargs
|
|
|
- ), get_transactions(cur))
|
|
|
+ ), get_transactions(cur, statement))
|
|
|
|
|
|
def show_or_exit(key):
|
|
|
if isinstance(key, tuple):
|
|
@@ -164,9 +163,18 @@ def interleave(_list, div):
|
|
|
|
|
|
args = sys.argv
|
|
|
log = args[1]
|
|
|
-app = GroceryTransactionEditor(cur, log,
|
|
|
- cols, grid_layout,
|
|
|
+
|
|
|
+cur.execute("BEGIN")
|
|
|
+
|
|
|
+app = GroceryTransactionEditor(cur,
|
|
|
+ lambda name, **kwargs: unique_suggestions(cur, get_transactions_statement(), name, **kwargs),
|
|
|
+ lambda date, store, *x, **y: get_session_transactions(
|
|
|
+ cur, get_session_transactions_statement(
|
|
|
+ parse_time(date), store, full_name=True, exact_time=True
|
|
|
+ ), date, store),
|
|
|
+ log, cols, grid_layout,
|
|
|
side_pane, bottom_pane)
|
|
|
+
|
|
|
loop = urwid.MainLoop(app, palette, unhandled_input=show_or_exit)
|
|
|
loop.run()
|
|
|
|