From 0220e401cd68256c2c0231284f566885cd7b07ba Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Fri, 4 Dec 2020 15:26:21 -0600 Subject: [PATCH] Add "is empty" and related verbs, for "string" type grid filters --- tailbone/grids/filters.py | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/tailbone/grids/filters.py b/tailbone/grids/filters.py index 9d08c881..02ca9130 100644 --- a/tailbone/grids/filters.py +++ b/tailbone/grids/filters.py @@ -134,19 +134,33 @@ class GridFilter(object): 'greater_equal': "greater than or equal to", 'less_than': "less than", 'less_equal': "less than or equal to", + 'is_empty': "is empty", + 'is_not_empty': "is not empty", 'is_null': "is null", 'is_not_null': "is not null", 'is_true': "is true", 'is_false': "is false", 'is_false_null': "is false or null", + 'is_empty_or_null': "is either empty or null", 'contains': "contains", 'does_not_contain': "does not contain", 'is_me': "is me", 'is_not_me': "is not me", } - valueless_verbs = ['is_any', 'is_null', 'is_not_null', 'is_true', 'is_false', - 'is_false_null', 'is_me', 'is_not_me'] + valueless_verbs = [ + 'is_any', + 'is_empty', + 'is_not_empty', + 'is_null', + 'is_not_null', + 'is_true', + 'is_false', + 'is_false_null', + 'is_empty_or_null', + 'is_me', + 'is_not_me', + ] value_renderer_factory = DefaultValueRenderer data_type = 'string' # default, but will be set from value renderer @@ -380,7 +394,11 @@ class AlchemyStringFilter(AlchemyGridFilter): Expose contains / does-not-contain verbs in addition to core. """ return ['contains', 'does_not_contain', - 'equal', 'not_equal', 'is_null', 'is_not_null', 'is_any'] + 'equal', 'not_equal', + 'is_empty', 'is_not_empty', + 'is_null', 'is_not_null', + 'is_empty_or_null', + 'is_any'] def filter_contains(self, query, value): """ @@ -408,6 +426,17 @@ class AlchemyStringFilter(AlchemyGridFilter): for v in value.split()]), )) + def filter_is_empty(self, query, value): + return query.filter(sa.func.trim(self.column) == self.encode_value('')) + + def filter_is_not_empty(self, query, value): + return query.filter(sa.func.trim(self.column) != self.encode_value('')) + + def filter_is_empty_or_null(self, query, value): + return query.filter( + sa.or_( + sa.func.trim(self.column) == self.encode_value(''), + self.column == None)) class AlchemyEmptyStringFilter(AlchemyStringFilter): """