Commit ccfa163f authored by Serge S. Koval's avatar Serge S. Koval

Merge pull request #423 from iurisilvio/remove_base_url

Make FileAdmin.base_url optional.
parents d0ca5f28 a26751d2
...@@ -7,7 +7,7 @@ import shutil ...@@ -7,7 +7,7 @@ import shutil
from operator import itemgetter from operator import itemgetter
from werkzeug import secure_filename from werkzeug import secure_filename
from flask import flash, url_for, redirect, abort, request from flask import flash, url_for, redirect, abort, request, send_from_directory
from wtforms import fields, validators from wtforms import fields, validators
...@@ -64,13 +64,12 @@ class FileAdmin(BaseView, ActionsMixin): ...@@ -64,13 +64,12 @@ class FileAdmin(BaseView, ActionsMixin):
""" """
Simple file-management interface. Simple file-management interface.
Requires two parameters:
:param path: :param path:
Path to the directory which will be managed Path to the directory which will be managed
:param url: :param base_url:
Base URL for the directory. Will be used to generate Optional base URL for the directory. Will be used to generate
static links to the files. static links to the files. If not defined, a route will be created
to serve uploaded files.
Sample usage:: Sample usage::
...@@ -86,6 +85,11 @@ class FileAdmin(BaseView, ActionsMixin): ...@@ -86,6 +85,11 @@ class FileAdmin(BaseView, ActionsMixin):
Is file upload allowed. Is file upload allowed.
""" """
can_download = True
"""
Is file download allowed.
"""
can_delete = True can_delete = True
""" """
Is file deletion allowed. Is file deletion allowed.
...@@ -151,7 +155,7 @@ class FileAdmin(BaseView, ActionsMixin): ...@@ -151,7 +155,7 @@ class FileAdmin(BaseView, ActionsMixin):
Edit template Edit template
""" """
def __init__(self, base_path, base_url, def __init__(self, base_path, base_url=None,
name=None, category=None, endpoint=None, url=None, name=None, category=None, endpoint=None, url=None,
verify_path=True): verify_path=True):
""" """
...@@ -310,10 +314,10 @@ class FileAdmin(BaseView, ActionsMixin): ...@@ -310,10 +314,10 @@ class FileAdmin(BaseView, ActionsMixin):
Static file path Static file path
""" """
if self.is_file_editable(path): if self.is_file_editable(path):
return url_for(".edit", path=path) route = '.edit'
else: else:
base_url = self.get_base_url() route = '.download'
return urljoin(base_url, path) return url_for(route, path=path)
def _normalize_path(self, path): def _normalize_path(self, path):
""" """
...@@ -503,6 +507,27 @@ class FileAdmin(BaseView, ActionsMixin): ...@@ -503,6 +507,27 @@ class FileAdmin(BaseView, ActionsMixin):
return self.render(self.upload_template, form=form) return self.render(self.upload_template, form=form)
@expose('/download/<path:path>')
def download(self, path=None):
"""
Download view method.
:param path:
File path.
"""
if not self.can_download:
abort(404)
base_path, directory, path = self._normalize_path(path)
# backward compatibility with base_url
base_url = self.get_base_url()
if base_url:
base_url = urljoin(url_for('.index'), base_url)
return redirect(urljoin(base_url, path))
return send_from_directory(base_path, path)
@expose('/mkdir/', methods=('GET', 'POST')) @expose('/mkdir/', methods=('GET', 'POST'))
@expose('/mkdir/<path:path>', methods=('GET', 'POST')) @expose('/mkdir/<path:path>', methods=('GET', 'POST'))
def mkdir(self, path=None): def mkdir(self, path=None):
......
...@@ -83,7 +83,11 @@ ...@@ -83,7 +83,11 @@
</td> </td>
{% else %} {% else %}
<td> <td>
{% if admin_view.can_download %}
<a href="{{ get_file_url(path)|safe }}">{{ name }}</a> <a href="{{ get_file_url(path)|safe }}">{{ name }}</a>
{% else %}
{{ name }}
{% endif %}
</td> </td>
<td> <td>
{{ size }} {{ size }}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment