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
if isinstance(column.type.choices, enum.EnumMeta):
for choice in column.type.choices:
if choice.name == user_query:
choice_type = choice.value
break
else:
for type, value in column.type.choices: for type, value in column.type.choices:
if value == user_query: if value == user_query:
choice_type = type choice_type = type
break break
if choice_type:
return query.filter(column == choice_type) return query.filter(column == choice_type)
else:
return query.filter(column.in_([]))
class ChoiceTypeNotEqualFilter(FilterNotEqual): class ChoiceTypeNotEqualFilter(FilterNotEqual):
...@@ -362,6 +372,12 @@ class ChoiceTypeNotEqualFilter(FilterNotEqual): ...@@ -362,6 +372,12 @@ 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
if isinstance(column.type.choices, enum.EnumMeta):
for choice in column.type.choices:
if choice.name == user_query:
choice_type = choice.value
break
else:
for type, value in column.type.choices: for type, value in column.type.choices:
if value == user_query: if value == user_query:
choice_type = type choice_type = type
...@@ -382,6 +398,11 @@ class ChoiceTypeLikeFilter(FilterLike): ...@@ -382,6 +398,11 @@ 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
if isinstance(column.type.choices, enum.EnumMeta):
for choice in column.type.choices:
if user_query.lower() in choice.name.lower():
choice_types.append(choice.value)
else:
for type, value in column.type.choices: for type, value in column.type.choices:
if user_query.lower() in value.lower(): if user_query.lower() in value.lower():
choice_types.append(type) choice_types.append(type)
...@@ -400,6 +421,11 @@ class ChoiceTypeNotLikeFilter(FilterNotLike): ...@@ -400,6 +421,11 @@ 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
if isinstance(column.type.choices, enum.EnumMeta):
for choice in column.type.choices:
if user_query.lower() in choice.name.lower():
choice_types.append(choice.value)
else:
for type, value in column.type.choices: for type, value in column.type.choices:
if user_query.lower() in value.lower(): if user_query.lower() in value.lower():
choice_types.append(type) choice_types.append(type)
......
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