1. Kevin Chan
  2. django-filebrowser-no-grappelli-and-uploadify

Commits

Kevin Chan  committed 4fe4ed6

Updated templates and views to work with Django 1.4.

  • Participants
  • Parent commits 66701ba
  • Branches master

Comments (0)

Files changed (3)

File templates/filebrowser/include/search.html

View file
  • Ignore whitespace
 <div id="toolbar">
     <form id="changelist-search" action="." method="get">
         <div><!-- DIV needed for valid HTML -->
-        <label for="searchbar"><img src="{% admin_media_prefix %}img/admin/icon_searchbox.png" alt="Search" /></label>
+        <label for="searchbar"><img src="{% admin_media_prefix %}img/icon_searchbox.png" alt="Search" /></label>
         <input type="text" size="40" name="q" value="{{ q }}" id="searchbar" />
         {% if query.filter_type %}<input type="hidden" name="filter_type" value="{{ query.filter_type }}" />{% endif %}
         {% if query.filter_date %}<input type="hidden" name="filter_date" value="{{ query.filter_date }}" />{% endif %}
         </div>
     </form>
 </div>
-<script type="text/javascript">document.getElementById("searchbar").focus();</script>
+<script type="text/javascript">document.getElementById("searchbar").focus();</script>

File templates/filebrowser/include/toolbar.html

View file
  • Ignore whitespace
 <div id="toolbar">
   <form id="changelist-search" action="." method="get">
     <div><!-- DIV needed for valid HTML -->
-      <label for="searchbar"><img alt="Search" src="{% admin_media_prefix %}img/admin/icon_searchbox.png"></label>
+      <label for="searchbar"><img alt="Search" src="{% admin_media_prefix %}img/icon_searchbox.png"></label>
       <input type="text" name="q" value="{{ query.q }}" id="searchbar" />
       {% if query.filter_type %}<input type="hidden" name="filter_type" value="{{ query.filter_type }}" />{% endif %}
       {% if query.filter_date %}<input type="hidden" name="filter_date" value="{{ query.filter_date }}" />{% endif %}
     </div>
   </form>
 </div>
-<script type="text/javascript">document.getElementById("searchbar").focus();</script>
+<script type="text/javascript">document.getElementById("searchbar").focus();</script>

File views.py

View file
  • Ignore whitespace
     filter_re.append(re.compile(exp))
 
 
+# local helper message function
+
+try:
+    from django.contrib import messages
+    HAS_MESSAGES_FRAMEWORK = True
+    INFO = messages.INFO
+    DEBUG = messages.DEBUG
+    SUCCESS = messages.SUCCESS
+    WARNING = messages.WARNING
+    ERROR = messages.ERROR
+except ImportError:
+    HAS_MESSAGES_FRAMEWORK = False
+    DEBUG = 10
+    INFO = 20
+    SUCCESS = 25
+    WARNING = 30
+    ERROR = 40
+
+DEFAULT_MSG_LEVEL = INFO
+
+def _message(request, msg, level=DEFAULT_MSG_LEVEL):
+    """
+    Helper function to send message to user.
+    * uses messages framework if exists (for Django >= 1.2)
+    """
+    if HAS_MESSAGES_FRAMEWORK:
+        msglevels = {
+            DEBUG: messages.debug,
+            INFO: messages.info,
+            SUCCESS: messages.success,
+            WARNING: messages.warning,
+            ERROR: messages.error
+        }
+        try:
+            _m = msglevels[level]
+        except:
+            _m = msglevels[DEFAULT_MSG_LEVEL]
+        _m(request, msg)
+    else:
+        request.user.message_set.create(message=msg)
+
+
+
 def browse(request):
     """
     Browse Files/Directories.
     """
-    
+
     # QUERY / PATH CHECK
     query = request.GET.copy()
     path = get_path(query.get('dir', ''))
     directory = get_path('')
-    
+
     if path is None:
         msg = _('The requested Folder does not exist.')
-        request.user.message_set.create(message=msg)
+        _message(request, msg, level=ERROR)
         if directory is None:
             # The DIRECTORY does not exist, raise an error to prevent eternal redirecting.
             raise ImproperlyConfigured, _("Error finding Upload-Folder. Maybe it does not exist?")
         redirect_url = reverse("fb_browse") + query_helper(query, "", "dir")
         return HttpResponseRedirect(redirect_url)
     abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path)
