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 datetime
import warnings import warnings
import logging
import six import six
from six.moves import urllib from six.moves import urllib
@ -49,6 +50,9 @@ from tailbone.db import Session
from tailbone.util import raw_datetime from tailbone.util import raw_datetime
log = logging.getLogger(__name__)
class FieldList(list): class FieldList(list):
""" """
Convenience wrapper for a field list. Convenience wrapper for a field list.
@ -1047,7 +1051,7 @@ class Grid(object):
return render(template, context) 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 Explicitly set the sequence for grid filters, using the sequence
provided. If the grid currently has more filters than are mentioned in provided. If the grid currently has more filters than are mentioned in
@ -1055,11 +1059,20 @@ class Grid(object):
tacked on at the end. tacked on at the end.
:param filters: Sequence of filter keys, i.e. field names. :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() new_filters = gridfilters.GridFilterSet()
for field in filters: for field in filters:
if field in self.filters:
new_filters[field] = self.filters.pop(field) new_filters[field] = self.filters.pop(field)
for field in self.filters: 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] new_filters[field] = self.filters[field]
self.filters = new_filters self.filters = new_filters