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
7962a8ee
Commit
7962a8ee
authored
Jul 28, 2013
by
Serge S. Koval
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Image upload field
parent
d2775fe3
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
0 additions
and
143 deletions
+0
-143
test_form.py
flask_admin/tests/test_form.py
+0
-143
No files found.
flask_admin/tests/test_form.py
deleted
100644 → 0
View file @
d2775fe3
import
os
import
os.path
as
op
from
StringIO
import
StringIO
from
nose.tools
import
eq_
,
ok_
from
flask
import
Flask
from
flask.ext.admin
import
form
,
helpers
def
_create_temp
():
path
=
op
.
join
(
op
.
dirname
(
__file__
),
'tmp'
)
if
not
op
.
exists
(
path
):
os
.
mkdir
(
path
)
return
path
def
safe_delete
(
path
,
name
):
try
:
os
.
remove
(
op
.
join
(
path
,
name
))
except
:
pass
def
test_upload_field
():
app
=
Flask
(
__name__
)
path
=
_create_temp
()
def
_remove_testfiles
():
safe_delete
(
path
,
'test1.txt'
)
safe_delete
(
path
,
'test2.txt'
)
class
TestForm
(
form
.
BaseForm
):
upload
=
form
.
FileUploadField
(
'Upload'
,
path
=
path
)
class
Dummy
(
object
):
pass
my_form
=
TestForm
()
eq_
(
my_form
.
upload
.
path
,
path
)
_remove_testfiles
()
dummy
=
Dummy
()
# Check upload
with
app
.
test_request_context
(
method
=
'POST'
,
data
=
{
'upload'
:
(
StringIO
(
'Hello World'
),
'test1.txt'
)}):
my_form
=
TestForm
(
helpers
.
get_form_data
())
ok_
(
my_form
.
validate
())
my_form
.
populate_obj
(
dummy
)
eq_
(
dummy
.
upload
,
'test1.txt'
)
ok_
(
op
.
exists
(
op
.
join
(
path
,
'test1.txt'
)))
# Check replace
with
app
.
test_request_context
(
method
=
'POST'
,
data
=
{
'upload'
:
(
StringIO
(
'Hello World'
),
'test2.txt'
)}):
my_form
=
TestForm
(
helpers
.
get_form_data
())
ok_
(
my_form
.
validate
())
my_form
.
populate_obj
(
dummy
)
eq_
(
dummy
.
upload
,
'test2.txt'
)
ok_
(
not
op
.
exists
(
op
.
join
(
path
,
'test1.txt'
)))
ok_
(
op
.
exists
(
op
.
join
(
path
,
'test2.txt'
)))
# Check delete
with
app
.
test_request_context
(
method
=
'POST'
,
data
=
{
'_upload-delete'
:
'checked'
}):
my_form
=
TestForm
(
helpers
.
get_form_data
())
ok_
(
my_form
.
validate
())
my_form
.
populate_obj
(
dummy
)
ok_
(
not
op
.
exists
(
op
.
join
(
path
,
'test2.txt'
)))
def
test_image_upload_field
():
app
=
Flask
(
__name__
)
path
=
_create_temp
()
def
_remove_testimages
():
safe_delete
(
path
,
'test1.png'
)
safe_delete
(
path
,
'test1_thumb.jpg'
)
safe_delete
(
path
,
'test2.png'
)
safe_delete
(
path
,
'test2_thumb.jpg'
)
class
TestForm
(
form
.
BaseForm
):
upload
=
form
.
ImageUploadField
(
'Upload'
,
path
=
path
,
thumbnail_size
=
(
100
,
100
,
True
))
class
Dummy
(
object
):
pass
my_form
=
TestForm
()
eq_
(
my_form
.
upload
.
path
,
path
)
eq_
(
my_form
.
upload
.
endpoint
,
'static'
)
_remove_testimages
()
dummy
=
Dummy
()
# Check upload
with
file
(
op
.
join
(
op
.
dirname
(
__file__
),
'data'
,
'copyleft.png'
),
'rb'
)
as
fp
:
with
app
.
test_request_context
(
method
=
'POST'
,
data
=
{
'upload'
:
(
fp
,
'test1.png'
)}):
my_form
=
TestForm
(
helpers
.
get_form_data
())
ok_
(
my_form
.
validate
())
my_form
.
populate_obj
(
dummy
)
eq_
(
dummy
.
upload
,
'test1.png'
)
ok_
(
op
.
exists
(
op
.
join
(
path
,
'test1.png'
)))
ok_
(
op
.
exists
(
op
.
join
(
path
,
'test1_thumb.jpg'
)))
# Check replace
with
file
(
op
.
join
(
op
.
dirname
(
__file__
),
'data'
,
'copyleft.png'
),
'rb'
)
as
fp
:
with
app
.
test_request_context
(
method
=
'POST'
,
data
=
{
'upload'
:
(
fp
,
'test2.png'
)}):
my_form
=
TestForm
(
helpers
.
get_form_data
())
ok_
(
my_form
.
validate
())
my_form
.
populate_obj
(
dummy
)
eq_
(
dummy
.
upload
,
'test2.png'
)
ok_
(
op
.
exists
(
op
.
join
(
path
,
'test2.png'
)))
ok_
(
op
.
exists
(
op
.
join
(
path
,
'test2_thumb.jpg'
)))
ok_
(
not
op
.
exists
(
op
.
join
(
path
,
'test1.png'
)))
ok_
(
not
op
.
exists
(
op
.
join
(
path
,
'test1_thumb.jpg'
)))
# Check delete
with
app
.
test_request_context
(
method
=
'POST'
,
data
=
{
'_upload-delete'
:
'checked'
}):
my_form
=
TestForm
(
helpers
.
get_form_data
())
ok_
(
my_form
.
validate
())
my_form
.
populate_obj
(
dummy
)
ok_
(
not
op
.
exists
(
op
.
join
(
path
,
'test2.png'
)))
ok_
(
not
op
.
exists
(
op
.
join
(
path
,
'test2_thumb.jpg'
)))
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