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
204b05c1
Commit
204b05c1
authored
Oct 18, 2018
by
PJ Janse van Rensburg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Merge 'custom-filter' example into 'sqla'.
parent
dddc2a8f
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
21 additions
and
119 deletions
+21
-119
README.rst
examples/sqla-custom-filter/README.rst
+0
-27
__init__.py
examples/sqla-custom-filter/__init__.py
+0
-1
app.py
examples/sqla-custom-filter/app.py
+0
-88
requirements.txt
examples/sqla-custom-filter/requirements.txt
+0
-3
app.py
examples/sqla/app.py
+21
-0
No files found.
examples/sqla-custom-filter/README.rst
deleted
100644 → 0
View file @
dddc2a8f
Example of custom filters for the SQLAlchemy backend.
To run this example:
1. Clone the repository::
git clone https://github.com/flask-admin/flask-admin.git
cd flask-admin
2. Create and activate a virtual environment::
virtualenv env
source env/bin/activate
3. Install requirements::
pip install -r 'examples/sqla-custom-filter/requirements.txt'
4. Run the application::
python examples/sqla-custom-filter/app.py
The first time you run this example, a sample sqlite database gets populated automatically. To suppress this behaviour,
comment the following lines in app.py:::
if not os.path.exists(database_path):
build_sample_db()
examples/sqla-custom-filter/__init__.py
deleted
100644 → 0
View file @
dddc2a8f
examples/sqla-custom-filter/app.py
deleted
100644 → 0
View file @
dddc2a8f
from
flask
import
Flask
from
flask_sqlalchemy
import
SQLAlchemy
from
flask_admin.contrib
import
sqla
from
flask_admin
import
Admin
# required for creating custom filters
from
flask_admin.contrib.sqla.filters
import
BaseSQLAFilter
,
FilterEqual
# Create application
app
=
Flask
(
__name__
)
# Create dummy secrey key so we can use sessions
app
.
config
[
'SECRET_KEY'
]
=
'123456790'
# Create in-memory database
app
.
config
[
'DATABASE_FILE'
]
=
'sample_db.sqlite'
app
.
config
[
'SQLALCHEMY_DATABASE_URI'
]
=
'sqlite:///'
+
app
.
config
[
'DATABASE_FILE'
]
app
.
config
[
'SQLALCHEMY_ECHO'
]
=
True
db
=
SQLAlchemy
(
app
)
# Flask views
@
app
.
route
(
'/'
)
def
index
():
return
'<a href="/admin/">Click me to get to Admin!</a>'
# Create model
class
User
(
db
.
Model
):
def
__init__
(
self
,
first_name
,
last_name
,
username
,
email
):
self
.
first_name
=
first_name
self
.
last_name
=
last_name
self
.
username
=
username
self
.
email
=
email
id
=
db
.
Column
(
db
.
Integer
,
primary_key
=
True
)
first_name
=
db
.
Column
(
db
.
String
(
100
))
last_name
=
db
.
Column
(
db
.
String
(
100
))
username
=
db
.
Column
(
db
.
String
(
80
),
unique
=
True
)
email
=
db
.
Column
(
db
.
String
(
120
),
unique
=
True
)
# Required for admin interface. For python 3 please use __str__ instead.
def
__unicode__
(
self
):
return
self
.
username
# Create custom filter class
class
FilterLastNameBrown
(
BaseSQLAFilter
):
def
apply
(
self
,
query
,
value
,
alias
=
None
):
if
value
==
'1'
:
return
query
.
filter
(
self
.
column
==
"Brown"
)
else
:
return
query
.
filter
(
self
.
column
!=
"Brown"
)
def
operation
(
self
):
return
'is Brown'
# Add custom filter and standard FilterEqual to ModelView
class
UserAdmin
(
sqla
.
ModelView
):
# each filter in the list is a filter operation (equals, not equals, etc)
# filters with the same name will appear as operations under the same filter
column_filters
=
[
FilterEqual
(
column
=
User
.
last_name
,
name
=
'Last Name'
),
FilterLastNameBrown
(
column
=
User
.
last_name
,
name
=
'Last Name'
,
options
=
((
'1'
,
'Yes'
),
(
'0'
,
'No'
)))
]
admin
=
Admin
(
app
,
template_mode
=
"bootstrap3"
)
admin
.
add_view
(
UserAdmin
(
User
,
db
.
session
))
def
build_sample_db
():
db
.
drop_all
()
db
.
create_all
()
user_obj1
=
User
(
"Paul"
,
"Brown"
,
"pbrown"
,
"paul@gmail.com"
)
user_obj2
=
User
(
"Luke"
,
"Brown"
,
"lbrown"
,
"luke@gmail.com"
)
user_obj3
=
User
(
"Serge"
,
"Koval"
,
"skoval"
,
"serge@gmail.com"
)
db
.
session
.
add_all
([
user_obj1
,
user_obj2
,
user_obj3
])
db
.
session
.
commit
()
if
__name__
==
'__main__'
:
build_sample_db
()
app
.
run
(
port
=
5000
,
debug
=
True
)
examples/sqla-custom-filter/requirements.txt
deleted
100644 → 0
View file @
dddc2a8f
Flask
Flask-Admin
Flask-SQLAlchemy
examples/sqla/app.py
View file @
204b05c1
...
@@ -12,6 +12,7 @@ from flask_admin.contrib import sqla
...
@@ -12,6 +12,7 @@ from flask_admin.contrib import sqla
from
flask_admin.contrib.sqla
import
filters
from
flask_admin.contrib.sqla
import
filters
from
flask_admin.contrib.sqla.form
import
InlineModelConverter
from
flask_admin.contrib.sqla.form
import
InlineModelConverter
from
flask_admin.contrib.sqla.fields
import
InlineModelFormList
from
flask_admin.contrib.sqla.fields
import
InlineModelFormList
from
flask_admin.contrib.sqla.filters
import
BaseSQLAFilter
,
FilterEqual
# Create application
# Create application
...
@@ -112,6 +113,18 @@ def index():
...
@@ -112,6 +113,18 @@ def index():
return
'<a href="/admin/">Click me to get to Admin!</a>'
return
'<a href="/admin/">Click me to get to Admin!</a>'
# Custom filter class
class
FilterLastNameBrown
(
BaseSQLAFilter
):
def
apply
(
self
,
query
,
value
,
alias
=
None
):
if
value
==
'1'
:
return
query
.
filter
(
self
.
column
==
"Brown"
)
else
:
return
query
.
filter
(
self
.
column
!=
"Brown"
)
def
operation
(
self
):
return
'is Brown'
# Customized User model admin
# Customized User model admin
inline_form_options
=
{
inline_form_options
=
{
'form_label'
:
"Info item"
,
'form_label'
:
"Info item"
,
...
@@ -129,9 +142,17 @@ class UserAdmin(sqla.ModelView):
...
@@ -129,9 +142,17 @@ class UserAdmin(sqla.ModelView):
'email'
,
'email'
,
]
]
column_default_sort
=
[(
'last_name'
,
False
),
(
'first_name'
,
False
)]
# sort on multiple columns
column_default_sort
=
[(
'last_name'
,
False
),
(
'first_name'
,
False
)]
# sort on multiple columns
# each filter in the list is a filter operation (equals, not equals, etc)
# filters with the same name will appear as operations under the same filter
column_filters
=
[
FilterEqual
(
column
=
User
.
last_name
,
name
=
'Last Name'
),
FilterLastNameBrown
(
column
=
User
.
last_name
,
name
=
'Last Name'
,
options
=
((
'1'
,
'Yes'
),
(
'0'
,
'No'
)))
]
inline_models
=
[(
UserInfo
,
inline_form_options
),
]
inline_models
=
[(
UserInfo
,
inline_form_options
),
]
# Customized Post model admin
# Customized Post model admin
class
PostAdmin
(
sqla
.
ModelView
):
class
PostAdmin
(
sqla
.
ModelView
):
column_exclude_list
=
[
'text'
]
column_exclude_list
=
[
'text'
]
...
...
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