Commit 9a2fa7e0 authored by PJ Janse van Rensburg's avatar PJ Janse van Rensburg

Support ChoiceType Enum field filters.

parent 0e79fae7
...@@ -2,6 +2,7 @@ from flask_admin.babel import lazy_gettext ...@@ -2,6 +2,7 @@ from flask_admin.babel import lazy_gettext
from flask_admin.model import filters from flask_admin.model import filters
from flask_admin.contrib.sqla import tools from flask_admin.contrib.sqla import tools
from sqlalchemy.sql import not_, or_ from sqlalchemy.sql import not_, or_
import enum
class BaseSQLAFilter(filters.BaseFilter): class BaseSQLAFilter(filters.BaseFilter):
...@@ -345,13 +346,22 @@ class ChoiceTypeEqualFilter(FilterEqual): ...@@ -345,13 +346,22 @@ class ChoiceTypeEqualFilter(FilterEqual):
def apply(self, query, user_query, alias=None): def apply(self, query, user_query, alias=None):
column = self.get_column(alias) column = self.get_column(alias)
choice_type = user_query choice_type = None
# loop through choice 'values' to try and find an exact match # loop through choice 'values' to try and find an exact match
for type, value in column.type.choices: if isinstance(column.type.choices, enum.EnumMeta):
if value == user_query: for choice in column.type.choices:
choice_type = type if choice.name == user_query:
break choice_type = choice.value
return query.filter(column == choice_type) break
else:
for type, value in column.type.choices:
if value == user_query:
choice_type = type
break
if choice_type:
return query.filter(column == choice_type)
else:
return query.filter(column.in_([]))
class ChoiceTypeNotEqualFilter(FilterNotEqual): class ChoiceTypeNotEqualFilter(FilterNotEqual):
...@@ -362,10 +372,16 @@ class ChoiceTypeNotEqualFilter(FilterNotEqual): ...@@ -362,10 +372,16 @@ class ChoiceTypeNotEqualFilter(FilterNotEqual):
column = self.get_column(alias) column = self.get_column(alias)
choice_type = None choice_type = None
# loop through choice 'values' to try and find an exact match # loop through choice 'values' to try and find an exact match
for type, value in column.type.choices: if isinstance(column.type.choices, enum.EnumMeta):
if value == user_query: for choice in column.type.choices:
choice_type = type if choice.name == user_query:
break choice_type = choice.value
break
else:
for type, value in column.type.choices:
if value == user_query:
choice_type = type
break
if choice_type: if choice_type:
# != can exclude NULL values, so "or_ == None" needed to be added # != can exclude NULL values, so "or_ == None" needed to be added
return query.filter(or_(column != choice_type, column == None)) return query.filter(or_(column != choice_type, column == None))
...@@ -382,9 +398,14 @@ class ChoiceTypeLikeFilter(FilterLike): ...@@ -382,9 +398,14 @@ class ChoiceTypeLikeFilter(FilterLike):
choice_types = [] choice_types = []
if user_query: if user_query:
# loop through choice 'values' looking for matches # loop through choice 'values' looking for matches
for type, value in column.type.choices: if isinstance(column.type.choices, enum.EnumMeta):
if user_query.lower() in value.lower(): for choice in column.type.choices:
choice_types.append(type) if user_query.lower() in choice.name.lower():
choice_types.append(choice.value)
else:
for type, value in column.type.choices:
if user_query.lower() in value.lower():
choice_types.append(type)
if choice_types: if choice_types:
return query.filter(column.in_(choice_types)) return query.filter(column.in_(choice_types))
else: else:
...@@ -400,9 +421,14 @@ class ChoiceTypeNotLikeFilter(FilterNotLike): ...@@ -400,9 +421,14 @@ class ChoiceTypeNotLikeFilter(FilterNotLike):
choice_types = [] choice_types = []
if user_query: if user_query:
# loop through choice 'values' looking for matches # loop through choice 'values' looking for matches
for type, value in column.type.choices: if isinstance(column.type.choices, enum.EnumMeta):
if user_query.lower() in value.lower(): for choice in column.type.choices:
choice_types.append(type) if user_query.lower() in choice.name.lower():
choice_types.append(choice.value)
else:
for type, value in column.type.choices:
if user_query.lower() in value.lower():
choice_types.append(type)
if choice_types: if choice_types:
# != can exclude NULL values, so "or_ == None" needed to be added # != can exclude NULL values, so "or_ == None" needed to be added
return query.filter(or_(column.notin_(choice_types), column == None)) return query.filter(or_(column.notin_(choice_types), column == None))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment