Pārlūkot izejas kodu

add slider to select organic true/false/mixed

Daniel Sheffield 1 gadu atpakaļ
vecāks
revīzija
08a72ef1dc

+ 9 - 2
app/rest/__init__.py

@@ -7,5 +7,12 @@
 from bottle import TEMPLATE_PATH
 
 TEMPLATE_PATH.append("app/rest/templates")
-ALL_UNITS = {'g', 'kg', 'mL', 'L', 'Pieces', 'Bunches', 'Bags'}
-PARAMS = { 'group', 'category', 'product', 'unit', 'tag' }
+ALL_UNITS = { 'g', 'kg', 'mL', 'L', 'Pieces', 'Bunches', 'Bags' }
+PARAMS = { 'group', 'category', 'product', 'unit', 'tag', 'organic' }
+BOOLEAN = {
+    "1": True,
+    True: "1",
+    "0": False,
+    False: "0",
+    None: "0.5",
+}

+ 5 - 4
app/rest/form.py

@@ -8,7 +8,7 @@ from typing import Dict, Tuple
 from bottle import template
 from pandas import DataFrame
 from itertools import chain
-from ..rest import ALL_UNITS
+from . import ALL_UNITS, BOOLEAN
 
 def get_option_groups(
     data: DataFrame, filter_data: Dict[str, Tuple[set, set]],
@@ -67,8 +67,8 @@ def get_option_groups(
             }
 
 
-def get_form(action: str, method: str, filter_data, data: DataFrame):
-    keys = sorted(filter(lambda x: x not in ('unit', 'tag'), filter_data), key=lambda x: {
+def get_form(action: str, method: str, filter_data: Dict[str, Tuple[set, set]], organic: bool, data: DataFrame):
+    keys = sorted(filter(lambda x: x not in ('unit', 'tag', 'organic'), filter_data), key=lambda x: {
         'product': 0,
         'category': 1,
         'group': 2,
@@ -104,5 +104,6 @@ def get_form(action: str, method: str, filter_data, data: DataFrame):
                 map(lambda x: (True, x), filter_data['unit'][0]),
                 map(lambda x: (False, x), ALL_UNITS - filter_data['unit'][0])
             )), key=lambda x: x["display"] if "display" in x else x["value"])
-        }
+        },
+        organic=BOOLEAN[organic],
     )

+ 2 - 2
app/rest/query_to_xml.py

@@ -12,7 +12,7 @@ from ..data.filter import get_filter
 from ..data.util import get_where_include_exclude
 from ..data.QueryManager import get_data
 from .form import get_form
-from . import PARAMS
+from . import BOOLEAN, PARAMS
 
 
 def get_product_rollup_statement(filters) -> SQL:
@@ -44,7 +44,7 @@ def get_inner_query(query: FormsDict) -> SQL:
 def render_form(cur: Cursor, inner: str, query: FormsDict):
     _filter = get_filter(query, allow=PARAMS)
     data = DataFrame(get_data(cur, inner)).dropna()
-    return get_form(request.path.split('/')[-1], 'get', _filter, data)
+    return get_form(request.path.split('/')[-1], 'get', _filter, BOOLEAN.get(query.organic, None), data)
 
 
 def get_xml(cur: Cursor, sql: str):

+ 6 - 2
app/rest/route_decorators.py

@@ -10,14 +10,18 @@ from psycopg import Connection
 from psycopg.connection import TupleRow
 
 from ..data.filter import get_filter, get_query_param
-from . import PARAMS
+from . import BOOLEAN, PARAMS
 from .Cache import Cache
 
 
 def normalize_query(query: FormsDict, allow: Iterable[str] = None) -> str:
     param = get_filter(query, allow=allow)
     return urlencode([
-        (k, get_query_param(*param[k])) for k in sorted(param) if param[k]
+        (
+            k, get_query_param(*param[k])
+        ) if k != 'organic' else (
+            "organic", BOOLEAN[BOOLEAN.get(query.organic, None)]
+        ) for k in sorted(param) if param[k]
     ])
 
 

+ 7 - 1
app/rest/templates/filter-set.tpl

