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
716880e1
Commit
716880e1
authored
Mar 29, 2012
by
Serge S. Koval
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed file uploading in FileAdmin.
parent
fedd6792
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
16 additions
and
10 deletions
+16
-10
fileadmin.py
flask_adminex/ext/fileadmin.py
+12
-9
form.py
flask_adminex/form.py
+4
-1
No files found.
flask_adminex/ext/fileadmin.py
View file @
716880e1
...
@@ -36,18 +36,21 @@ class UploadForm(form.BaseForm):
...
@@ -36,18 +36,21 @@ class UploadForm(form.BaseForm):
File upload form. Works with FileAdmin instance to check if it is allowed
File upload form. Works with FileAdmin instance to check if it is allowed
to upload file with given extension.
to upload file with given extension.
"""
"""
upload
=
wtf
.
FileField
(
'File to upload'
,
validators
=
[
wtf
.
file_required
()]
)
upload
=
wtf
.
FileField
(
'File to upload'
)
def
__init__
(
self
,
form
,
admin
):
def
__init__
(
self
,
admin
):
self
.
admin
=
admin
self
.
admin
=
admin
super
(
UploadForm
,
self
)
.
__init__
(
form
)
super
(
UploadForm
,
self
)
.
__init__
()
def
validate_upload
(
self
,
field
):
def
validate_upload
(
self
,
field
):
filename
=
field
.
file
.
filename
if
not
self
.
upload
.
has_file
():
raise
wtf
.
ValidationError
(
'File required.'
)
filename
=
self
.
upload
.
data
.
filename
if
not
self
.
admin
.
is_file_allowed
(
filename
):
if
not
self
.
admin
.
is_file_allowed
(
filename
):
raise
wtf
.
ValidationError
(
'Invalid file type'
)
raise
wtf
.
ValidationError
(
'Invalid file type
.
'
)
class
FileAdmin
(
BaseView
):
class
FileAdmin
(
BaseView
):
...
@@ -331,17 +334,17 @@ class FileAdmin(BaseView):
...
@@ -331,17 +334,17 @@ class FileAdmin(BaseView):
flash
(
'File uploading is disabled.'
,
'error'
)
flash
(
'File uploading is disabled.'
,
'error'
)
return
redirect
(
self
.
_get_dir_url
(
'.index'
,
path
))
return
redirect
(
self
.
_get_dir_url
(
'.index'
,
path
))
form
=
UploadForm
(
request
.
form
,
self
)
form
=
UploadForm
(
self
)
if
form
.
validate_on_submit
():
if
form
.
validate_on_submit
():
filename
=
op
.
join
(
directory
,
filename
=
op
.
join
(
directory
,
secure_filename
(
form
.
upload
.
file
.
filename
))
secure_filename
(
form
.
upload
.
data
.
filename
))
if
op
.
exists
(
filename
):
if
op
.
exists
(
filename
):
flash
(
'File "
%
s" already exists.'
%
form
.
upload
.
file
.
filename
,
flash
(
'File "
%
s" already exists.'
%
form
.
upload
.
data
.
filename
,
'error'
)
'error'
)
else
:
else
:
try
:
try
:
self
.
save_file
(
filename
,
form
.
upload
.
file
)
self
.
save_file
(
filename
,
form
.
upload
.
data
)
return
redirect
(
self
.
_get_dir_url
(
'.index'
,
path
))
return
redirect
(
self
.
_get_dir_url
(
'.index'
,
path
))
except
Exception
,
ex
:
except
Exception
,
ex
:
flash
(
'Failed to save file:
%
s'
%
ex
)
flash
(
'Failed to save file:
%
s'
%
ex
)
...
...
flask_adminex/form.py
View file @
716880e1
...
@@ -10,7 +10,10 @@ class BaseForm(wtf.Form):
...
@@ -10,7 +10,10 @@ class BaseForm(wtf.Form):
Customized form class.
Customized form class.
"""
"""
def
__init__
(
self
,
formdata
=
None
,
obj
=
None
,
prefix
=
''
,
**
kwargs
):
def
__init__
(
self
,
formdata
=
None
,
obj
=
None
,
prefix
=
''
,
**
kwargs
):
super
(
BaseForm
,
self
)
.
__init__
(
formdata
,
obj
,
prefix
,
**
kwargs
)
if
formdata
:
super
(
BaseForm
,
self
)
.
__init__
(
formdata
,
obj
,
prefix
,
**
kwargs
)
else
:
super
(
BaseForm
,
self
)
.
__init__
(
obj
=
obj
,
prefix
=
prefix
,
**
kwargs
)
self
.
_obj
=
obj
self
.
_obj
=
obj
...
...
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