fix: allow passing filter factory to Grid.set_filter()
This commit is contained in:
parent
97e5a96cd6
commit
ec18ce7116
2 changed files with 26 additions and 15 deletions
|
|
@ -1404,10 +1404,11 @@ class Grid: # pylint: disable=too-many-instance-attributes,too-many-public-meth
|
||||||
Code usually does not need to call this directly. See also
|
Code usually does not need to call this directly. See also
|
||||||
:meth:`set_filter()`, which calls this method automatically.
|
:meth:`set_filter()`, which calls this method automatically.
|
||||||
|
|
||||||
:param columninfo: Can be either a model property (see below),
|
:param columninfo: Can be either a model property
|
||||||
or a column name.
|
(e.g. ``model.User.username``), or a column name
|
||||||
|
(e.g. ``"username"``).
|
||||||
|
|
||||||
:returns: A :class:`~wuttaweb.grids.filters.GridFilter`
|
:returns: :class:`~wuttaweb.grids.filters.GridFilter`
|
||||||
instance.
|
instance.
|
||||||
"""
|
"""
|
||||||
key = kwargs.pop("key", None)
|
key = kwargs.pop("key", None)
|
||||||
|
|
@ -1445,12 +1446,18 @@ class Grid: # pylint: disable=too-many-instance-attributes,too-many-public-meth
|
||||||
|
|
||||||
:param key: Name of column.
|
:param key: Name of column.
|
||||||
|
|
||||||
:param filterinfo: Can be either a
|
:param filterinfo: Can be either a filter factory, or else a
|
||||||
:class:`~wuttweb.grids.filters.GridFilter` instance, or
|
model property (e.g. ``model.User.username``) or column
|
||||||
else a model property (see below).
|
name (e.g. ``"username"``). If not specified then the
|
||||||
|
``key`` will be used instead.
|
||||||
|
|
||||||
If ``filterinfo`` is a ``GridFilter`` instance, it will be
|
:param \\**kwargs: Additional kwargs to pass along to the
|
||||||
used as-is for the backend filter.
|
filter factory.
|
||||||
|
|
||||||
|
If ``filterinfo`` is a factory, it will be called with the
|
||||||
|
current request, key and kwargs like so::
|
||||||
|
|
||||||
|
filtr = factory(self.request, key, **kwargs)
|
||||||
|
|
||||||
Otherwise :meth:`make_filter()` will be called to obtain the
|
Otherwise :meth:`make_filter()` will be called to obtain the
|
||||||
backend filter. The ``filterinfo`` will be passed along to
|
backend filter. The ``filterinfo`` will be passed along to
|
||||||
|
|
@ -1462,12 +1469,13 @@ class Grid: # pylint: disable=too-many-instance-attributes,too-many-public-meth
|
||||||
filtr = None
|
filtr = None
|
||||||
|
|
||||||
if filterinfo and callable(filterinfo):
|
if filterinfo and callable(filterinfo):
|
||||||
# filtr = filterinfo
|
kwargs.setdefault("label", self.get_filter_label(key))
|
||||||
raise NotImplementedError
|
filtr = filterinfo(self.request, key, **kwargs)
|
||||||
|
|
||||||
kwargs["key"] = key
|
else:
|
||||||
kwargs.setdefault("label", self.get_label(key))
|
kwargs["key"] = key
|
||||||
filtr = self.make_filter(filterinfo or key, **kwargs)
|
kwargs.setdefault("label", self.get_filter_label(key))
|
||||||
|
filtr = self.make_filter(filterinfo or key, **kwargs)
|
||||||
|
|
||||||
self.filters[key] = filtr
|
self.filters[key] = filtr
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1310,10 +1310,13 @@ class TestGrid(WebTestCase):
|
||||||
grid.set_filter("name")
|
grid.set_filter("name")
|
||||||
self.assertIn("name", grid.filters)
|
self.assertIn("name", grid.filters)
|
||||||
|
|
||||||
# explicit is not yet implemented
|
# auto from filter factory
|
||||||
grid = self.make_grid(model_class=model.Setting)
|
grid = self.make_grid(model_class=model.Setting)
|
||||||
self.assertEqual(grid.filters, {})
|
self.assertEqual(grid.filters, {})
|
||||||
self.assertRaises(NotImplementedError, grid.set_filter, "name", lambda q: q)
|
grid.set_filter(
|
||||||
|
"name", StringAlchemyFilter, model_property=model.Setting.name
|
||||||
|
)
|
||||||
|
self.assertIn("name", grid.filters)
|
||||||
|
|
||||||
def test_remove_filter(self):
|
def test_remove_filter(self):
|
||||||
model = self.app.model
|
model = self.app.model
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue