Commits

Guilherme Gondim committed 15d8b8f

zip_upload view improvements and refactoring

Comments (0)

Files changed (5)

 from django import forms
+from django import http
+from django.conf.urls import patterns, url
 from django.contrib import admin
+from django.core.urlresolvers import reverse
 from django.db import models
+from django.template.response import TemplateResponse
+from django.utils.encoding import force_unicode
 from django.utils.translation import ugettext_lazy as _
 from imagekit.admin import AdminThumbnail
 from mptt.admin import MPTTModelAdmin
 
-from galeria.forms import AlbumForm
+from galeria.forms import AlbumForm, ZipUploadForm
 from galeria.models import Album, Picture
 
 
     )
     thumbnail.short_description = _('thumbnail')
 
+    def get_urls(self):
+        info = self.model._meta.app_label, self.model._meta.module_name
+        default_patterns = super(PictureAdmin, self).get_urls()
+        custom_patterns = patterns('', url(
+            r'^zip_upload/$',
+            self.admin_site.admin_view(self.zip_upload_view),
+            name='%s_%s_zip_upload' % info
+        ))
+        return custom_patterns + default_patterns
+
+    def zip_upload_view(self, request, template='galeria/admin/zip_upload_form.html'):
+        model = self.model
+        opts = model._meta
+        form = ZipUploadForm()
+
+        if request.method == 'POST':
+            form = ZipUploadForm(request.POST, request.FILES)
+            if form.is_valid():
+                album = form.cleaned_data['album']
+                picture_list = form.process_zip_archive()
+                model.objects.bulk_create(picture_list)
+
+                # Send user to album change view to edit the uploaded pictures
+                if request.user.has_perm('galeria.change_album', album):
+                    url_ = reverse('admin:galeria_album_change', args=[album.pk])
+                    return http.HttpResponseRedirect(url_ + '#pictures-group')
+                url_ = reverse('admin:galeria_picture_changelist')
+                return http.HttpResponseRedirect(url_)
+
+        context = {
+            'app_label': opts.app_label,
+            'has_add_permission': self.has_add_permission(request),
+            'has_change_permission': self.has_change_permission(request),
+            'module_name': force_unicode(opts.verbose_name_plural),
+            'opts': opts,
+            'title': _('ZIP Upload'),
+            'form': form,
+        }
+        return TemplateResponse(request, template, context, current_app=self.admin_site.name)
+
 admin.site.register(Picture, PictureAdmin)
 
 

galeria/templates/galeria/admin/change_list.html

 {% block object-tools-items %}
 {% if not is_popup %}
 <li>
-  <a href="{% url galeria-zip-upload %}" title="{% trans "Upload a ZIP archive with a set of pictures" %}">{% trans "Upload ZIP" %}</a>
+  <a href="{% url admin:galeria_picture_zip_upload %}"
+     title="{% trans "Upload a ZIP archive with a set of pictures" %}">{% trans "Upload ZIP" %}</a>
 </li>
 {% endif %}
 {{ block.super }}

galeria/templates/galeria/admin/zip_upload_form.html

 {% extends "admin/base_site.html" %}
 {% load i18n admin_static admin_modify %}
 {% load url from future %}
+{% load admin_urls %}
 
 {% block extrastyle %}{{ block.super }}
 <link rel="stylesheet" type="text/css" href="{% static "admin/css/forms.css" %}" />
 {% endblock %}
 
+{% block bodyclass %}{{ opts.app_label }} zip_upload_form{% endblock %}
+
 {% block breadcrumbs %}
 <div class="breadcrumbs">
 <a href="{% url 'admin:index' %}">{% trans 'Home' %}</a>
-&rsaquo; <a href="{% url 'admin:app_list' app_label='galeria' %}">Galeria</a>
+&rsaquo; <a href="{% url 'admin:app_list' app_label=opts.app_label %}"> {{ app_label|capfirst|escape }}</a>
+&rsaquo; {% if has_change_permission %}<a href="{% url opts|admin_urlname:'changelist' %}">{{ opts.verbose_name_plural|capfirst }}</a>{% else %}{{ opts.verbose_name_plural|capfirst }}{% endif %}
 &rsaquo; {{ title }}
 </div>
 {% endblock %}
     <p>{% trans "Upload a ZIP archive with a set of pictures" %}</p>
     <p><b>{% trans "Note: all pictures will be non-public by default, so you can edit its properties before publish they to the web." %}</b></p>
     <div>
-      {% if zip_upload_form.errors %}
+      {% if form.errors %}
         <p class="errornote">{% trans "Please correct the error below." %}</p>
       {% endif %}
       <fieldset class="module aligned">
-        {% for field in zip_upload_form %}
+        {% for field in form %}
           <div class="form-row{% if field.errors %} errors{% endif %}">
 	    <div>
 	      {% if field.errors %}
         name='galeria-album'
     ),
     url('^$', views.AlbumList.as_view(), name='galeria-album-list'),
-    url('admin/zip-upload/', views.zip_upload, name='galeria-zip-upload')
 )
-from django import http
-from django.contrib.auth.decorators import login_required
-from django.core.exceptions import PermissionDenied
-from django.core.urlresolvers import reverse
-from django.shortcuts import render_to_response
-from django.template import RequestContext
-from django.utils.translation import ugettext_lazy as _
 from django.views import generic
 
-from galeria.forms import ZipUploadForm
 from galeria.models import Album, Picture
 
 
 
 class PictureDetail(generic.detail.DetailView):
     model = Picture
-
-
-@login_required
-def zip_upload(request, template='galeria/admin/zip_upload_form.html'):
-    if not request.user.has_perm('galeria.add_picture'):
-        raise PermissionDenied
-
-    form = ZipUploadForm()
-
-    if request.method == 'POST':
-        form = ZipUploadForm(request.POST, request.FILES)
-        if form.is_valid():
-            picture_list = form.process_zip_archive()
-            Picture.objects.bulk_create(picture_list)
-
-            # Send user to album change view to edit the uploaded pictures
-            album = form.cleaned_data['album']
-            url = reverse('admin:galeria_album_change', args=[album.pk])
-            return http.HttpResponseRedirect(url + '#pictures-group')
-
-    context = {
-        'title': _('ZIP Upload'),
-        'zip_upload_form': form
-    }
-    return render_to_response(template, context, context_instance=RequestContext(request))