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
72cce208
Commit
72cce208
authored
Mar 01, 2015
by
Paul Brown
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add tests for fileadmin, minor formatting fixes
parent
b22b3dd1
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
117 additions
and
12 deletions
+117
-12
fileadmin.py
flask_admin/contrib/fileadmin.py
+7
-7
list.html
flask_admin/templates/bootstrap3/admin/file/list.html
+1
-1
test_fileadmin.py
flask_admin/tests/fileadmin/test_fileadmin.py
+109
-4
No files found.
flask_admin/contrib/fileadmin.py
View file @
72cce208
...
@@ -33,10 +33,10 @@ class FileAdmin(BaseView, ActionsMixin):
...
@@ -33,10 +33,10 @@ class FileAdmin(BaseView, ActionsMixin):
Sample usage::
Sample usage::
import os.path as op
import os.path as op
from flask.ext.admin import Admin
from flask.ext.admin import Admin
from flask.ext.admin.contrib.fileadmin import FileAdmin
from flask.ext.admin.contrib.fileadmin import FileAdmin
admin = Admin()
admin = Admin()
path = op.join(op.dirname(__file__), 'static')
path = op.join(op.dirname(__file__), 'static')
...
@@ -117,7 +117,7 @@ class FileAdmin(BaseView, ActionsMixin):
...
@@ -117,7 +117,7 @@ class FileAdmin(BaseView, ActionsMixin):
"""
"""
Edit template
Edit template
"""
"""
form_base_class
=
form
.
BaseForm
form_base_class
=
form
.
BaseForm
"""
"""
Base form class. Will be used to create the upload, rename, edit, and delete form.
Base form class. Will be used to create the upload, rename, edit, and delete form.
...
@@ -398,7 +398,7 @@ class FileAdmin(BaseView, ActionsMixin):
...
@@ -398,7 +398,7 @@ class FileAdmin(BaseView, ActionsMixin):
Werkzeug `FileStorage` object
Werkzeug `FileStorage` object
"""
"""
file_data
.
save
(
path
)
file_data
.
save
(
path
)
def
validate_form
(
self
,
form
):
def
validate_form
(
self
,
form
):
"""
"""
Validate the form on submit.
Validate the form on submit.
...
@@ -560,12 +560,12 @@ class FileAdmin(BaseView, ActionsMixin):
...
@@ -560,12 +560,12 @@ class FileAdmin(BaseView, ActionsMixin):
delete_form
=
self
.
delete_form
()
delete_form
=
self
.
delete_form
()
else
:
else
:
delete_form
=
None
delete_form
=
None
# Get path and verify if it is valid
# Get path and verify if it is valid
base_path
,
directory
,
path
=
self
.
_normalize_path
(
path
)
base_path
,
directory
,
path
=
self
.
_normalize_path
(
path
)
if
not
self
.
is_accessible_path
(
path
):
if
not
self
.
is_accessible_path
(
path
):
flash
(
gettext
(
'Permission denied.'
,
'error'
)
)
flash
(
gettext
(
'Permission denied.'
),
'error'
)
return
redirect
(
self
.
_get_dir_url
(
'.index'
))
return
redirect
(
self
.
_get_dir_url
(
'.index'
))
# Get directory listing
# Get directory listing
...
@@ -632,7 +632,7 @@ class FileAdmin(BaseView, ActionsMixin):
...
@@ -632,7 +632,7 @@ class FileAdmin(BaseView, ActionsMixin):
return
redirect
(
self
.
_get_dir_url
(
'.index'
,
path
))
return
redirect
(
self
.
_get_dir_url
(
'.index'
,
path
))
if
not
self
.
is_accessible_path
(
path
):
if
not
self
.
is_accessible_path
(
path
):
flash
(
gettext
(
'Permission denied.'
,
'error'
)
)
flash
(
gettext
(
'Permission denied.'
),
'error'
)
return
redirect
(
self
.
_get_dir_url
(
'.index'
))
return
redirect
(
self
.
_get_dir_url
(
'.index'
))
form
=
self
.
upload_form
()
form
=
self
.
upload_form
()
...
...
flask_admin/templates/bootstrap3/admin/file/list.html
View file @
72cce208
...
@@ -57,7 +57,7 @@
...
@@ -57,7 +57,7 @@
{%- if admin_view.can_delete and path -%}
{%- if admin_view.can_delete and path -%}
{% if is_dir %}
{% if is_dir %}
{% if name != '..' and admin_view.can_delete_dirs %}
{% if name != '..' and admin_view.can_delete_dirs %}
<form
class=
"icon"
method=
"POST"
action=
"{{ get_url('.delete') }}"
>
<form
class=
"icon"
method=
"POST"
action=
"{{ get_url('.delete') }}"
>
{{ delete_form.path(value=path) }}
{{ delete_form.path(value=path) }}
{{ delete_form.csrf_token }}
{{ delete_form.csrf_token }}
<button
onclick=
"return confirm('{{ _gettext('Are you sure you want to delete \\\'%(name)s\\\' recursively?', name=name) }}')"
>
<button
onclick=
"return confirm('{{ _gettext('Are you sure you want to delete \\\'%(name)s\\\' recursively?', name=name) }}')"
>
...
...
flask_admin/tests/fileadmin/test_fileadmin.py
View file @
72cce208
from
nose.tools
import
eq_
,
ok_
import
os.path
as
op
import
os.path
as
op
from
nose.tools
import
eq_
,
ok_
from
flask.ext.admin.contrib
import
fileadmin
from
flask.ext.admin.contrib
import
fileadmin
from
.
import
setup
from
.
import
setup
try
:
from
StringIO
import
StringIO
except
ImportError
:
from
io
import
StringIO
def
create_view
():
def
create_view
():
app
,
admin
=
setup
()
app
,
admin
=
setup
()
class
MyFileAdmin
(
fileadmin
.
FileAdmin
):
editable_extensions
=
(
'txt'
,)
path
=
op
.
join
(
op
.
dirname
(
__file__
),
'files'
)
path
=
op
.
join
(
op
.
dirname
(
__file__
),
'files'
)
view
=
fileadmin
.
FileAdmin
(
path
,
'/files/'
,
name
=
'Files'
)
view
=
My
FileAdmin
(
path
,
'/files/'
,
name
=
'Files'
)
admin
.
add_view
(
view
)
admin
.
add_view
(
view
)
return
app
,
admin
,
view
return
app
,
admin
,
view
...
@@ -21,8 +30,104 @@ def test_file_admin():
...
@@ -21,8 +30,104 @@ def test_file_admin():
client
=
app
.
test_client
()
client
=
app
.
test_client
()
rv
=
client
.
get
(
'/admin/fileadmin/'
)
# index
rv
=
client
.
get
(
'/admin/myfileadmin/'
)
eq_
(
rv
.
status_code
,
200
)
ok_
(
'path=dummy.txt'
in
rv
.
data
.
decode
(
'utf-8'
))
# edit
rv
=
client
.
get
(
'/admin/myfileadmin/edit/?path=dummy.txt'
)
eq_
(
rv
.
status_code
,
200
)
ok_
(
'dummy.txt'
in
rv
.
data
.
decode
(
'utf-8'
))
rv
=
client
.
post
(
'/admin/myfileadmin/edit/?path=dummy.txt'
,
data
=
dict
(
content
=
'new_string'
))
eq_
(
rv
.
status_code
,
302
)
rv
=
client
.
get
(
'/admin/myfileadmin/edit/?path=dummy.txt'
)
eq_
(
rv
.
status_code
,
200
)
eq_
(
rv
.
status_code
,
200
)
ok_
(
'dummy.txt'
in
rv
.
data
.
decode
(
'utf-8'
))
ok_
(
'dummy.txt'
in
rv
.
data
.
decode
(
'utf-8'
))
ok_
(
'new_string'
in
rv
.
data
.
decode
(
'utf-8'
))
# rename
rv
=
client
.
get
(
'/admin/myfileadmin/rename/?path=dummy.txt'
)
eq_
(
rv
.
status_code
,
200
)
ok_
(
'dummy.txt'
in
rv
.
data
.
decode
(
'utf-8'
))
rv
=
client
.
post
(
'/admin/myfileadmin/rename/?path=dummy.txt'
,
data
=
dict
(
name
=
'dummy_renamed.txt'
,
path
=
'dummy.txt'
))
eq_
(
rv
.
status_code
,
302
)
rv
=
client
.
get
(
'/admin/myfileadmin/'
)
eq_
(
rv
.
status_code
,
200
)
ok_
(
'path=dummy_renamed.txt'
in
rv
.
data
.
decode
(
'utf-8'
))
ok_
(
'path=dummy.txt'
not
in
rv
.
data
.
decode
(
'utf-8'
))
# upload
rv
=
client
.
get
(
'/admin/myfileadmin/upload/'
)
eq_
(
rv
.
status_code
,
200
)
# TODO: Check actions, etc
rv
=
client
.
post
(
'/admin/myfileadmin/upload/'
,
data
=
dict
(
upload
=
(
StringIO
(
""
),
'dummy.txt'
),
))
eq_
(
rv
.
status_code
,
302
)
rv
=
client
.
get
(
'/admin/myfileadmin/'
)
eq_
(
rv
.
status_code
,
200
)
ok_
(
'path=dummy.txt'
in
rv
.
data
.
decode
(
'utf-8'
))
ok_
(
'path=dummy_renamed.txt'
in
rv
.
data
.
decode
(
'utf-8'
))
# delete
rv
=
client
.
post
(
'/admin/myfileadmin/delete/'
,
data
=
dict
(
path
=
'dummy_renamed.txt'
))
eq_
(
rv
.
status_code
,
302
)
rv
=
client
.
get
(
'/admin/myfileadmin/'
)
eq_
(
rv
.
status_code
,
200
)
ok_
(
'path=dummy_renamed.txt'
not
in
rv
.
data
.
decode
(
'utf-8'
))
ok_
(
'path=dummy.txt'
in
rv
.
data
.
decode
(
'utf-8'
))
# mkdir
rv
=
client
.
get
(
'/admin/myfileadmin/mkdir/'
)
eq_
(
rv
.
status_code
,
200
)
rv
=
client
.
post
(
'/admin/myfileadmin/mkdir/'
,
data
=
dict
(
name
=
'dummy_dir'
))
eq_
(
rv
.
status_code
,
302
)
rv
=
client
.
get
(
'/admin/myfileadmin/'
)
eq_
(
rv
.
status_code
,
200
)
ok_
(
'path=dummy.txt'
in
rv
.
data
.
decode
(
'utf-8'
))
ok_
(
'path=dummy_dir'
in
rv
.
data
.
decode
(
'utf-8'
))
# rename - directory
rv
=
client
.
get
(
'/admin/myfileadmin/rename/?path=dummy_dir'
)
eq_
(
rv
.
status_code
,
200
)
ok_
(
'dummy_dir'
in
rv
.
data
.
decode
(
'utf-8'
))
rv
=
client
.
post
(
'/admin/myfileadmin/rename/?path=dummy_dir'
,
data
=
dict
(
name
=
'dummy_renamed_dir'
,
path
=
'dummy_dir'
))
eq_
(
rv
.
status_code
,
302
)
rv
=
client
.
get
(
'/admin/myfileadmin/'
)
eq_
(
rv
.
status_code
,
200
)
ok_
(
'path=dummy_renamed_dir'
in
rv
.
data
.
decode
(
'utf-8'
))
ok_
(
'path=dummy_dir'
not
in
rv
.
data
.
decode
(
'utf-8'
))
# delete - directory
rv
=
client
.
post
(
'/admin/myfileadmin/delete/'
,
data
=
dict
(
path
=
'dummy_renamed_dir'
))
eq_
(
rv
.
status_code
,
302
)
rv
=
client
.
get
(
'/admin/myfileadmin/'
)
eq_
(
rv
.
status_code
,
200
)
ok_
(
'path=dummy_renamed_dir'
not
in
rv
.
data
.
decode
(
'utf-8'
))
ok_
(
'path=dummy.txt'
in
rv
.
data
.
decode
(
'utf-8'
))
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