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
6b6fe519
Commit
6b6fe519
authored
Jun 13, 2015
by
Serge S. Koval
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of github.com:flask-admin/flask-admin
parents
c62a1715
38173f89
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
49 additions
and
3 deletions
+49
-3
app.py
examples/forms/app.py
+2
-1
upload.py
flask_admin/form/upload.py
+25
-2
test_form_upload.py
flask_admin/tests/test_form_upload.py
+22
-0
No files found.
examples/forms/app.py
View file @
6b6fe519
...
...
@@ -101,7 +101,8 @@ class FileView(sqla.ModelView):
form_args
=
{
'path'
:
{
'label'
:
'File'
,
'base_path'
:
file_path
'base_path'
:
file_path
,
'allow_overwrite'
:
False
}
}
...
...
flask_admin/form/upload.py
View file @
6b6fe519
...
...
@@ -51,12 +51,21 @@ class FileUploadInput(object):
template
=
self
.
data_template
if
field
.
data
else
self
.
empty_template
if
field
.
errors
:
template
=
self
.
empty_template
if
field
.
data
and
isinstance
(
field
.
data
,
FileStorage
):
value
=
field
.
data
.
filename
else
:
value
=
field
.
data
or
''
return
HTMLString
(
template
%
{
'text'
:
html_params
(
type
=
'text'
,
readonly
=
'readonly'
,
value
=
field
.
data
,
value
=
value
,
name
=
field
.
name
),
'file'
:
html_params
(
type
=
'file'
,
value
=
value
,
**
kwargs
),
'marker'
:
'_
%
s-delete'
%
field
.
name
})
...
...
@@ -122,7 +131,7 @@ class FileUploadField(fields.StringField):
def
__init__
(
self
,
label
=
None
,
validators
=
None
,
base_path
=
None
,
relative_path
=
None
,
namegen
=
None
,
allowed_extensions
=
None
,
permission
=
0o666
,
permission
=
0o666
,
allow_overwrite
=
True
,
**
kwargs
):
"""
Constructor.
...
...
@@ -154,6 +163,11 @@ class FileUploadField(fields.StringField):
:param allowed_extensions:
List of allowed extensions. If not provided, will allow any file.
:param allow_overwrite:
Whether to overwrite existing files in upload directory. Defaults to `True`.
.. versionadded:: 1.1.1
The `allow_overwrite` parameter was added.
"""
self
.
base_path
=
base_path
self
.
relative_path
=
relative_path
...
...
@@ -161,6 +175,7 @@ class FileUploadField(fields.StringField):
self
.
namegen
=
namegen
or
namegen_filename
self
.
allowed_extensions
=
allowed_extensions
self
.
permission
=
permission
self
.
_allow_overwrite
=
allow_overwrite
self
.
_should_delete
=
False
...
...
@@ -188,6 +203,11 @@ class FileUploadField(fields.StringField):
def
pre_validate
(
self
,
form
):
if
self
.
_is_uploaded_file
(
self
.
data
)
and
not
self
.
is_file_allowed
(
self
.
data
.
filename
):
raise
ValidationError
(
gettext
(
'Invalid file extension'
))
# Handle overwriting existing content
if
not
self
.
_is_uploaded_file
(
self
.
data
):
return
if
self
.
_allow_overwrite
==
False
and
os
.
path
.
exists
(
self
.
_get_path
(
self
.
data
.
filename
)):
raise
ValidationError
(
gettext
(
'File "
%
s" already exists.'
%
self
.
data
.
filename
))
def
process
(
self
,
formdata
,
data
=
unset_value
):
if
formdata
:
...
...
@@ -253,6 +273,9 @@ class FileUploadField(fields.StringField):
if
not
op
.
exists
(
op
.
dirname
(
path
)):
os
.
makedirs
(
os
.
path
.
dirname
(
path
),
self
.
permission
|
0o111
)
if
self
.
_allow_overwrite
==
False
and
os
.
path
.
exists
(
path
):
raise
ValueError
(
gettext
(
'File "
%
s" already exists.'
%
path
))
data
.
save
(
path
)
return
filename
...
...
flask_admin/tests/test_form_upload.py
View file @
6b6fe519
...
...
@@ -40,6 +40,9 @@ def test_upload_field():
class
TestForm
(
form
.
BaseForm
):
upload
=
form
.
FileUploadField
(
'Upload'
,
base_path
=
path
)
class
TestNoOverWriteForm
(
form
.
BaseForm
):
upload
=
form
.
FileUploadField
(
'Upload'
,
base_path
=
path
,
allow_overwrite
=
False
)
class
Dummy
(
object
):
pass
...
...
@@ -74,6 +77,7 @@ def test_upload_field():
# Check delete
with
app
.
test_request_context
(
method
=
'POST'
,
data
=
{
'_upload-delete'
:
'checked'
}):
my_form
=
TestForm
(
helpers
.
get_form_data
())
ok_
(
my_form
.
validate
())
...
...
@@ -83,6 +87,24 @@ def test_upload_field():
ok_
(
not
op
.
exists
(
op
.
join
(
path
,
'test2.txt'
)))
# Check overwrite
_remove_testfiles
()
my_form_ow
=
TestNoOverWriteForm
()
with
app
.
test_request_context
(
method
=
'POST'
,
data
=
{
'upload'
:
(
BytesIO
(
b
'Hullo'
),
'test1.txt'
)}):
my_form_ow
=
TestNoOverWriteForm
(
helpers
.
get_form_data
())
ok_
(
my_form_ow
.
validate
())
my_form_ow
.
populate_obj
(
dummy
)
eq_
(
dummy
.
upload
,
'test1.txt'
)
ok_
(
op
.
exists
(
op
.
join
(
path
,
'test1.txt'
)))
with
app
.
test_request_context
(
method
=
'POST'
,
data
=
{
'upload'
:
(
BytesIO
(
b
'Hullo'
),
'test1.txt'
)}):
my_form_ow
=
TestNoOverWriteForm
(
helpers
.
get_form_data
())
ok_
(
not
my_form_ow
.
validate
())
_remove_testfiles
()
def
test_image_upload_field
():
app
=
Flask
(
__name__
)
...
...
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