|
@@ -1,62 +1,75 @@
|
|
#
|
|
#
|
|
-# Copyright (c) Daniel Sheffield 2022 - 2022
|
|
|
|
|
|
+# Copyright (c) Daniel Sheffield 2022 - 2023
|
|
#
|
|
#
|
|
# All rights reserved
|
|
# All rights reserved
|
|
#
|
|
#
|
|
# THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
|
|
# THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
|
|
from collections import OrderedDict
|
|
from collections import OrderedDict
|
|
|
|
|
|
-import urwid
|
|
|
|
-from app.activities import ActivityManager
|
|
|
|
-from app.db_utils import NON_IDENTIFIER_COLUMNS, QueryManager
|
|
|
|
-
|
|
|
|
|
|
+from urwid import (
|
|
|
|
+ connect_signal,
|
|
|
|
+ AttrMap,
|
|
|
|
+ Button,
|
|
|
|
+ Divider,
|
|
|
|
+ ListBox,
|
|
|
|
+ LineBox,
|
|
|
|
+ Overlay,
|
|
|
|
+ Padding,
|
|
|
|
+ Pile,
|
|
|
|
+ SimpleListWalker,
|
|
|
|
+ Text,
|
|
|
|
+)
|
|
|
|
+from . import ActivityManager
|
|
|
|
+from ..db_utils import NON_IDENTIFIER_COLUMNS, QueryManager, get_insert_product_statement
|
|
from ..widgets import AutoCompleteEdit, AutoCompletePopUp
|
|
from ..widgets import AutoCompleteEdit, AutoCompletePopUp
|
|
|
|
|
|
-
|
|
|
|
-class NewProduct(urwid.Overlay):
|
|
|
|
|
|
+class NewProduct(Overlay):
|
|
|
|
|
|
def __init__(self,
|
|
def __init__(self,
|
|
activity_manager: ActivityManager,
|
|
activity_manager: ActivityManager,
|
|
query_manager: QueryManager,
|
|
query_manager: QueryManager,
|
|
under, name, data,
|
|
under, name, data,
|
|
- autocomplete_cb, change_cb, apply_cb, esc_cb,
|
|
|
|
|
|
+ change_cb,
|
|
):
|
|
):
|
|
- self.esc_cb = esc_cb
|
|
|
|
self.under = under
|
|
self.under = under
|
|
|
|
+ self.activity_manager = activity_manager
|
|
self.query_manager = query_manager
|
|
self.query_manager = query_manager
|
|
self.name = name
|
|
self.name = name
|
|
|
|
|
|
- title = urwid.Text('Enter Product Info', align='center')
|
|
|
|
|
|
+ title = Text('Enter Product Info', align='center')
|
|
self.fields = OrderedDict()
|
|
self.fields = OrderedDict()
|
|
|
|
+ self.autocomplete_callback = lambda widget, options: len(options) and widget._emit('open', options)
|
|
for f in ('product', 'category', 'group'):
|
|
for f in ('product', 'category', 'group'):
|
|
w = AutoCompleteEdit(('bg', f))
|
|
w = AutoCompleteEdit(('bg', f))
|
|
self.fields[f] = w
|
|
self.fields[f] = w
|
|
w.set_edit_text(data[f])
|
|
w.set_edit_text(data[f])
|
|
- urwid.connect_signal(w, 'change', lambda w, v: change_cb(w.name, v))
|
|
|
|
- urwid.connect_signal(w, 'apply', lambda w, name: autocomplete_cb(w, name, self.data))
|
|
|
|
-
|
|
|
|
- ok = urwid.Button('Done', on_press=lambda _: apply_cb(**self.data))
|
|
|
|
-
|
|
|
|
- body = urwid.AttrMap(urwid.ListBox(urwid.SimpleListWalker([
|
|
|
|
- urwid.Padding(
|
|
|
|
- urwid.Pile([
|
|
|
|
- #urwid.Divider(),
|
|
|
|
- urwid.AttrMap(title, 'banner'),
|
|
|
|
- #urwid.Divider(),
|
|
|
|
- *[urwid.AttrMap(urwid.LineBox(urwid.AttrMap(
|
|
|
|
|
|
+ connect_signal(w, 'change', lambda w, v: change_cb(w.name, v))
|
|
|
|
+ connect_signal(w, 'apply', lambda w, name: self.autocomplete_callback(
|
|
|
|
+ w, query_manager.unique_suggestions(name, **self.data)
|
|
|
|
+ ))
|
|
|
|
+
|
|
|
|
+ ok = Button('Done', on_press=lambda _: self.insert_new_product())
|
|
|
|
+
|
|
|
|
+ body = AttrMap(ListBox(SimpleListWalker([
|
|
|
|
+ Padding(
|
|
|
|
+ Pile([
|
|
|
|
+ #Divider(),
|
|
|
|
+ AttrMap(title, 'banner'),
|
|
|
|
+ #Divider(),
|
|
|
|
+ *[AttrMap(LineBox(AttrMap(
|
|
AutoCompletePopUp(
|
|
AutoCompletePopUp(
|
|
v,
|
|
v,
|
|
self.apply_choice,
|
|
self.apply_choice,
|
|
lambda: activity_manager.show(self.update())
|
|
lambda: activity_manager.show(self.update())
|
|
),'streak'), title=k.title(), title_align='left'), 'bg'
|
|
),'streak'), title=k.title(), title_align='left'), 'bg'
|
|
) for k,v in self.fields.items()],
|
|
) for k,v in self.fields.items()],
|
|
- urwid.Divider(),
|
|
|
|
- urwid.AttrMap(ok, 'bg'),
|
|
|
|
|
|
+ Divider(),
|
|
|
|
+ AttrMap(ok, 'bg'),
|
|
], focus_item=2),
|
|
], focus_item=2),
|
|
align='center', left=1, right=1,
|
|
align='center', left=1, right=1,
|
|
)
|
|
)
|
|
])), 'banner')
|
|
])), 'banner')
|
|
- super().__init__(urwid.AttrMap(body, 'bg'), under,
|
|
|
|
|
|
+ super().__init__(AttrMap(body, 'bg'), under,
|
|
align='center', width=('relative', 40),
|
|
align='center', width=('relative', 40),
|
|
valign='middle', height=13,
|
|
valign='middle', height=13,
|
|
min_width=20)
|
|
min_width=20)
|
|
@@ -72,6 +85,16 @@ class NewProduct(urwid.Overlay):
|
|
for k,v in _data.items():
|
|
for k,v in _data.items():
|
|
self.fields[k].set_edit_text(v)
|
|
self.fields[k].set_edit_text(v)
|
|
|
|
|
|
|
|
+ def insert_new_product(self):
|
|
|
|
+ product, category, group = [ self.fields[k].get_edit_text() for k in (
|
|
|
|
+ 'product', 'category', 'group'
|
|
|
|
+ )]
|
|
|
|
+ self.activity_manager.app.log.write(
|
|
|
|
+ f'{get_insert_product_statement(product, category, group)};\n'
|
|
|
|
+ )
|
|
|
|
+ self.query_manager.insert_new_product(product, category, group)
|
|
|
|
+ self.activity_manager.show(self.under.update())
|
|
|
|
+
|
|
def apply_choice(self, name, value):
|
|
def apply_choice(self, name, value):
|
|
self.data = {
|
|
self.data = {
|
|
name: value
|
|
name: value
|
|
@@ -91,7 +114,7 @@ class NewProduct(urwid.Overlay):
|
|
|
|
|
|
def keypress(self, size, key):
|
|
def keypress(self, size, key):
|
|
if key == 'esc':
|
|
if key == 'esc':
|
|
- self.esc_cb()
|
|
|
|
|
|
+ self.activity_manager.show(self.under.update())
|
|
return
|
|
return
|
|
|
|
|
|
if key == 'tab':
|
|
if key == 'tab':
|