Add move_before() convenience method for GridFilterSet

to more easily rearrange sort order of grid filters
This commit is contained in:
Lance Edgar 2019-04-02 14:44:59 -05:00
parent bf189bb704
commit 511ba61b1c

View file

@ -770,6 +770,28 @@ class GridFilterSet(OrderedDict):
Collection class for :class:`GridFilter` instances. Collection class for :class:`GridFilter` instances.
""" """
def move_before(self, key, refkey):
"""
Rearrange underlying key sorting, such that the given ``key`` comes
just *before* the given ``refkey``.
"""
# first must work out the new order for all keys
newkeys = []
for k in self.keys():
if k == key:
continue
if k == refkey:
newkeys.append(key)
newkeys.append(refkey)
else:
newkeys.append(k)
# then effectively replace dict contents, using new order
items = dict(self)
self.clear()
for k in newkeys:
self[k] = items[k]
class GridFiltersForm(forms.Form): class GridFiltersForm(forms.Form):
""" """