Bläddra i källkod

use preferred unit

Daniel Sheffield 1 år sedan
förälder
incheckning
7c964c39c0
2 ändrade filer med 25 tillägg och 2 borttagningar
  1. 7 1
      app/activities/PriceCheck.py
  2. 18 1
      app/data/QueryManager.py

+ 7 - 1
app/activities/PriceCheck.py

@@ -56,7 +56,13 @@ class PriceCheck(FocusWidget):
         for k,v in self.data.items():
             if k == name or v:
                 continue
-            options = self.query_manager.unique_suggestions(k, **self.data)
+            options = self.query_manager.unique_suggestions(
+                k, **self.data
+            )
+            if k == 'unit':
+                options = list(filter(lambda x: x, [
+                self.query_manager.get_preferred_unit(self.data['product'])
+            ])) or options
             if len(options) == 1:
                 self.apply_changes(k, list(options)[0])
 

+ 18 - 1
app/data/QueryManager.py

@@ -4,7 +4,11 @@
 # All rights reserved
 #
 # THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
-from sqlite3 import Cursor
+from psycopg import Cursor
+from psycopg.sql import (
+    SQL,
+    Literal,
+)
 import time
 from typing import Any, Callable
 from .TransactionView import (
@@ -144,3 +148,16 @@ class QueryManager(object):
 
     def insert_new_product(self, product, category, group):
         self.cursor.execute(get_insert_product_statement(product, category, group))
+
+    def get_preferred_unit(self, product):
+        self.cursor.execute(SQL("""SELECT
+    units.name AS preferred_unit
+FROM products
+JOIN units ON unit_id = units.id
+WHERE products.name = {product}""").format(product=Literal(product)))
+        res = list(cursor_as_dict(self.cursor))
+        if len(res) == 0:
+            return None
+        assert len(res) == 1
+        return res[0]['preferred_unit']
+