Add grid filter which treats empty string as NULL

This commit is contained in:
Lance Edgar 2017-10-22 20:00:31 -07:00
parent 512405f01f
commit 401cba23b7
2 changed files with 21 additions and 0 deletions

View file

@ -123,6 +123,9 @@ class Grid(object):
def set_sorter(self, key, *args, **kwargs): def set_sorter(self, key, *args, **kwargs):
self.sorters[key] = self.make_sorter(*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): def set_label(self, key, label):
self.labels[key] = label self.labels[key] = label
if key in self.filters: if key in self.filters:

View file

@ -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): class AlchemyByteStringFilter(AlchemyStringFilter):
""" """
String filter for SQLAlchemy, which encodes value as bytestring before String filter for SQLAlchemy, which encodes value as bytestring before