Allow setting the "exclusive" sequence of grid filters

i.e. let caller specify that any not included, should be omitted
This commit is contained in:
Lance Edgar 2021-09-19 18:36:25 -05:00
parent 2188e91fae
commit d295cf04af

View file

@ -28,6 +28,7 @@ from __future__ import unicode_literals, absolute_import
import datetime
import warnings
import logging
import six
from six.moves import urllib
@ -49,6 +50,9 @@ from tailbone.db import Session
from tailbone.util import raw_datetime
log = logging.getLogger(__name__)
class FieldList(list):
"""
Convenience wrapper for a field list.
@ -1047,7 +1051,7 @@ class Grid(object):
return render(template, context)
def set_filters_sequence(self, filters):
def set_filters_sequence(self, filters, only=False):
"""
Explicitly set the sequence for grid filters, using the sequence
provided. If the grid currently has more filters than are mentioned in
@ -1055,12 +1059,21 @@ class Grid(object):
tacked on at the end.
:param filters: Sequence of filter keys, i.e. field names.
:param only: If true, then *only* those filters specified will
be kept, and all others discarded. If false then any
filters not specified will still be tacked onto the end, in
alphabetical order.
"""
new_filters = gridfilters.GridFilterSet()
for field in filters:
new_filters[field] = self.filters.pop(field)
for field in self.filters:
new_filters[field] = self.filters[field]
if field in self.filters:
new_filters[field] = self.filters.pop(field)
else:
log.warning("field '%s' is not in current filter set", field)
if not only:
for field in sorted(self.filters):
new_filters[field] = self.filters[field]
self.filters = new_filters
def get_filters_sequence(self):