-    
+
     # INITIAL VARIABLES
     results_var = {'results_total': 0, 'results_current': 0, 'delete_total': 0, 'images_total': 0, 'select_total': 0 }
     counter = {}
     for k,v in EXTENSIONS.iteritems():
         counter[k] = 0
-    
+
     dir_list = os.listdir(abs_path)
     files = []
     for file in dir_list:
-        
+
         # EXCLUDE FILES MATCHING VERSIONS_PREFIX OR ANY OF THE EXCLUDE PATTERNS
         filtered = file.startswith('.')
         for re_prefix in filter_re:
         if filtered:
             continue
         results_var['results_total'] += 1
-        
+
         # CREATE FILEOBJECT
         fileobject = FileObject(os.path.join(DIRECTORY, path, file))
-        
+
         # FILTER / SEARCH
         append = False
         if fileobject.filetype == request.GET.get('filter_type', fileobject.filetype) and get_filterdate(request.GET.get('filter_date', ''), fileobject.date):
             append = True
         if request.GET.get('q') and not re.compile(request.GET.get('q').lower(), re.M).search(file.lower()):
             append = False
-        
+
         # APPEND FILE_LIST
         if append:
             try:
             else:
                 files.append(fileobject)
                 results_var['results_current'] += 1
-        
+
         # COUNTER/RESULTS
         if fileobject.filetype:
             counter[fileobject.filetype] += 1
-    
+
     # SORTING
     query['o'] = request.GET.get('o', DEFAULT_SORTING_BY)
     query['ot'] = request.GET.get('ot', DEFAULT_SORTING_ORDER)
     files = sort_by_attr(files, request.GET.get('o', DEFAULT_SORTING_BY))
     if not request.GET.get('ot') and DEFAULT_SORTING_ORDER == "desc" or request.GET.get('ot') == "desc":
         files.reverse()
-    
+
     p = Paginator(files, LIST_PER_PAGE)
     try:
         page_nr = request.GET.get('p', '1')
         page = p.page(page_nr)
     except (EmptyPage, InvalidPage):
         page = p.page(p.num_pages)
