|
@@ -1,22 +1,16 @@
|
|
|
#!/usr/bin/python3
|
|
|
#
|
|
|
-# Copyright (c) Daniel Sheffield 2021 - 2022
|
|
|
+# Copyright (c) Daniel Sheffield 2021 - 2023
|
|
|
#
|
|
|
# All rights reserved
|
|
|
#
|
|
|
# THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
|
|
|
import sys
|
|
|
-from sqlite3 import Cursor
|
|
|
-from typing import Union
|
|
|
-
|
|
|
-import urwid
|
|
|
-from urwid import raw_display
|
|
|
+from psycopg import Cursor
|
|
|
+from urwid import raw_display, WidgetPlaceholder, SolidFill, MainLoop
|
|
|
from app.activities import ActivityManager, show_or_exit
|
|
|
-from app.activities.NewProduct import NewProduct
|
|
|
from app.activities.TransactionEditor import TransactionEditor
|
|
|
-from app.db_utils import (QueryManager, display_mapper,
|
|
|
- get_insert_product_statement)
|
|
|
-from app.widgets import AutoCompleteEdit, AutoCompleteFloatEdit
|
|
|
+from app.db_utils import QueryManager, display_mapper
|
|
|
from app.palette import solarized
|
|
|
|
|
|
try:
|
|
@@ -38,58 +32,12 @@ except:
|
|
|
print('Failed to set up db connection. Entering Mock mode')
|
|
|
from mock import *
|
|
|
|
|
|
-def _insert_new_product_callback(activity_manager, query_manager, product, category, group):
|
|
|
- activity_manager.app.log.write(
|
|
|
- '{};\n'.format(get_insert_product_statement(product, category, group)))
|
|
|
- query_manager.insert_new_product(product, category, group)
|
|
|
- activity_manager.show(activity_manager.get('transaction').update())
|
|
|
-
|
|
|
-def _new_product_callback(
|
|
|
- activity_manager: ActivityManager,
|
|
|
- query_manager: QueryManager,
|
|
|
- name: str
|
|
|
-):
|
|
|
- cur = activity_manager.current()
|
|
|
- txn : TransactionEditor = activity_manager.get('transaction')
|
|
|
- activity_manager.show(
|
|
|
- activity_manager.create(NewProduct, 'new_product',
|
|
|
- activity_manager, query_manager,
|
|
|
- cur, name, txn.data,
|
|
|
- lambda w, t, data: _autocomplete_callback(activity_manager, query_manager, w, t, data),
|
|
|
- txn.apply_changes,
|
|
|
- lambda product, category, group: _insert_new_product_callback(
|
|
|
- activity_manager, query_manager, product, category, group),
|
|
|
- lambda: activity_manager.show(cur.update()),
|
|
|
- )
|
|
|
- )
|
|
|
-
|
|
|
-def _autocomplete_callback(
|
|
|
- activity_manager: ActivityManager,
|
|
|
- query_manager: QueryManager,
|
|
|
- widget: Union[AutoCompleteEdit, AutoCompleteFloatEdit],
|
|
|
- name: str, data: dict
|
|
|
-):
|
|
|
- options = query_manager.unique_suggestions(name, **data)
|
|
|
- if len(options) > 0:
|
|
|
- widget._emit('open', options)
|
|
|
- elif len(options) == 0 and activity_manager.current() is not activity_manager.get('new_product'):
|
|
|
- if name in ('product', 'category', 'group'):
|
|
|
- _new_product_callback(activity_manager, query_manager, name)
|
|
|
-
|
|
|
-
|
|
|
-def _save_and_clear_callback(activity_manager):
|
|
|
- txn = activity_manager.get('transaction')
|
|
|
- activity_manager.app.save(txn.data)
|
|
|
- txn.clear()
|
|
|
- txn.focus_on(txn.edit_fields['product'])
|
|
|
- activity_manager.show(txn.update())
|
|
|
-
|
|
|
args = sys.argv
|
|
|
log = args[1]
|
|
|
|
|
|
-class GroceryTransactionEditor(urwid.WidgetPlaceholder):
|
|
|
+class GroceryTransactionEditor(WidgetPlaceholder):
|
|
|
def __init__(self, activity_manager, cur, log):
|
|
|
- super().__init__(urwid.SolidFill(u'/'))
|
|
|
+ super().__init__(SolidFill(u'/'))
|
|
|
self.activity_manager = activity_manager
|
|
|
self.cur = cur
|
|
|
txn: TransactionEditor = self.activity_manager.get('transaction')
|
|
@@ -159,8 +107,6 @@ query_manager = QueryManager(cur, display_mapper)
|
|
|
|
|
|
activity_manager.create(TransactionEditor, 'transaction',
|
|
|
activity_manager, query_manager,
|
|
|
- lambda: _save_and_clear_callback(activity_manager),
|
|
|
- lambda widget, name, data: _autocomplete_callback(activity_manager, query_manager, widget, name, data),
|
|
|
)
|
|
|
|
|
|
app = None
|
|
@@ -176,7 +122,7 @@ palettes = iter_palettes()
|
|
|
try:
|
|
|
app = GroceryTransactionEditor(activity_manager, cur, log)
|
|
|
screen = raw_display.Screen()
|
|
|
- loop = urwid.MainLoop(app, next(palettes), screen=screen,
|
|
|
+ loop = MainLoop(app, next(palettes), screen=screen,
|
|
|
unhandled_input=lambda k: show_or_exit(k, screen=screen, palettes=palettes),
|
|
|
pop_ups=True
|
|
|
)
|