Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
flask-admin
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Python-Dev
flask-admin
Commits
9a2fa7e0
Commit
9a2fa7e0
authored
Oct 25, 2018
by
PJ Janse van Rensburg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Support ChoiceType Enum field filters.
parent
0e79fae7
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
42 additions
and
16 deletions
+42
-16
filters.py
flask_admin/contrib/sqla/filters.py
+42
-16
No files found.
flask_admin/contrib/sqla/filters.py
View file @
9a2fa7e0
...
@@ -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
))
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment