Преглед на файлове

use composition over inheritance in NewProduct widget, and fix autofill by excluding product field when creating a new product for an existing group

Daniel Sheffield преди 1 година
родител
ревизия
9022096185
променени са 3 файла, в които са добавени 23 реда и са изтрити 22 реда
  1. 20 17
      app/activities/NewProduct.py
  2. 1 3
      app/activities/TransactionEditor.py
  3. 2 2
      app/data/TransactionView.py

+ 20 - 17
app/activities/NewProduct.py

@@ -18,18 +18,20 @@ from urwid import (
     Pile,
     SimpleListWalker,
     Text,
+    WidgetPlaceholder,
+    Widget,
 )
 from . import ActivityManager
 from ..data.QueryManager import NON_IDENTIFIER_COLUMNS, QueryManager, get_insert_product_statement
 from ..widgets import AutoCompleteEdit, AutoCompletePopUp
 
-class NewProduct(Overlay):
+class NewProduct(WidgetPlaceholder):
 
     def __init__(self,
         activity_manager: ActivityManager,
         query_manager: QueryManager,
-        under, name, data,
-        change_cb,
+        under: Widget,
+        name,
     ):
         self.under = under
         self.activity_manager = activity_manager
@@ -38,14 +40,14 @@ class NewProduct(Overlay):
 
         title = Text('Enter Product Info', align='center')
         self.fields = OrderedDict()
-        self.autocomplete_callback = lambda widget, options: len(options) and widget._emit('open', options)
+        self.autocomplete_cb = lambda widget, options: len(options) and widget._emit('open', options)
         for f in ('product', 'category', 'group'):
             w = AutoCompleteEdit(('bg', f))
             self.fields[f] = w
-            w.set_edit_text(data[f])
-            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)
+            w.set_edit_text(under.data[f])
+            connect_signal(w, 'change', lambda w, v: under.apply_changes(w.name, v))
+            connect_signal(w, 'apply', lambda _, name: self.autocomplete_cb(
+                _, query_manager.unique_suggestions(name, **self.data)
             ))
 
         ok = Button('Done', on_press=lambda _: self.insert_new_product())
@@ -53,9 +55,7 @@ class NewProduct(Overlay):
         body = AttrMap(ListBox(SimpleListWalker([
             Padding(
                 Pile([
-                    #Divider(),
                     AttrMap(title, 'banner'),
-                    #Divider(),
                     *[AttrMap(LineBox(AttrMap(
                         AutoCompletePopUp(
                             v,
@@ -69,7 +69,10 @@ class NewProduct(Overlay):
                 align='center', left=1, right=1,
             )
         ])), 'banner')
-        super().__init__(AttrMap(body, 'bg'), under,
+        super().__init__(under)
+        self.original_widget = Overlay(
+            AttrMap(body, 'bg'),
+            self.original_widget,
             align='center', width=('relative', 40),
             valign='middle', height=13,
             min_width=20)
@@ -103,7 +106,7 @@ class NewProduct(Overlay):
             if k == name or v:
                 continue
             options = self.query_manager.unique_suggestions(
-                k, exclude=[self.name, *NON_IDENTIFIER_COLUMNS], **self.data)
+                k, exclude=['product', *NON_IDENTIFIER_COLUMNS], **self.data)
             if len(options) == 1 and k != 'ts':
                 self.data = { k: list(options)[0] }
 
@@ -115,13 +118,13 @@ class NewProduct(Overlay):
     def keypress(self, size, key):
         if key == 'esc':
             self.activity_manager.show(self.under.update())
-            return
+            return None
 
         if key == 'tab':
-            return super().keypress(size, 'down')
-        elif key == 'shift tab':
-            return super().keypress(size, 'up')
+            return self.original_widget.keypress(size, 'down')
+        if key == 'shift tab':
+            return self.original_widget.keypress(size, 'up')
 
 
-        return super().keypress(size, key)
+        return self.original_widget.keypress(size, key)
 

+ 1 - 3
app/activities/TransactionEditor.py

@@ -326,12 +326,10 @@ class TransactionEditor(FocusWidget):
 
     def new_product_callback(self, name):
         cur = self.activity_manager.current()
-        txn = self.activity_manager.get('transaction')
         new_product = self.activity_manager.create(
             NewProduct, 'new_product',
             self.activity_manager, self.query_manager,
-            cur, name, txn.data,
-            txn.apply_changes,
+            cur, name,
         )
         self.activity_manager.show(new_product)
 

+ 2 - 2
app/data/TransactionView.py

@@ -40,14 +40,14 @@ def get_table_statement(alias):
         alias=Identifier(alias),
     )
 
-def get_transactions_statement(name, **kwargs):
+def get_transactions_statement(name, exclude=NON_IDENTIFIER_COLUMNS, **kwargs):
     where = []
     for k,v in kwargs.items():
         where.append(SQL('{key} {condition} {value}').format(
             key=Identifier(ALIAS_TO_TABLE[k], 'name'),
             condition=SQL('ILIKE' if k == name else '='),
             value=Literal(f'%{v}%' if k == name else v),
-        ) if k in ALIAS_TO_TABLE and (k not in NON_IDENTIFIER_COLUMNS or k == name) and v else SQL('TRUE'))
+        ) if k in ALIAS_TO_TABLE and (k not in exclude or k == name) and v else SQL('TRUE'))
     statement = SQL('\n').join([
         get_select(dict([('tags', Identifier('tags','name')), *[
             (k,v) for k,v in SELECT.items() if k != 'tags'