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
5c77e899
Commit
5c77e899
authored
Aug 26, 2012
by
Serge S. Koval
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Peewee backend: Mass-model actions
parent
19ae3be1
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
47 additions
and
1 deletion
+47
-1
view.py
flask_admin/contrib/peeweemodel/view.py
+47
-1
No files found.
flask_admin/contrib/peeweemodel/view.py
View file @
5c77e899
...
...
@@ -4,9 +4,10 @@ from flask.ext.admin import form
from
flask.ext.admin.babel
import
gettext
,
ngettext
,
lazy_gettext
from
flask.ext.admin.model
import
BaseModelView
from
peewee
import
PrimaryKeyField
,
ForeignKeyField
,
Field
,
CharField
,
TextField
,
Q
from
peewee
import
PrimaryKeyField
,
ForeignKeyField
,
Field
,
CharField
,
TextField
from
wtfpeewee.orm
import
model_form
from
flask.ext.admin.actions
import
action
from
flask.ext.admin.contrib.peeweemodel
import
filters
from
.form
import
CustomModelConverter
...
...
@@ -36,6 +37,18 @@ class ModelView(BaseModelView):
Override this attribute to use non-default converter.
"""
fast_mass_delete
=
False
"""
If set to `False` and user deletes more than one model using actions,
all models will be read from the database and then deleted one by one
giving SQLAlchemy chance to manually cleanup any dependencies (many-to-many
relationships, etc).
If set to True, will run DELETE statement which is somewhat faster, but
might leave corrupted data if you forget to configure DELETE CASCADE
for your model.
"""
def
__init__
(
self
,
model
,
name
=
None
,
category
=
None
,
endpoint
=
None
,
url
=
None
):
self
.
_search_fields
=
[]
...
...
@@ -246,3 +259,36 @@ class ModelView(BaseModelView):
except
Exception
,
ex
:
flash
(
gettext
(
'Failed to delete model.
%(error)
s'
,
error
=
str
(
ex
)),
'error'
)
return
False
# Default model actions
def
is_action_allowed
(
self
,
name
):
# Check delete action permission
if
name
==
'delete'
and
not
self
.
can_delete
:
return
False
return
super
(
ModelView
,
self
)
.
is_action_allowed
(
name
)
@
action
(
'delete'
,
lazy_gettext
(
'Delete'
),
lazy_gettext
(
'Are you sure you want to delete selected models?'
))
def
action_delete
(
self
,
ids
):
try
:
model_pk
=
getattr
(
self
.
model
,
self
.
_primary_key
)
if
self
.
fast_mass_delete
:
count
=
self
.
model
.
delete
()
.
where
(
model_pk
<<
ids
)
.
execute
()
else
:
count
=
0
query
=
self
.
model
.
select
()
.
filter
(
model_pk
<<
ids
)
for
m
in
query
:
m
.
delete_instance
(
recursive
=
True
)
count
+=
1
flash
(
ngettext
(
'Model was successfully deleted.'
,
'
%(count)
s models were sucessfully deleted.'
,
count
,
count
=
count
))
except
Exception
,
ex
:
flash
(
gettext
(
'Failed to delete models.
%(error)
s'
,
error
=
str
(
ex
)),
'error'
)
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