3
0
Fork 0

fix: include grid filters for all column properties of model class

by default anyway.  previous logic started from `grid.columns` and
then only included column properties, but now we start from the model
class itself and let sa-utils figure out the default list
This commit is contained in:
Lance Edgar 2024-12-28 21:08:10 -06:00
parent c2efc1cd1a
commit 84ab931081
3 changed files with 22 additions and 12 deletions

View file

@ -42,6 +42,7 @@ dependencies = [
"pyramid_fanstatic", "pyramid_fanstatic",
"pyramid_mako", "pyramid_mako",
"pyramid_tm", "pyramid_tm",
"SQLAlchemy-Utils",
"waitress", "waitress",
"WebHelpers2", "WebHelpers2",
"WuttJamaican[db]>=0.19.1", "WuttJamaican[db]>=0.19.1",

View file

@ -32,6 +32,7 @@ from collections import namedtuple, OrderedDict
import sqlalchemy as sa import sqlalchemy as sa
from sqlalchemy import orm from sqlalchemy import orm
from sqlalchemy_utils import get_columns
import paginate import paginate
from paginate_sqlalchemy import SqlalchemyOrmPage from paginate_sqlalchemy import SqlalchemyOrmPage
@ -1116,19 +1117,16 @@ class Grid:
filters = filters or {} filters = filters or {}
if self.model_class: if self.model_class:
# TODO: i tried using self.get_model_columns() here but in # nb. i first tried self.get_model_columns() but my notes
# many cases that will be too aggressive. however it is # say that was too aggressive in many cases. then i tried
# often the case that the *grid* columns are a subset of # using the *subset* of self.columns, just the ones which
# the unerlying *table* columns. so until a better way # corresponded to a property on the model class. and now
# is found, we choose "too few" instead of "too many" # i am using sa-utils to give the "true" column list..
# filters here. surely must improve it at some point. for col in get_columns(self.model_class):
for key in self.columns: if col.key in filters:
if key in filters:
continue continue
prop = getattr(self.model_class, key, None) prop = getattr(self.model_class, col.key)
if (prop and hasattr(prop, 'property') filters[prop.key] = self.make_filter(prop)
and isinstance(prop.property, orm.ColumnProperty)):
filters[prop.key] = self.make_filter(prop)
return filters return filters

View file

@ -982,6 +982,17 @@ class TestGrid(WebTestCase):
self.assertEqual(filters['value'], 42) self.assertEqual(filters['value'], 42)
self.assertEqual(myfilters['value'], 42) self.assertEqual(myfilters['value'], 42)
# filters for all *true* columns by default, despite grid.columns
with patch.object(mod.Grid, 'make_filter'):
# nb. filters are MagicMock instances
grid = self.make_grid(model_class=model.User,
columns=['username', 'person'])
filters = grid.make_backend_filters()
self.assertIn('username', filters)
self.assertIn('active', filters)
# nb. relationship not included by default
self.assertNotIn('person', filters)
def test_make_filter(self): def test_make_filter(self):
model = self.app.model model = self.app.model