@@ -6,6 +6,12 @@
 %>
 <%
   include('include-exclude', **tags)
-  include('select-one', **units)
+
 %>
+  <div class="pure-u-1-3 pure-u-lg-1-12">
+    <div class="pure-g">
+    <% include('select-one', **units) %>
+    <% include('range-organic') %>
+    </div>
+  </div>
 <div class="pure-u-lg-1-8"></div>

+ 14 - 0
app/rest/templates/range-organic.tpl

@@ -0,0 +1,14 @@
+<div class="pure-u-1">
+  <div class="l-box">
+    <h3>Organic</h3>
+  </div>
+  </div>
+  <label for="organic-state" hidden="true">Organic</label>
+  <div class="pure-g">
+  <div class="pure-u-1-3">No</div>
+  <div class="pure-u-1-3">Any</div>
+  <div class="pure-u-1-3">Yes</div>
+  <div class="pure-u-1">
+    <input type="range" id="organic-state" name="organic" min="0" max="1" step="0.5" value="{{organic}}" />
+  </div>
+</div>

+ 8 - 11
app/rest/templates/select-one.tpl

@@ -1,13 +1,10 @@
-<div class="pure-u-1-3 pure-u-lg-1-12">
-<div class="pure-g">
-  <div class="pure-u-1">
-    <div class="l-box">
-      <h3>{{name.title()}}</h3>
-    </div>
+<div class="pure-u-1">
+  <div class="l-box">
+    <h3>{{name.title()}}</h3>
   </div>
-  <% include('select', id=f"{name}-select-one", name=name,
-     children=[{
-        "options": options
-     }]) %>
 </div>
-</div>
+<% include('select', id=f"{name}-select-one", name=name,
+   children=[{
+      "options": options
+   }])
+%>

+ 3 - 2
app/rest/trend.py

@@ -16,7 +16,7 @@ import seaborn as sns
 from psycopg import Connection
 from psycopg.connection import TupleRow
 from queue import Queue
-from . import ALL_UNITS
+from . import ALL_UNITS, BOOLEAN
 from ..data.QueryManager import (
     display_mapper,
     QueryManager,
@@ -49,6 +49,7 @@ def trend_internal(conn: Connection[TupleRow], path: str, query: DictProperty):
             query_manager = QueryManager(cur, display_mapper)
             fields = { k: query[k] or None for k in query.keys() if k in PARAMS }
             unit = fields['unit'] = fields['unit'] or 'kg' if 'unit' in fields else 'kg'
+            fields['organic'] = BOOLEAN.get(query.organic, None)
             if unit and unit not in ALL_UNITS:
                 yield abort(400, f"Unsupported unit {unit}")
                 return
@@ -117,7 +118,7 @@ def trend_internal(conn: Connection[TupleRow], path: str, query: DictProperty):
             f = StringIO()
             plt.savefig(f, format='svg')
             _filter = get_filter(query, allow=PARAMS)
-            form = get_form(path.split('/')[-1], 'get', _filter, data)
+            form = get_form(path.split('/')[-1], 'get', _filter, BOOLEAN.get(query.organic, None), data)
             
             progress[-1]["status"] = "done"
             yield template("loading", progress=progress)

+ 4 - 7
test/rest/templates/test_select-one.py

@@ -3,13 +3,11 @@ from pytest import mark, raises
 from bottle import template
 
 @mark.parametrize('expected, params', [
-    ("""<div class="pure-u-1-3 pure-u-lg-1-12">
-<div class="pure-g">
-  <div class="pure-u-1">
-    <div class="l-box">
-      <h3>Unit</h3>
-    </div>
+    ("""<div class="pure-u-1">
+  <div class="l-box">
+    <h3>Unit</h3>
   </div>
+</div>
 <div class="pure-u-1">
 
 <select id="unit-select-one" name="unit" size="10"  style="width: calc(100% - 1em); margin: 0 1em 1em">
@@ -23,7 +21,6 @@ from bottle import template
   <option value="kg"  selected="true">kg</option>
   <option value="mL"  >mL</option>
 </select>
-</div></div>
 </div>""", {
     "name": "unit", "options": [{
         "value": "Bags",