-    
+
     return render_to_response('filebrowser/index.html', {
         'dir': path,
         'p': p,
     """
     Make Directory.
     """
-    
+
     from filebrowser.forms import MakeDirForm
-    
+
     # QUERY / PATH CHECK
     query = request.GET
     path = get_path(query.get('dir', ''))
     if path is None:
         msg = _('The requested Folder does not exist.')
-        request.user.message_set.create(message=msg)
+        _message(request, msg, level=ERROR)
         return HttpResponseRedirect(reverse("fb_browse"))
     abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path)
-    
+
     if request.method == 'POST':
         form = MakeDirForm(abs_path, request.POST)
         if form.is_valid():
                 filebrowser_post_createdir.send(sender=request, path=path, dirname=form.cleaned_data['dir_name'])
                 # MESSAGE & REDIRECT
                 msg = _('The Folder %s was successfully created.') % (form.cleaned_data['dir_name'])
-                request.user.message_set.create(message=msg)
+                _message(request, msg, level=SUCCESS)
                 # on redirect, sort by date desc to see the new directory on top of the list
                 # remove filter in order to actually _see_ the new folder
                 # remove pagination
                     form.errors['dir_name'] = forms.util.ErrorList([_('Error creating folder.')])
     else:
         form = MakeDirForm(abs_path)
-    
+
     return render_to_response('filebrowser/makedir.html', {
         'form': form,
         'query': query,
     """
 
     from django.forms.formsets import formset_factory
-    
+
     # QUERY / PATH CHECK
     query = request.GET
     path = get_path(query.get('dir', ''))
     if path is None:
         msg = _('The requested Folder does not exist.')
-        request.user.message_set.create(message=msg)
+        _message(request, msg, level=ERROR)
         return HttpResponseRedirect(reverse("fb_browse"))
     abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path)
 
     ImageFile.MAXBLOCK = IMAGE_MAXBLOCK # default is 64k
 
     from filebrowser.forms import UploadForm, BaseUploadFormSet
-    
+
     UploadFormSet = formset_factory(UploadForm, formset=BaseUploadFormSet, extra=5)
     if request.method == 'POST':
         formset = UploadFormSet(data=request.POST, files=request.FILES, path=abs_path)
                     filebrowser_post_upload.send(sender=request, path=abs_path, file=uploadedfile)
             # MESSAGE & REDIRECT
             msg = _('Upload successful.')
-            request.user.message_set.create(message=msg)
+            _message(request, msg, level=SUCCESS)
             # on redirect, sort by date desc to see the uploaded files on top of the list
             redirect_url = reverse("fb_browse") + query_helper(query, "ot=desc,o=date", "ot,o")
             return HttpResponseRedirect(redirect_url)
         'breadcrumbs_title': _(u'Upload'),
         'title': _(u'Select files to upload'),
     }, context_instance=Context(request))
-    
+
 upload = staff_member_required(never_cache(upload))
 
 
 def delete(request):
     """
     Delete existing File/Directory.
-    
+
     When trying to delete a Directory, the Directory has to be empty.
     """
-    
+
     # QUERY / PATH CHECK
     query = request.GET
     path = get_path(query.get('dir', ''))
             msg = _('The requested Folder does not exist.')
         else:
             msg = _('The requested File does not exist.')
-        request.user.message_set.create(message=msg)
+        _message(request, msg, level=ERROR)
         return HttpResponseRedirect(reverse("fb_browse"))
     abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path)
-    
+
     msg = ""
     if request.GET:
         if request.GET.get('filetype') != "Folder":
                 filebrowser_post_delete.send(sender=request, path=path, filename=filename)
                 # MESSAGE & REDIRECT
                 msg = _('The file %s was successfully deleted.') % (filename.lower())
-                request.user.message_set.create(message=msg)
+                _message(request, msg, level=SUCCESS)
                 redirect_url = reverse("fb_browse") + query_helper(query, "", "filename,filetype")
                 return HttpResponseRedirect(redirect_url)
             except OSError:
                 filebrowser_post_delete.send(sender=request, path=path, filename=filename)
                 # MESSAGE & REDIRECT
                 msg = _('The folder %s was successfully deleted.') % (filename.lower())
-                request.user.message_set.create(message=msg)
+                _message(request, msg, level=SUCCESS)
                 redirect_url = reverse("fb_browse") + query_helper(query, "", "filename,filetype")
                 return HttpResponseRedirect(redirect_url)
             except OSError:
                 # todo: define error message
                 msg = OSError
-    
+
     if msg:
-        request.user.message_set.create(message=msg)
-    
+        _message(request, msg, level=INFO)
+
     return render_to_response('filebrowser/index.html', {
         'dir': dir_name,
         'file': request.GET.get('filename', ''),
 def rename(request):
     """
     Rename existing File/Directory.
-    
+
     Includes renaming existing Image Versions/Thumbnails.
     """
-    
+
     from filebrowser.forms import RenameForm
-    
+
     # QUERY / PATH CHECK
     query = request.GET
     path = get_path(query.get('dir', ''))
             msg = _('The requested Folder does not exist.')
         else:
             msg = _('The requested File does not exist.')
-        request.user.message_set.create(message=msg)
+        _message(request, msg, level=ERROR)
         return HttpResponseRedirect(reverse("fb_browse"))
     abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path)
     file_extension = os.path.splitext(filename)[1].lower()
-    
+
     if request.method == 'POST':
         form = RenameForm(abs_path, file_extension, request.POST)
         if form.is_valid():
                 filebrowser_post_rename.send(sender=request, path=path, filename=filename, new_filename=new_filename)
                 # MESSAGE & REDIRECT
                 msg = _('Renaming was successful.')
-                request.user.message_set.create(message=msg)
+                _message(request, msg, level=SUCCESS)
                 redirect_url = reverse("fb_browse") + query_helper(query, "", "filename")
                 return HttpResponseRedirect(redirect_url)
             except OSError, (errno, strerror):
                 form.errors['name'] = forms.util.ErrorList([_('Error.')])
     else:
         form = RenameForm(abs_path, file_extension)
-    
+
     return render_to_response('filebrowser/rename.html', {
         'form': form,
         'query': query,
     """
     Show all Versions for an Image according to ADMIN_VERSIONS.
     """
-    
+
     # QUERY / PATH CHECK
     query = request.GET
     path = get_path(query.get('dir', ''))
             msg = _('The requested Folder does not exist.')
         else:
             msg = _('The requested File does not exist.')
-        request.user.message_set.create(message=msg)
+        _message(request, msg, level=ERROR)
         return HttpResponseRedirect(reverse("fb_browse"))
     abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path)
-    
+
     return render_to_response('filebrowser/versions.html', {
         'original': path_to_url(os.path.join(DIRECTORY, path, filename)),
         'query': query,