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
af11b72e
Commit
af11b72e
authored
Jul 30, 2018
by
Clemens Wolff
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Enable edit-file for non-local file storages
parent
2bfb6b9c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
24 additions
and
4 deletions
+24
-4
__init__.py
flask_admin/contrib/fileadmin/__init__.py
+16
-4
s3.py
flask_admin/contrib/fileadmin/s3.py
+8
-0
No files found.
flask_admin/contrib/fileadmin/__init__.py
View file @
af11b72e
...
...
@@ -107,6 +107,20 @@ class LocalFileStorage(object):
"""
return
send_file
(
file_path
)
def
read_file
(
self
,
path
):
"""
Reads the content of the file located at `file_path`.
"""
with
open
(
path
,
'rb'
)
as
f
:
return
f
.
read
()
def
write_file
(
self
,
path
,
content
):
"""
Writes `content` to the file located at `file_path`.
"""
with
open
(
path
,
'w'
)
as
f
:
return
f
.
write
(
content
)
def
save_file
(
self
,
path
,
file_data
):
"""
Save uploaded file to the disk
...
...
@@ -1118,8 +1132,7 @@ class BaseFileAdmin(BaseView, ActionsMixin):
form
.
process
(
request
.
form
,
content
=
''
)
if
form
.
validate
():
try
:
with
open
(
full_path
,
'w'
)
as
f
:
f
.
write
(
request
.
form
[
'content'
])
self
.
storage
.
write_file
(
full_path
,
request
.
form
[
'content'
])
except
IOError
:
flash
(
gettext
(
"Error saving changes to
%(name)
s."
,
name
=
path
),
'error'
)
error
=
True
...
...
@@ -1131,8 +1144,7 @@ class BaseFileAdmin(BaseView, ActionsMixin):
helpers
.
flash_errors
(
form
,
message
=
'Failed to edit file.
%(error)
s'
)
try
:
with
open
(
full_path
,
'rb'
)
as
f
:
content
=
f
.
read
()
content
=
self
.
storage
.
read_file
(
full_path
)
except
IOError
:
flash
(
gettext
(
"Error reading
%(name)
s."
,
name
=
path
),
'error'
)
error
=
True
...
...
flask_admin/contrib/fileadmin/s3.py
View file @
af11b72e
...
...
@@ -166,6 +166,14 @@ class S3Storage(object):
keys
=
self
.
_get_path_keys
(
path
+
self
.
separator
)
return
len
(
keys
)
==
1
def
read_file
(
self
,
path
):
key
=
Key
(
self
.
bucket
,
path
)
return
key
.
get_contents_as_string
()
def
write_file
(
self
,
path
,
content
):
key
=
Key
(
self
.
bucket
,
path
)
key
.
set_contents_from_file
(
content
)
class
S3FileAdmin
(
BaseFileAdmin
):
"""
...
...
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