|
@@ -0,0 +1,87 @@
|
|
|
+#
|
|
|
+# Copyright (c) Daniel Sheffield 2021
|
|
|
+#
|
|
|
+# All rights reserved
|
|
|
+#
|
|
|
+# THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
|
|
|
+import urwid
|
|
|
+from ..widgets import (
|
|
|
+ AutoCompleteEdit,
|
|
|
+ NON_IDENTIFIER_COLUMNS,
|
|
|
+)
|
|
|
+from collections import OrderedDict
|
|
|
+class NewProduct(urwid.Overlay):
|
|
|
+
|
|
|
+ def __init__(self, query_manager, under, name, data, autocomplete_cb, change_cb, apply_cb, esc_cb):
|
|
|
+ self.esc_cb = esc_cb
|
|
|
+ self.under = under
|
|
|
+ self._data = data
|
|
|
+ self.query_manager = query_manager
|
|
|
+ self.name = name
|
|
|
+
|
|
|
+ title = urwid.Text('Enter Product Info', align='center')
|
|
|
+ self.fields = OrderedDict()
|
|
|
+ for f in ('product', 'category', 'group'):
|
|
|
+ w = AutoCompleteEdit(
|
|
|
+ ('bg', f),
|
|
|
+ apply_change_func=lambda name: autocomplete_cb(name)
|
|
|
+ )
|
|
|
+ self.fields[f] = w
|
|
|
+ w.set_edit_text(data[f])
|
|
|
+ urwid.connect_signal(w, 'change', change_cb(f))
|
|
|
+
|
|
|
+ ok = urwid.Button('Done', on_press=lambda w: apply_cb(**self.data))
|
|
|
+
|
|
|
+ body = urwid.AttrMap(urwid.ListBox(urwid.SimpleListWalker([
|
|
|
+ urwid.Pile([
|
|
|
+ urwid.AttrMap(title, 'banner'),
|
|
|
+ urwid.Divider(),
|
|
|
+ *[
|
|
|
+ urwid.AttrMap(
|
|
|
+ urwid.LineBox(urwid.AttrMap(v,'streak'), title=k.title(), title_align='left'), 'banner'
|
|
|
+ ) for k,v in self.fields.items()
|
|
|
+ ],
|
|
|
+ urwid.AttrMap(ok, 'banner'),
|
|
|
+ ], focus_item=2)
|
|
|
+ ])
|
|
|
+ ), 'banner')
|
|
|
+ super().__init__(urwid.AttrMap(body, 'bg'), under,
|
|
|
+ align='center', width=('relative', 40),
|
|
|
+ valign='middle', height=('relative', 40),
|
|
|
+ min_width=20, min_height=12)
|
|
|
+
|
|
|
+ @property
|
|
|
+ def data(self):
|
|
|
+ return dict([(k,v) for k,v in self._data.items() if k in ('product', 'category', 'group')])
|
|
|
+
|
|
|
+ def _apply_choice(self, name, value):
|
|
|
+ self._data.update({
|
|
|
+ name: value
|
|
|
+ })
|
|
|
+ for k,v in self.data.items():
|
|
|
+ if k == name or v:
|
|
|
+ continue
|
|
|
+ options = self.query_manager.unique_suggestions(k, exclude=[self.name, *NON_IDENTIFIER_COLUMNS], **self.data)
|
|
|
+ if len(options) == 1 and k != 'ts':
|
|
|
+ self._data.update({
|
|
|
+ k: list(options)[0]
|
|
|
+ })
|
|
|
+
|
|
|
+ def apply_choice(self, name):
|
|
|
+ return lambda w,x: self._apply_choice(name, x)
|
|
|
+
|
|
|
+ def update(self):
|
|
|
+ for k, v in self.data.items():
|
|
|
+ self.fields[k].set_edit_text(v)
|
|
|
+ return self
|
|
|
+
|
|
|
+ def keypress(self, size, key):
|
|
|
+ if key == 'esc':
|
|
|
+ self.esc_cb()
|
|
|
+ return
|
|
|
+
|
|
|
+ if key == 'tab':
|
|
|
+ return
|
|
|
+
|
|
|
+ return super().keypress(size, key)
|
|
|
+
|