Quellcode durchsuchen

Move NewProduct activity from widgets to activities

Daniel Sheffield vor 3 Jahren
Ursprung
Commit
b381937805
4 geänderte Dateien mit 89 neuen und 77 gelöschten Zeilen
  1. 87 0
      app/activities/NewProduct.py
  2. 1 1
      app/activities/TransactionEditor.py
  3. 0 75
      app/widgets.py
  4. 1 1
      grocery_transactions.py

+ 87 - 0
app/activities/NewProduct.py

@@ -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)
+

+ 1 - 1
app/activities/TransactionEditor.py

@@ -121,7 +121,7 @@ class TransactionEditor(FocusWidget):
             'product': urwid.LineBox(urwid.Columns([
                 urwid.AttrMap(self.edit_fields['product'],'streak'),
                 self.organic_checkbox
-            ], dividechars=1), title='Product', title_align='left')
+            ], dividechars=2), title='Product', title_align='left')
         })
         txn_view = urwid.Text('')
         self.text_fields.update({'dbview': txn_view})

+ 0 - 75
app/widgets.py

@@ -229,78 +229,3 @@ class SuggestionPopup(urwid.Overlay):
             return
 
         return super().keypress(size, key)
-
-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)

+ 1 - 1
grocery_transactions.py

@@ -15,7 +15,6 @@ from app.db_utils import (
     get_insert_product_statement
 )
 from app.widgets import (
-    NewProduct,
     SuggestionPopup,
 )
 from app.activities import (
@@ -23,6 +22,7 @@ from app.activities import (
     ActivityManager,
 )  
 from app.activities.TransactionEditor import TransactionEditor
+from app.activities.NewProduct import NewProduct
 
 try:
     from db_credentials import HOST, PASSWORD