Skip to content

Commit

Permalink
Merge pull request wardi#5 from vdboor/master
Browse files Browse the repository at this point in the history
General improvements for the filebrowser in the Django admin
  • Loading branch information
smacker committed Aug 15, 2014
2 parents 6de55ac + 55cb9b2 commit 011045a
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 5 deletions.
27 changes: 27 additions & 0 deletions filebrowser/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from django.conf.urls import patterns, url
from django.contrib import admin
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from .models import FileBrowser


class FileBrowserAdmin(admin.ModelAdmin):
actions = []

def has_add_permission(self, request):
return False

def has_delete_permission(self, request, obj=None):
return False

def get_urls(self):
info = self.model._meta.app_label, self.model._meta.module_name
return patterns('',
url('^$', self.admin_site.admin_view(self.filebrowser_view), name='{0}_{1}_changelist'.format(*info)),
)

def filebrowser_view(self, request):
return HttpResponseRedirect(reverse('filebrowser:fb_browse'))


admin.site.register(FileBrowser, FileBrowserAdmin)
2 changes: 2 additions & 0 deletions filebrowser/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from django.template.loader import render_to_string
from django.utils.six import with_metaclass
from django.utils.translation import ugettext_lazy as _
from django.contrib.admin.templatetags.admin_static import static

# FILEBROWSER IMPORTS
from filebrowser.settings import *
Expand Down Expand Up @@ -44,6 +45,7 @@ def render(self, name, value, attrs=None):
if value != "" and not isinstance(value, FileObject):
value = FileObject(value, site=self.site)
final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
final_attrs['search_icon'] = static('filebrowser/img/filebrowser_icon_show.gif')
final_attrs['url'] = url
final_attrs['directory'] = self.directory
final_attrs['extensions'] = self.extensions
Expand Down
10 changes: 9 additions & 1 deletion filebrowser/models.py
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
# This file is only necessary for the tests to work
from django.db import models
from django.utils.translation import ugettext_lazy as _


class FileBrowser(models.Model):
class Meta:
managed = False
verbose_name = _("FileBrowser")
verbose_name_plural = _("FileBrowser")
3 changes: 2 additions & 1 deletion filebrowser/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# django imports
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
import re

# PATH AND URL SETTINGS

Expand Down Expand Up @@ -77,7 +78,7 @@
# Default is to exclude 'thumbnail' style naming of image-thumbnails.
EXTENSION_LIST = []
for exts in EXTENSIONS.values():
EXTENSION_LIST += exts
EXTENSION_LIST += [re.escape(ext) for ext in exts if ext]
EXCLUDE = getattr(settings, 'FILEBROWSER_EXCLUDE', (r'_(%(exts)s)_.*_q\d{1,3}\.(%(exts)s)' % {'exts': ('|'.join(EXTENSION_LIST))},))
# Max. Upload Size in Bytes.
MAX_UPLOAD_SIZE = getattr(settings, "FILEBROWSER_MAX_UPLOAD_SIZE", 10485760)
Expand Down
3 changes: 2 additions & 1 deletion filebrowser/sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.utils.encoding import smart_text
from django.contrib import messages
from django.views.decorators.clickjacking import xframe_options_sameorigin
from django.views.decorators.csrf import csrf_exempt
from django.core.files.storage import DefaultStorage, default_storage, FileSystemStorage

Expand Down Expand Up @@ -172,7 +173,7 @@ def handle_file_upload(path, file, site):

def filebrowser_view(view):
"Only let staff browse the files"
return staff_member_required(never_cache(view))
return staff_member_required(never_cache(xframe_options_sameorigin(view)))


class FileBrowserSite(object):
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 4 additions & 2 deletions filebrowser/templates/filebrowser/custom_field.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{% load i18n fb_versions %}
<input id="{{ final_attrs.id }}" type="text" class="vFileBrowseField{% if final_attrs.class %} {{ final_attrs.class }}{% endif %}" name="{{ final_attrs.name }}" value="{{ value.path }}" />
<a href="javascript:FileBrowser.show('{{ final_attrs.id }}', '{{ url }}?pop=1{% if final_attrs.directory %}&amp;dir={{ final_attrs.directory|urlencode|urlencode }}{% endif %}{% if final_attrs.format %}&amp;type={{ final_attrs.format }}{% endif %}');" class="fb_show">{% trans "Change" %}</a>
<a href="#" onclick="FileBrowser.show(this.parentNode.getElementsByTagName('input')[0].id, '{{ url }}?pop=1{% if final_attrs.directory %}&amp;dir={{ final_attrs.directory|urlencode|urlencode }}{% endif %}{% if final_attrs.format %}&amp;type={{ final_attrs.format }}{% endif %}');" class="fb_show">
<img src="{{ final_attrs.search_icon }}" alt="{% trans "Change" %}" />
</a>
{% if value.filetype == "Image" and value.exists %}
{% version_object value.path final_attrs.ADMIN_THUMBNAIL as thumbnail_version %}
{% if thumbnail_version %}
Expand All @@ -25,4 +27,4 @@
{% endif %}
{% if value and not value.exists %}
<ul class="errorlist"><li>{% trans "File not found" %}</li></ul>
{% endif %}
{% endif %}

0 comments on commit 011045a

Please sign in to comment.