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
0e79fae7
Commit
0e79fae7
authored
Oct 25, 2018
by
PJ Janse van Rensburg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add filters for ChoiceType fields.
parent
1daad3eb
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
78 additions
and
1 deletion
+78
-1
filters.py
flask_admin/contrib/sqla/filters.py
+78
-1
No files found.
flask_admin/contrib/sqla/filters.py
View file @
0e79fae7
...
@@ -339,6 +339,77 @@ class EnumFilterNotInList(FilterNotInList):
...
@@ -339,6 +339,77 @@ class EnumFilterNotInList(FilterNotInList):
return
values
return
values
class
ChoiceTypeEqualFilter
(
FilterEqual
):
def
__init__
(
self
,
column
,
name
,
options
=
None
,
**
kwargs
):
super
(
ChoiceTypeEqualFilter
,
self
)
.
__init__
(
column
,
name
,
options
,
**
kwargs
)
def
apply
(
self
,
query
,
user_query
,
alias
=
None
):
column
=
self
.
get_column
(
alias
)
choice_type
=
user_query
# loop through choice 'values' to try and find an exact match
for
type
,
value
in
column
.
type
.
choices
:
if
value
==
user_query
:
choice_type
=
type
break
return
query
.
filter
(
column
==
choice_type
)
class
ChoiceTypeNotEqualFilter
(
FilterNotEqual
):
def
__init__
(
self
,
column
,
name
,
options
=
None
,
**
kwargs
):
super
(
ChoiceTypeNotEqualFilter
,
self
)
.
__init__
(
column
,
name
,
options
,
**
kwargs
)
def
apply
(
self
,
query
,
user_query
,
alias
=
None
):
column
=
self
.
get_column
(
alias
)
choice_type
=
None
# loop through choice 'values' to try and find an exact match
for
type
,
value
in
column
.
type
.
choices
:
if
value
==
user_query
:
choice_type
=
type
break
if
choice_type
:
# != can exclude NULL values, so "or_ == None" needed to be added
return
query
.
filter
(
or_
(
column
!=
choice_type
,
column
==
None
))
else
:
return
query
class
ChoiceTypeLikeFilter
(
FilterLike
):
def
__init__
(
self
,
column
,
name
,
options
=
None
,
**
kwargs
):
super
(
ChoiceTypeLikeFilter
,
self
)
.
__init__
(
column
,
name
,
options
,
**
kwargs
)
def
apply
(
self
,
query
,
user_query
,
alias
=
None
):
column
=
self
.
get_column
(
alias
)
choice_types
=
[]
if
user_query
:
# loop through choice 'values' looking for matches
for
type
,
value
in
column
.
type
.
choices
:
if
user_query
.
lower
()
in
value
.
lower
():
choice_types
.
append
(
type
)
if
choice_types
:
return
query
.
filter
(
column
.
in_
(
choice_types
))
else
:
return
query
class
ChoiceTypeNotLikeFilter
(
FilterNotLike
):
def
__init__
(
self
,
column
,
name
,
options
=
None
,
**
kwargs
):
super
(
ChoiceTypeNotLikeFilter
,
self
)
.
__init__
(
column
,
name
,
options
,
**
kwargs
)
def
apply
(
self
,
query
,
user_query
,
alias
=
None
):
column
=
self
.
get_column
(
alias
)
choice_types
=
[]
if
user_query
:
# loop through choice 'values' looking for matches
for
type
,
value
in
column
.
type
.
choices
:
if
user_query
.
lower
()
in
value
.
lower
():
choice_types
.
append
(
type
)
if
choice_types
:
# != can exclude NULL values, so "or_ == None" needed to be added
return
query
.
filter
(
or_
(
column
.
notin_
(
choice_types
),
column
==
None
))
else
:
return
query
# Base SQLA filter field converter
# Base SQLA filter field converter
class
FilterConverter
(
filters
.
BaseFilterConverter
):
class
FilterConverter
(
filters
.
BaseFilterConverter
):
strings
=
(
FilterLike
,
FilterNotLike
,
FilterEqual
,
FilterNotEqual
,
strings
=
(
FilterLike
,
FilterNotLike
,
FilterEqual
,
FilterNotEqual
,
...
@@ -362,6 +433,8 @@ class FilterConverter(filters.BaseFilterConverter):
...
@@ -362,6 +433,8 @@ class FilterConverter(filters.BaseFilterConverter):
time_filters
=
(
TimeEqualFilter
,
TimeNotEqualFilter
,
TimeGreaterFilter
,
time_filters
=
(
TimeEqualFilter
,
TimeNotEqualFilter
,
TimeGreaterFilter
,
TimeSmallerFilter
,
TimeBetweenFilter
,
TimeNotBetweenFilter
,
TimeSmallerFilter
,
TimeBetweenFilter
,
TimeNotBetweenFilter
,
FilterEmpty
)
FilterEmpty
)
choice_type_filters
=
(
ChoiceTypeEqualFilter
,
ChoiceTypeNotEqualFilter
,
ChoiceTypeLikeFilter
,
ChoiceTypeNotLikeFilter
,
FilterEmpty
)
def
convert
(
self
,
type_name
,
column
,
name
,
**
kwargs
):
def
convert
(
self
,
type_name
,
column
,
name
,
**
kwargs
):
filter_name
=
type_name
.
lower
()
filter_name
=
type_name
.
lower
()
...
@@ -373,7 +446,7 @@ class FilterConverter(filters.BaseFilterConverter):
...
@@ -373,7 +446,7 @@ class FilterConverter(filters.BaseFilterConverter):
@
filters
.
convert
(
'string'
,
'char'
,
'unicode'
,
'varchar'
,
'tinytext'
,
@
filters
.
convert
(
'string'
,
'char'
,
'unicode'
,
'varchar'
,
'tinytext'
,
'text'
,
'mediumtext'
,
'longtext'
,
'unicodetext'
,
'text'
,
'mediumtext'
,
'longtext'
,
'unicodetext'
,
'nchar'
,
'nvarchar'
,
'ntext'
,
'citext'
)
'nchar'
,
'nvarchar'
,
'ntext'
,
'citext'
,
'emailtype'
)
def
conv_string
(
self
,
column
,
name
,
**
kwargs
):
def
conv_string
(
self
,
column
,
name
,
**
kwargs
):
return
[
f
(
column
,
name
,
**
kwargs
)
for
f
in
self
.
strings
]
return
[
f
(
column
,
name
,
**
kwargs
)
for
f
in
self
.
strings
]
...
@@ -402,6 +475,10 @@ class FilterConverter(filters.BaseFilterConverter):
...
@@ -402,6 +475,10 @@ class FilterConverter(filters.BaseFilterConverter):
def
conv_time
(
self
,
column
,
name
,
**
kwargs
):
def
conv_time
(
self
,
column
,
name
,
**
kwargs
):
return
[
f
(
column
,
name
,
**
kwargs
)
for
f
in
self
.
time_filters
]
return
[
f
(
column
,
name
,
**
kwargs
)
for
f
in
self
.
time_filters
]
@
filters
.
convert
(
'ChoiceType'
)
def
conv_sqla_utils_choice
(
self
,
column
,
name
,
**
kwargs
):
return
[
f
(
column
,
name
,
**
kwargs
)
for
f
in
self
.
choice_type_filters
]
@
filters
.
convert
(
'enum'
)
@
filters
.
convert
(
'enum'
)
def
conv_enum
(
self
,
column
,
name
,
options
=
None
,
**
kwargs
):
def
conv_enum
(
self
,
column
,
name
,
options
=
None
,
**
kwargs
):
if
not
options
:
if
not
options
:
...
...
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