|
@@ -15,7 +15,7 @@ from bottle import (
|
|
run,
|
|
run,
|
|
response,
|
|
response,
|
|
abort,
|
|
abort,
|
|
- DictProperty,
|
|
|
|
|
|
+ FormsDict,
|
|
redirect,
|
|
redirect,
|
|
template,
|
|
template,
|
|
HTTPError,
|
|
HTTPError,
|
|
@@ -80,11 +80,11 @@ ORDER BY "Product", "Category", "Group"
|
|
""").format(having=having) if having else SQL('')
|
|
""").format(having=having) if having else SQL('')
|
|
])
|
|
])
|
|
|
|
|
|
-def get_filter(query: DictProperty, allow: Iterable[str] = None):
|
|
|
|
|
|
+def get_filter(query: FormsDict, allow: Iterable[str] = None):
|
|
return {
|
|
return {
|
|
- k: get_include_exclude(
|
|
|
|
- (query[k] or 'kg' if k == 'unit' else query[k]) if k in query else None
|
|
|
|
- ) for k in sorted(set(query.keys()) | set(allow)) if allow is None or k in allow
|
|
|
|
|
|
+ k: list(map(set, get_include_exclude(
|
|
|
|
+ (query[k] or 'kg' if k == 'unit' else query.getall(k)) if k in query else None
|
|
|
|
+ ))) for k in sorted(set(query.keys()) | set(allow)) if allow is None or k in allow
|
|
}
|
|
}
|
|
|
|
|
|
def get_query_param(include, exclude):
|
|
def get_query_param(include, exclude):
|
|
@@ -98,7 +98,7 @@ def get_query(**params):
|
|
urlencode([ (k, get_query_param(*params[k])) for k in sorted(params) ])
|
|
urlencode([ (k, get_query_param(*params[k])) for k in sorted(params) ])
|
|
])
|
|
])
|
|
|
|
|
|
-def normalize_query(query: DictProperty, allow: Iterable[str] = None):
|
|
|
|
|
|
+def normalize_query(query: FormsDict, allow: Iterable[str] = None):
|
|
return get_query(**get_filter(query, allow=allow))
|
|
return get_query(**get_filter(query, allow=allow))
|
|
|
|
|
|
def get_form(action, method, filter_data, data):
|
|
def get_form(action, method, filter_data, data):
|
|
@@ -113,14 +113,10 @@ def get_form(action, method, filter_data, data):
|
|
]).size().reset_index()[[
|
|
]).size().reset_index()[[
|
|
k for k in keys
|
|
k for k in keys
|
|
]]
|
|
]]
|
|
- tags = set(chain(*data[in_chart]['tags']))
|
|
|
|
return template(
|
|
return template(
|
|
'app/rest/form',
|
|
'app/rest/form',
|
|
action=action,
|
|
action=action,
|
|
method=method,
|
|
method=method,
|
|
- unit=get_query_param(*filter_data['unit']),
|
|
|
|
- pattern='|'.join(ALL_UNITS),
|
|
|
|
- units=', '.join(ALL_UNITS),
|
|
|
|
header=[
|
|
header=[
|
|
template(
|
|
template(
|
|
'app/rest/filter-heading',
|
|
'app/rest/filter-heading',
|
|
@@ -128,24 +124,59 @@ def get_form(action, method, filter_data, data):
|
|
first=(idx == 0),
|
|
first=(idx == 0),
|
|
) for idx, k in enumerate(keys)
|
|
) for idx, k in enumerate(keys)
|
|
],
|
|
],
|
|
- items=[ template(
|
|
|
|
|
|
+ _include=[ template(
|
|
'app/rest/filter-item',
|
|
'app/rest/filter-item',
|
|
|
|
+ label="+",
|
|
|
|
+ hint="Include",
|
|
|
|
+ id=f"{k}-include",
|
|
fname=k,
|
|
fname=k,
|
|
- fvalue=get_query_param(*filter_data[k]),
|
|
|
|
- size=36,
|
|
|
|
|
|
+ options=sorted(map(lambda x: {
|
|
|
|
+ "selected": x[0],
|
|
|
|
+ "value": x[1],
|
|
|
|
+ }, chain(
|
|
|
|
+ map(lambda x: (True, x), filter_data[k][0]),
|
|
|
|
+ map(lambda x: (False, x), set([
|
|
|
|
+ x[k] for _, x in group.iterrows()
|
|
|
|
+ ]) - filter_data[k][0])
|
|
|
|
+ )), key=lambda x: x["display"] if "display" in x else x["value"]), # include
|
|
|
|
+ ) for k in keys ],
|
|
|
|
+ _exclude=[ template(
|
|
|
|
+ 'app/rest/filter-item',
|
|
|
|
+ label="-",
|
|
|
|
+ hint="Exclude",
|
|
|
|
+ id=f"{k}-exclude",
|
|
|
|
+ fname=k,
|
|
|
|
+ options=sorted(map(lambda x: {
|
|
|
|
+ "selected": x[0],
|
|
|
|
+ "value": f"!{x[1]}",
|
|
|
|
+ "display": x[1]
|
|
|
|
+ }, chain(
|
|
|
|
+ map(lambda x: (True, x), filter_data[k][1]),
|
|
|
|
+ map(lambda x: (False, x), set([
|
|
|
|
+ x[k] for _, x in group.iterrows()
|
|
|
|
+ ]) - filter_data[k][1])
|
|
|
|
+ )), key=lambda x: x["display"] if "display" in x else x["value"]), # exclude
|
|
) for k in keys ],
|
|
) for k in keys ],
|
|
data=[ template(
|
|
data=[ template(
|
|
'app/rest/data-item',
|
|
'app/rest/data-item',
|
|
product=row['product'],
|
|
product=row['product'],
|
|
category=row['category'],
|
|
category=row['category'],
|
|
group=row['group'],
|
|
group=row['group'],
|
|
- size=36,
|
|
|
|
) for _, row in group.iterrows() ],
|
|
) for _, row in group.iterrows() ],
|
|
- tag=get_query_param(*filter_data['tag']),
|
|
|
|
- tags=[
|
|
|
|
- template('app/rest/tag-item', tag=x) for x in tags
|
|
|
|
- ],
|
|
|
|
- size=36,
|
|
|
|
|
|
+ tags=sorted(map(lambda x: {
|
|
|
|
+ "selected": x[0],
|
|
|
|
+ "value": x[1],
|
|
|
|
+ },chain(
|
|
|
|
+ map(lambda x: (True, x), filter_data['tag'][0]),
|
|
|
|
+ map(lambda x: (False, x), set(chain(*data[in_chart]['tags'])) - filter_data['tag'][0])
|
|
|
|
+ )), key=lambda x: x["display"] if "display" in x else x["value"]), # include,
|
|
|
|
+ units=sorted(map(lambda x: {
|
|
|
|
+ "selected": x[0],
|
|
|
|
+ "value": x[1],
|
|
|
|
+ },chain(
|
|
|
|
+ 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"]), # include
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|