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