Răsfoiți Sursa

make tags autocomplete and fix display of NULL tags

Daniel Sheffield 2 ani în urmă
părinte
comite
6d5366653f

+ 0 - 1
app/activities/TransactionEditor.py

@@ -204,7 +204,6 @@ class TransactionEditor(FocusWidget):
         self.components['gutter'][1].contents = list(gutter.contents)
         for idx, widget in enumerate(self._tags):
             connect_signal(widget[0], 'postchange', lambda w,_: self.update())
-            continue
             connect_signal(widget[0], 'apply', lambda w, name: self.autocomplete_callback(
                 w, name, self.autocomplete_options(name, dict(map(
                     to_unnumbered_field,

+ 2 - 9
app/data/QueryManager.py

@@ -11,21 +11,13 @@ from .TransactionView import (
     get_table_statement,
     get_transactions_statement,
     get_session_transactions_statement,
+    NON_IDENTIFIER_COLUMNS,
 )
 from .PriceView import(
     get_historic_prices_statement,
 )
 from dateutil.parser import parse as parse_time
 import pandas as pd
-NON_IDENTIFIER_COLUMNS = [
-    'ts',
-    'store',
-    'quantity',
-    'unit',
-    'price',
-    'organic',
-]
-
 display_map = {
     'ts': lambda x: f"{time.strftime('%Y-%m-%d %H:%M', (x.year, x.month, x.day, x.hour, x.minute, 0, 0, 0, 0))}",
     '%d/%m/%y %_I%P': lambda x: f"{time.strftime('%d/%m/%y %_I%P', (x.year, x.month, x.day, x.hour, x.minute, 0, 0, 0, 0))}",
@@ -100,6 +92,7 @@ def unique_suggestions(cur, statement, name, display, exclude=NON_IDENTIFIER_COL
         'group',
         'unit',
         'store',
+        'tags',
     }
     if len(ret) > 0 or name not in tables:
         return ret

+ 39 - 10
app/data/TransactionView.py

@@ -13,14 +13,24 @@ from psycopg.sql import (
     SQL,
     Literal,
 )
-from .util import get_select, get_from
+from .util import get_select, get_from, get_groupby
 ALIAS_TO_TABLE = {
     'product': 'products',
     'category': 'categories',
     'group': 'groups',
-    #'unit': 'units',
-    #'store': 'stores',
+    'tags': 'tags',
+    'unit': 'units',
+    'store': 'stores',
 }
+NON_IDENTIFIER_COLUMNS = [
+    'ts',
+    'store',
+    'quantity',
+    'unit',
+    'price',
+    'organic',
+    'tags',
+]
 
 def get_table_statement(alias):
     tables = ALIAS_TO_TABLE
@@ -37,9 +47,9 @@ def get_transactions_statement(name, **kwargs):
             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 v else SQL('TRUE'))
+        ) if k in ALIAS_TO_TABLE and (k not in NON_IDENTIFIER_COLUMNS or k == name) and v else SQL('TRUE'))
     statement = SQL('\n').join([
-        get_select(SELECT),
+        get_select(dict([ (k,v) for k,v in SELECT.items() if k != 'tags'])),
         get_from("transactions", JOINS),
         SQL('').join([SQL('WHERE '), SQL('\n    AND ').join(where)]),
     ])
@@ -66,14 +76,32 @@ OVER (
     ('category', Identifier('categories', 'name')),
     ('product', Identifier('products', 'name')),
     ('organic', Identifier('organic')),
-    # need a group by to aggregate
-    #('tags', SQL("""array_agg({tag_name})""").format(
-    #    tag_name=Identifier('tags','name'),
-    #))
-    ('tags', Identifier('tags','name')),
+    ('tags', SQL(
+        """array_agg({tag_name}) FILTER (WHERE {tag_name} IS NOT NULL)"""
+    ).format(
+        tag_name=Identifier('tags','name'),
+    ))
+
+])
 
+GROUPBY = OrderedDict([
+    ('id', Identifier('transactions', 'id')),
+    ('ts', Identifier('transactions', 'ts')),
+    ('store', Identifier('stores', 'name')),
+    ('code', Identifier('stores', 'code')),
+    ('description', Identifier('transactions', 'description')),
+    ('volume', Identifier('quantity')),
+    ('unit', Identifier('units', 'name')),
+    ('price', Identifier('price')),
+    ('quantity', Identifier('quantity')),
+    ('group', Identifier('groups', 'name')),
+    ('category', Identifier('categories', 'name')),
+    ('product', Identifier('products', 'name')),
+    ('organic', Identifier('organic')),
 ])
 
+
+
 JOINS = OrderedDict([
     ('units', ('id', 'unit_id')),
     ('stores', ('id', 'store_id')),
@@ -125,6 +153,7 @@ def get_session_transactions_statement(date, store, full_name=False, exact_time=
             full_name=full_name,
             exact_time=exact_time
         ),
+        get_groupby(GROUPBY),
         get_sort(),
     ])
     return statement

+ 7 - 0
app/data/util.py

@@ -36,3 +36,10 @@ def get_from(
 LEFT JOIN """).format(base=Identifier(base)),
         SQL("""
 LEFT JOIN """).join(joins)])
+
+def get_groupby(alias_to_sql: dict[str, Composable]) -> Composable:
+    groupby = SQL(""",
+    """).join([ v for k, v in alias_to_sql.items() if k != 'tags'])
+    return SQL("""
+    """).join([SQL("GROUP BY"), *groupby])
+