3
0
Fork 0

fix: add grid filters specific to numeric, integer types

This commit is contained in:
Lance Edgar 2025-01-12 19:12:53 -06:00
parent d63a9223d3
commit c33f211633
2 changed files with 53 additions and 0 deletions

View file

@ -465,6 +465,33 @@ class StringAlchemyFilter(AlchemyFilter):
sa.and_(*criteria))) sa.and_(*criteria)))
class NumericAlchemyFilter(AlchemyFilter):
"""
SQLAlchemy filter option for a numeric data column.
Subclass of :class:`AlchemyFilter`.
"""
default_verbs = ['equal', 'not_equal',
'greater_than', 'greater_equal',
'less_than', 'less_equal']
class IntegerAlchemyFilter(NumericAlchemyFilter):
"""
SQLAlchemy filter option for an integer data column.
Subclass of :class:`NumericAlchemyFilter`.
"""
def coerce_value(self, value):
""" """
if value:
try:
return int(value)
except:
pass
class BooleanAlchemyFilter(AlchemyFilter): class BooleanAlchemyFilter(AlchemyFilter):
""" """
SQLAlchemy filter option for a boolean data column. SQLAlchemy filter option for a boolean data column.
@ -568,6 +595,8 @@ default_sqlalchemy_filters = {
None: AlchemyFilter, None: AlchemyFilter,
sa.String: StringAlchemyFilter, sa.String: StringAlchemyFilter,
sa.Text: StringAlchemyFilter, sa.Text: StringAlchemyFilter,
sa.Numeric: NumericAlchemyFilter,
sa.Integer: IntegerAlchemyFilter,
sa.Boolean: BooleanAlchemyFilter, sa.Boolean: BooleanAlchemyFilter,
sa.Date: DateAlchemyFilter, sa.Date: DateAlchemyFilter,
} }

View file

@ -326,6 +326,30 @@ class TestStringAlchemyFilter(WebTestCase):
self.assertEqual(filtered_query.count(), 6) self.assertEqual(filtered_query.count(), 6)
class TestIntegerAlchemyFilter(WebTestCase):
def make_filter(self, model_property, **kwargs):
factory = kwargs.pop('factory', mod.IntegerAlchemyFilter)
kwargs['model_property'] = model_property
return factory(self.request, model_property.key, **kwargs)
def test_coerce_value(self):
model = self.app.model
filtr = self.make_filter(model.Upgrade.exit_code)
# null
self.assertIsNone(filtr.coerce_value(None))
self.assertIsNone(filtr.coerce_value(''))
# typical
self.assertEqual(filtr.coerce_value('42'), 42)
self.assertEqual(filtr.coerce_value('-42'), -42)
# invalid
self.assertIsNone(filtr.coerce_value('42.12'))
self.assertIsNone(filtr.coerce_value('bogus'))
class TestBooleanAlchemyFilter(WebTestCase): class TestBooleanAlchemyFilter(WebTestCase):
def setUp(self): def setUp(self):