Prechádzať zdrojové kódy

do multi row support properly

Daniel Sheffield 2 rokov pred
rodič
commit
243775b6d9
2 zmenil súbory, kde vykonal 35 pridanie a 14 odobranie
  1. 34 9
      app/activities/RecipeEditor.py
  2. 1 5
      app/db_utils.py

+ 34 - 9
app/activities/RecipeEditor.py

@@ -35,6 +35,18 @@ from ..db_utils import QueryManager
 from . import ActivityManager, show_or_exit
 from .Rating import Rating
 
+def to_numbered_field(x):
+    if len(x[0].split('#', 1)) > 1:
+        name, idx = x[0].split('#', 1)
+        idx = int(idx)
+    else:
+        name, idx = x[0], 0
+
+    return (name, int(idx)), x[1]
+
+def in_same_row(row):
+    return lambda x: x[0][1] == int(row)
+
 class RecipeEditor(FocusWidget):
 
     def keypress(self, size, key):
@@ -53,12 +65,20 @@ class RecipeEditor(FocusWidget):
 
     def apply_choice(self, name, value):
         self.apply_changes(name, value)
-        for k,v in self.data.items():
-            if k == name or v:
+        if len(name.split('#', 1)) > 1:
+            _, row = name.split('#', 1)
+        data = dict(filter(
+            in_same_row(row),
+            map(to_numbered_field, self.data.items())
+        ))
+        
+        for k,v in data.items():
+            if f'{k[0]}#{k[1]}' == name or v:
                 continue
-            options = self.query_manager.unique_suggestions(k, **self.data)
+            _data = dict(map(lambda x: (x[0][0], x[1]), data.items()))
+            options = self.query_manager.unique_suggestions(k[0], **_data)
             if len(options) == 1:
-                self.apply_changes(k, list(options)[0])
+                self.apply_changes(f'{k[0]}#{k[1]}', list(options)[0])
 
     def apply_changes(self, name, value):
         self.data = {
@@ -111,7 +131,7 @@ class RecipeEditor(FocusWidget):
         }
         self.ingredients = [
           (AutoCompleteEdit(('bg', 'product#0')), AutoCompleteEdit(('bg', 'unit#0'))),
-          #(AutoCompleteEdit(('bg', 'product#1')), AutoCompleteEdit(('bg', 'unit#1'))),
+          (AutoCompleteEdit(('bg', 'product#1')), AutoCompleteEdit(('bg', 'unit#1'))),
         ]
         self.organic = NoTabCheckBox(('bg', "Organic"), state='mixed')
         self.instructions = Edit(f'', multiline=True, allow_tab=True)
@@ -128,12 +148,17 @@ class RecipeEditor(FocusWidget):
         for idx, widget in enumerate(self.ingredients):
             connect_signal(widget[0], 'postchange', lambda _,v: self.update())
             connect_signal(widget[0], 'apply', lambda w, name: autocomplete_cb(
-                w, name, self.data)
-            )
+                w, name.split('#', 1)[0], dict(map(lambda x: (x[0][0], x[1]), filter(
+                    in_same_row(name.split('#', 1)[1]), map(to_numbered_field, self.data.items())
+                )))
+            ))
             connect_signal(widget[1], 'postchange', lambda _,v: self.update())
             connect_signal(widget[1], 'apply', lambda w, name: autocomplete_cb(
-                w, name, self.data)
-            )
+                w, name.split('#', 1)[0], dict(map(lambda x: (x[0][0], x[1]), filter(
+                    in_same_row(name.split('#', 1)[1]),
+                    map(to_numbered_field, self.data.items())
+                )))
+            ))
 
         connect_signal(self.buttons['clear'], 'click', lambda x: self.clear().update())
         connect_signal(self.buttons['exit'], 'click', lambda x: show_or_exit('esc'))

+ 1 - 5
app/db_utils.py

@@ -71,7 +71,7 @@ def get_session_transactions(cursor, statement, display):
 def record_matches(record, strict=None, **kwargs):
     strict = [ x.lower() for x in (strict or []) ]
     for key, query, candidate in (
-        (k.lower(), f"{v}".lower(), f"{record[k.split('#',1)[0]]}".lower()) for k, v in kwargs.items()
+        (k.lower(), f"{v}".lower(), f"{record[k]}".lower()) for k, v in kwargs.items()
     ):
         if not query:
             continue
@@ -86,8 +86,6 @@ def record_matches(record, strict=None, **kwargs):
     return True
 
 def unique_suggestions(cur, statement, name, display, exclude=NON_IDENTIFIER_COLUMNS, **kwargs):
-    if len(name.split('#', 1)) > 1:
-        name, _ = name.split('#', 1)
     exclude = filter(
         lambda x: x != name or name == 'ts',
         exclude,
@@ -111,8 +109,6 @@ def unique_suggestions(cur, statement, name, display, exclude=NON_IDENTIFIER_COL
     return ret
 
 def suggestions(cur, statement, name, display, exclude=NON_IDENTIFIER_COLUMNS, **kwargs):
-    if len(name.split('#', 1)) > 1:
-        name, _ = name.split('#', 1)
     exclude = filter(
         lambda x: x != name or name == 'ts',
         exclude,