Add "equal to any of" verb for string-type grid filters

This commit is contained in:
Lance Edgar 2023-02-28 14:30:25 -06:00
parent 01af73502a
commit ad311e9e7e

View file

@ -331,6 +331,38 @@ class AlchemyGridFilter(GridFilter):
self.column != self.encode_value(value),
))
def filter_equal_any_of(self, query, value):
"""
This filter expects "multiple values" separated by newline
character, and will add an "OR" condition with each value
being checked separately. For instance if the user submits a
"value" like this:
.. code-block:: none
foo bar
baz
This will result in SQL condition like this:
.. code-block:: sql
name = 'foo bar' OR name = 'baz'
"""
if not value:
return query
values = value.split('\n')
values = [value for value in values if value]
if not values:
return query
conditions = []
for value in values:
conditions.append(self.column == self.encode_value(value))
return query.filter(sa.or_(*conditions))
def filter_is_null(self, query, value):
"""
Filter data with an 'IS NULL' query. Note that this filter does not
@ -430,7 +462,7 @@ class AlchemyStringFilter(AlchemyGridFilter):
"""
return ['contains', 'does_not_contain',
'contains_any_of',
'equal', 'not_equal',
'equal', 'not_equal', 'equal_any_of',
'is_empty', 'is_not_empty',
'is_null', 'is_not_null',
'is_empty_or_null',