Media browser for Django.
Setup
By default mediabrowser uses easy-thumbnails
for creating image thumbnails.
In settings.py:
INSTALLED_APPS = (
...
'easy_thumbnails',
'mediabrowser',
...
)
MEDIABROWSER_UPLOAD_TO = "mb/%Y/%m"
MEDIABROWSER_PAGE_SELECTOR_URL = "my-cms-url-content-selector-name"
MEDIABROWSER_USER_PASSES_TEST = lambda user:user.is_authenticated
MEDIABROWSER_CHECK_USER_PERMISSIONS = True
MEDIABROWSER_MAX_IMAGE_SIZE = (800, 400)
In urls.py:
urlpatterns = patterns('',
...
url(r'', include('mediabrowser.urls')),
...
)
After having added mediabrowser
to INSTALLED_APPS
run ./manage.py syncdb
.
Controlling mediabrowser access
By default full media browser access is allowed for any authenticated staff user
(i.e., user.is_staff == True
). You can use the following settings to refine user access
rules:
Set MEDIABROWSER_USER_PASSES_TEST
. It should be a callable that takes user object as argument
and returns Boolean. It is a single access control option for uploading, browsing and deletion. Example:
MEDIABROWSER_USER_PASSES_TEST = lambda user: user.has_perm("mycms.change_content")
Set MEDIABROWSER_CHECK_USER_PERMISSIONS = True
. This will ensure user must have
explicit permissions to delete or add assets.
Integrating your CMS content browser
To integrate mediabrowser with your custom CMS to be able to select your CMS's content
to link to, do the following:
- Define your own view listing content.
- Set
MEDIABROWSER_PAGE_SELECTOR_URL
to url name of this view. - Make your view's template inherit from
mediabrowser/base.html
.
(Required context variables: asset_type=doc, page_selector_url=your_url)
You CMS integration view can be created by subclassing
mediabrowser.views.BaseAssetListView
. But you can also create your view from scratch.
Just pass the following context to the template:
context = {
'QUERY_STRING': self.request.GET.urlencode(),
"asset_type": "doc",
"page_selector_url": settings.MEDIABROWSER_PAGE_SELECTOR_URL
}
Customizing appearance
To override CSS definitions create your own mediabrowser/includes/css.html
and include your own CSS.
By default mediabrowser uses easy-thumbnails. If you
would like to use your own thumbnailing engine override mediabrowser/includes/asset-listing.html
.
WYSIWYG integration
CKEditor
In Django template:
<script>
CKEDITOR.config.filebrowserImageBrowseUrl = "{% url 'mediabrowser-add-image' %}";
CKEDITOR.config.filebrowserLinkBrowseUrl = "{% url 'mediabrowser-add-document' %}";
</script>
For details see CKEditor documentation.
Custom integration
MEDIABROWSER.insertFile = function(asset_url) {
}
This can be done inside your own mediabrowser/includes/head.html
include file.
Note that you don't need to close editor window from your custom fuction. This will be done
automatically after your funciton is executed.