Browse Source

fix sort order being ignored

Daniel Sheffield 2 năm trước cách đây
mục cha
commit
34ef544ded
2 tập tin đã thay đổi với 9 bổ sung3 xóa
  1. 1 1
      app/db_utils.py
  2. 8 2
      app/price_view.py

+ 1 - 1
app/db_utils.py

@@ -130,7 +130,7 @@ class QueryManager(object):
         self.cursor = cursor
 
     def get_historic_prices_data(self, unit, sort=None, product=None, category=None, group=None, organic=None, limit=None):
-        statement = get_historic_prices_statement(unit, sort=None, product=product, category=category, group=group, organic=organic, limit=limit)
+        statement = get_historic_prices_statement(unit, sort=sort, product=product, category=category, group=group, organic=organic, limit=limit)
         #print(self.cursor.mogrify(statement).decode('utf-8'))
         #input()
         return get_data(self.cursor, statement)

+ 8 - 2
app/price_view.py

@@ -62,6 +62,13 @@ def get_where(unit, product=None, category=None, group=None, organic=None, limit
 def get_historic_prices_statement(unit, sort=None, product=None, category=None, group=None, organic=None, limit='90 days'):
     partition = f"(PARTITION BY {'organic,' if organic is not None else ''} product_id, unit_id)"
     organic_sort = f"{'organic,' if organic is not None else ''}"
+    sort_sql = SQL('').join([
+        SQL('{sort} {direction},').format(
+            sort=Identifier(f'{sort}'),
+            direction = SQL('DESC' if sort == 'ts' else 'ASC')
+        ),
+    ]) if sort is not None else SQL('')
+
     _with = SQL("""WITH conv AS (
     SELECT
         _1._from, _1._to, factor FROM conversions AS _1
@@ -137,8 +144,7 @@ def get_historic_prices_statement(unit, sort=None, product=None, category=None,
         ]),
         get_where(unit, product=product, category=category, group=group, organic=organic, limit=limit),
         SQL('ORDER BY {organic_sort} {sort} code, product, category, "group", "$/unit" ASC, ts DESC').format(
-            sort=SQL(f'{Identifier(sort)} {direction},' if sort is not None else ''),
-            direction=SQL('DESC' if sort == 'ts' else 'ASC'),
+            sort=sort_sql,
             organic_sort=SQL(organic_sort),
         ),
     ])