Commit e146f3ff authored by Arthur Bressan's avatar Arthur Bressan

Renames FileAdmin to BaseFileAdmin and creates a FileAdmin with a default local storage

parent 565f9e43
...@@ -21,6 +21,12 @@ from flask_admin.babel import gettext, lazy_gettext ...@@ -21,6 +21,12 @@ from flask_admin.babel import gettext, lazy_gettext
class LocalFileStorage(object): class LocalFileStorage(object):
def __init__(self, base_path): def __init__(self, base_path):
"""
Constructor.
:param base_path:
Base file storage location
"""
self.base_path = as_unicode(base_path) self.base_path = as_unicode(base_path)
self.separator = os.sep self.separator = os.sep
...@@ -114,29 +120,7 @@ class LocalFileStorage(object): ...@@ -114,29 +120,7 @@ class LocalFileStorage(object):
file_data.save(path) file_data.save(path)
class FileAdmin(BaseView, ActionsMixin): class BaseFileAdmin(BaseView, ActionsMixin):
"""
Simple file-management interface.
:param path:
Path to the directory which will be managed
:param base_url:
Optional base URL for the directory. Will be used to generate
static links to the files. If not defined, a route will be created
to serve uploaded files.
Sample usage::
import os.path as op
from flask_admin import Admin
from flask_admin.contrib.fileadmin import FileAdmin
admin = Admin()
path = op.join(op.dirname(__file__), 'static')
admin.add_view(FileAdmin(path, '/static/', name='Static Files'))
"""
can_upload = True can_upload = True
""" """
...@@ -264,15 +248,12 @@ class FileAdmin(BaseView, ActionsMixin): ...@@ -264,15 +248,12 @@ class FileAdmin(BaseView, ActionsMixin):
edit_modal = False edit_modal = False
"""Setting this to true will display the edit view as a modal dialog.""" """Setting this to true will display the edit view as a modal dialog."""
def __init__(self, base_path, base_url=None, def __init__(self, base_url=None, name=None, category=None, endpoint=None,
name=None, category=None, endpoint=None, url=None, url=None, verify_path=True, menu_class_name=None,
verify_path=True, menu_class_name=None, menu_icon_type=None, menu_icon_value=None, menu_icon_type=None, menu_icon_value=None, storage=None):
storage=LocalFileStorage, storage_args=None):
""" """
Constructor. Constructor.
:param base_path:
Base file storage location
:param base_url: :param base_url:
Base URL for the files Base URL for the files
:param name: :param name:
...@@ -286,13 +267,11 @@ class FileAdmin(BaseView, ActionsMixin): ...@@ -286,13 +267,11 @@ class FileAdmin(BaseView, ActionsMixin):
:param verify_path: :param verify_path:
Verify if path exists. If set to `True` and path does not exist Verify if path exists. If set to `True` and path does not exist
will raise an exception. will raise an exception.
:param storage:
The storage backend that the `BaseFileAdmin` will use to operate on the files.
""" """
if storage_args is None:
storage_args = {}
storage_args.setdefault('base_path', base_path)
self.base_url = base_url self.base_url = base_url
self.storage = storage(**storage_args) self.storage = storage
self.init_actions() self.init_actions()
...@@ -308,9 +287,10 @@ class FileAdmin(BaseView, ActionsMixin): ...@@ -308,9 +287,10 @@ class FileAdmin(BaseView, ActionsMixin):
not isinstance(self.editable_extensions, set)): not isinstance(self.editable_extensions, set)):
self.editable_extensions = set(self.editable_extensions) self.editable_extensions = set(self.editable_extensions)
super(FileAdmin, self).__init__(name, category, endpoint, url, super(BaseFileAdmin, self).__init__(name, category, endpoint, url,
menu_class_name=menu_class_name, menu_icon_type=menu_icon_type, menu_class_name=menu_class_name,
menu_icon_value=menu_icon_value) menu_icon_type=menu_icon_type,
menu_icon_value=menu_icon_value)
def is_accessible_path(self, path): def is_accessible_path(self, path):
""" """
...@@ -1090,3 +1070,32 @@ class FileAdmin(BaseView, ActionsMixin): ...@@ -1090,3 +1070,32 @@ class FileAdmin(BaseView, ActionsMixin):
@action('edit', lazy_gettext('Edit')) @action('edit', lazy_gettext('Edit'))
def action_edit(self, items): def action_edit(self, items):
return redirect(self.get_url('.edit', path=items)) return redirect(self.get_url('.edit', path=items))
class FileAdmin(BaseFileAdmin):
"""
Simple file-management interface.
:param base_path:
Path to the directory which will be managed
:param base_url:
Optional base URL for the directory. Will be used to generate
static links to the files. If not defined, a route will be created
to serve uploaded files.
Sample usage::
import os.path as op
from flask_admin import Admin
from flask_admin.contrib.fileadmin import FileAdmin
admin = Admin()
path = op.join(op.dirname(__file__), 'static')
admin.add_view(FileAdmin(path, '/static/', name='Static Files'))
"""
def __init__(self, base_path, *args, **kwargs):
storage = LocalFileStorage(base_path)
super(FileAdmin, self).__init__(*args, storage=storage, **kwargs)
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