From 401cba23b7aaa6ea8ed1d9be5152027b6dbbda96 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Sun, 22 Oct 2017 20:00:31 -0700 Subject: [PATCH] Add grid filter which treats empty string as NULL --- tailbone/grids/core.py | 3 +++ tailbone/grids/filters.py | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/tailbone/grids/core.py b/tailbone/grids/core.py index 4f79587b..7729a536 100644 --- a/tailbone/grids/core.py +++ b/tailbone/grids/core.py @@ -123,6 +123,9 @@ class Grid(object): def set_sorter(self, key, *args, **kwargs): self.sorters[key] = self.make_sorter(*args, **kwargs) + def set_filter(self, key, *args, **kwargs): + self.filters[key] = self.make_filter(key, *args, **kwargs) + def set_label(self, key, label): self.labels[key] = label if key in self.filters: diff --git a/tailbone/grids/filters.py b/tailbone/grids/filters.py index 87964693..e5618320 100644 --- a/tailbone/grids/filters.py +++ b/tailbone/grids/filters.py @@ -334,6 +334,24 @@ class AlchemyStringFilter(AlchemyGridFilter): )) +class AlchemyEmptyStringFilter(AlchemyStringFilter): + """ + String filter with special logic to treat empty string values as NULL + """ + + def filter_is_null(self, query, value): + return query.filter( + sa.or_( + self.column == None, + sa.func.trim(self.column) == '')) + + def filter_is_not_null(self, query, value): + return query.filter( + sa.and_( + self.column != None, + sa.func.trim(self.column) != '')) + + class AlchemyByteStringFilter(AlchemyStringFilter): """ String filter for SQLAlchemy, which encodes value as bytestring before