Commit c424c3fe authored by bryhoyt's avatar bryhoyt

Parse optional named filters from filter url

Old format fltN_M still works, but the new format flt_name_operation coexists side-by-side.
parent 749161e7
...@@ -958,29 +958,36 @@ class BaseModelView(BaseView, ActionsMixin): ...@@ -958,29 +958,36 @@ class BaseModelView(BaseView, ActionsMixin):
sort_desc = request.args.get('desc', None, type=int) sort_desc = request.args.get('desc', None, type=int)
search = request.args.get('search', None) search = request.args.get('search', None)
filter_idx_by_label = dict((flt.query_label(), i) for i, flt in enumerate(self._filters))
# Gather filters # Gather filters
if self._filters: if self._filters:
sfilters = [] sfilters = []
for n in request.args: for n in request.args:
if n.startswith('flt'): if not n.startswith('flt'):
ofs = n.find('_') continue
if ofs == -1:
continue pos, filter_label = n[3:].split('_', 1)
# If pos not specified, just add incrementally to the list.
pos = int(pos) if pos else len(sfilters)
try:
# See if filter is numeric
idx = int(filter_label)
except ValueError:
# If non-numeric, look filter up by name
try: try:
pos = int(n[3:ofs]) idx = filter_idx_by_label[filter_label]
idx = int(n[ofs + 1:]) except KeyError:
except ValueError: # No matching filter name
continue continue
if idx >= 0 and idx < len(self._filters): if 0 <= idx < len(self._filters):
flt = self._filters[idx] flt = self._filters[idx]
value = request.args[n]
value = request.args[n] if flt.validate(value):
sfilters.append((pos, (idx, flt.clean(value))))
if flt.validate(value):
sfilters.append((pos, (idx, flt.clean(value))))
filters = [v[1] for v in sorted(sfilters, key=lambda n: n[0])] filters = [v[1] for v in sorted(sfilters, key=lambda n: n[0])]
else: else:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment