From 0946041483bdb729ae076d8ead0699524eeeb4b2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Thu, 19 Dec 2013 12:41:21 +0100 Subject: [PATCH] Allow for creating package.zip containing files in a gallery --- apps/catalogue/urls.py | 5 +- apps/catalogue/views.py | 15 ++++-- .../locale/pl/LC_MESSAGES/django.mo | Bin 747 -> 794 bytes .../locale/pl/LC_MESSAGES/django.po | 16 +++--- .../templates/fileupload/picture_form.html | 6 ++- apps/fileupload/views.py | 51 +++++++++++------- redakcja/static/css/filelist.css | 4 ++ 7 files changed, 66 insertions(+), 31 deletions(-) diff --git a/apps/catalogue/urls.py b/apps/catalogue/urls.py index e72b88db..283e681f 100644 --- a/apps/catalogue/urls.py +++ b/apps/catalogue/urls.py @@ -3,7 +3,7 @@ from django.conf.urls import patterns, url from django.contrib.auth.decorators import permission_required from django.views.generic import RedirectView from catalogue.feeds import PublishTrackFeed -from catalogue.views import GalleryView +from catalogue.views import GalleryView, GalleryPackageView urlpatterns = patterns('catalogue.views', @@ -32,6 +32,9 @@ urlpatterns = patterns('catalogue.views', url(r'^book/(?P[^/]+)/gallery/$', permission_required('catalogue.change_book')(GalleryView.as_view()), name="catalogue_book_gallery"), + url(r'^book/(?P[^/]+)/gallery/package$', + permission_required('catalogue.change_book')(GalleryPackageView.as_view()), + name="catalogue_book_gallery_package"), url(r'^book/(?P[^/]+)/xml$', 'book_xml', name="catalogue_book_xml"), url(r'^book/(?P[^/]+)/txt$', 'book_txt', name="catalogue_book_txt"), url(r'^book/(?P[^/]+)/html$', 'book_html', name="catalogue_book_html"), diff --git a/apps/catalogue/views.py b/apps/catalogue/views.py index ebc35751..9bd69f8b 100644 --- a/apps/catalogue/views.py +++ b/apps/catalogue/views.py @@ -26,7 +26,7 @@ from catalogue import forms from catalogue import helpers from catalogue.helpers import active_tab from catalogue.models import Book, Chunk, BookPublishRecord, ChunkPublishRecord, Project -from fileupload.views import UploadView +from fileupload.views import UploadView, PackageView # # Quick hack around caching problems, TODO: use ETags @@ -488,13 +488,17 @@ def publish(request, slug): return http.HttpResponseRedirect(book.get_absolute_url()) -class GalleryView(UploadView): +class GalleryMixin(object): + def get_directory(self): + return "%s%s/" % (settings.IMAGE_DIR, self.object.gallery) def get_object(self, request, slug): book = get_object_or_404(Book, slug=slug) if not book.gallery: raise Http404 return book +class GalleryView(GalleryMixin, UploadView): + def breadcrumbs(self): return [ (_('books'), reverse('catalogue_document_list')), @@ -502,5 +506,8 @@ class GalleryView(UploadView): (_('scan gallery'),), ] - def get_directory(self): - return "%s%s/" % (settings.IMAGE_DIR, self.object.gallery) + +class GalleryPackageView(GalleryMixin, PackageView): + + def get_redirect_url(self, slug): + return reverse('catalogue_book_gallery', kwargs = dict(slug=slug)) diff --git a/apps/fileupload/locale/pl/LC_MESSAGES/django.mo b/apps/fileupload/locale/pl/LC_MESSAGES/django.mo index 8fdb9c485e454719f18f8a87e0b4f518a07a226b..bf6885ea1192d118d08fbb68c291179f240f57a1 100644 GIT binary patch delta 262 zcmaFOI*YCTo)F7a1|VPuVi_O~0b*_-?g3&D*a5^mK)e%(L28ZxF)t9G17bcPz5~Q; zK>QkrLFzsM@gYVAhW|hsB!7qrLSF>ZfH05EVaQUCw| delta 186 zcmbQm_L{Z+o)F7a1|VPqVi_Rz0b*_-t^r~YSOLU5K)e!&*@1W)5c2}@J|G6^IRnHX z`O83jh>?Ne0g&bc;>Ao5dJ~Wq1o968X;vV88Opy2q=CwnfI<+1zziq>Qpf_t%pm^6 XjoK4mr%$e86x^K5*v~kbiCF>wurU?U diff --git a/apps/fileupload/locale/pl/LC_MESSAGES/django.po b/apps/fileupload/locale/pl/LC_MESSAGES/django.po index a4b60990..bc592e9f 100644 --- a/apps/fileupload/locale/pl/LC_MESSAGES/django.po +++ b/apps/fileupload/locale/pl/LC_MESSAGES/django.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2013-03-07 16:27+0100\n" +"POT-Creation-Date: 2013-12-19 12:38+0100\n" "PO-Revision-Date: 2013-03-07 16:27+0100\n" "Last-Translator: Radek Czajka \n" "Language-Team: LANGUAGE \n" @@ -15,25 +15,29 @@ msgstr "" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" -"Plural-Forms: nplurals=3; plural=(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);\n" +"Plural-Forms: nplurals=3; plural=(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 " +"|| n%100>=20) ? 1 : 2);\n" #: templates/fileupload/picture_form.html:18 msgid "Browse:" msgstr "Przeglądanie:" -#: templates/fileupload/picture_form.html:35 +#: templates/fileupload/picture_form.html:34 msgid "Add files..." msgstr "Dodaj pliki..." -#: templates/fileupload/picture_form.html:40 +#: templates/fileupload/picture_form.html:39 msgid "Start upload" msgstr "Zacznij wysyłać" -#: templates/fileupload/picture_form.html:44 +#: templates/fileupload/picture_form.html:43 msgid "Cancel upload" msgstr "Anuluj wysyłanie" -#: templates/fileupload/picture_form.html:48 +#: templates/fileupload/picture_form.html:47 msgid "Delete" msgstr "Usuń" +#: templates/fileupload/picture_form.html:51 +msgid "Create package" +msgstr "Stwórz paczkę" diff --git a/apps/fileupload/templates/fileupload/picture_form.html b/apps/fileupload/templates/fileupload/picture_form.html index 44c37db7..81cb1a66 100644 --- a/apps/fileupload/templates/fileupload/picture_form.html +++ b/apps/fileupload/templates/fileupload/picture_form.html @@ -29,7 +29,7 @@
{% csrf_token %}
-
+
{% trans "Add files..." %} @@ -47,6 +47,10 @@ {% trans "Delete" %} + + + {% trans "Create package" %} +
diff --git a/apps/fileupload/views.py b/apps/fileupload/views.py index ab719a19..de2532f4 100644 --- a/apps/fileupload/views.py +++ b/apps/fileupload/views.py @@ -1,11 +1,12 @@ import json import os +from zipfile import ZipFile from urllib import quote from django.conf import settings from django.http import HttpResponse, HttpResponseRedirect, HttpResponseForbidden, Http404 from django.utils.decorators import method_decorator from django.views.decorators.vary import vary_on_headers -from django.views.generic import FormView, View +from django.views.generic import FormView, View, RedirectView from .forms import UploadForm @@ -30,8 +31,25 @@ class JSONResponse(HttpResponse): content = json.dumps(obj) super(JSONResponse, self).__init__(content, mimetype, *args, **kwargs) +class UploadViewMixin(object): + def get_safe_path(self, filename=""): + """Finds absolute filesystem path of the browsed dir of file. + + Makes sure it's inside MEDIA_ROOT. + + """ + path = os.path.abspath(os.path.join( + settings.MEDIA_ROOT, + self.get_directory(), + filename)) + if not path.startswith(os.path.abspath(settings.MEDIA_ROOT)): + raise Http404 + if filename: + if not path.startswith(self.get_safe_path()): + raise Http404 + return path -class UploadView(FormView): +class UploadView(UploadViewMixin, FormView): template_name = "fileupload/picture_form.html" form_class = UploadForm @@ -68,23 +86,6 @@ class UploadView(FormView): crumbs = [('media',)] return crumbs - def get_safe_path(self, filename=""): - """Finds absolute filesystem path of the browsed dir of file. - - Makes sure it's inside MEDIA_ROOT. - - """ - path = os.path.abspath(os.path.join( - settings.MEDIA_ROOT, - self.get_directory(), - filename)) - if not path.startswith(os.path.abspath(settings.MEDIA_ROOT)): - raise Http404 - if filename: - if not path.startswith(self.get_safe_path()): - raise Http404 - return path - def get_url(self, filename): """Finds URL of a file in browsed dir.""" return settings.MEDIA_URL + self.get_directory() + quote(filename.encode('utf-8')) @@ -150,3 +151,15 @@ class UploadView(FormView): response = JSONResponse(True) response['Content-Disposition'] = 'inline; filename=files.json' return response + + +class PackageView(UploadViewMixin, RedirectView): + def dispatch(self, request, *args, **kwargs): + self.object = self.get_object(request, *args, **kwargs) + path = self.get_safe_path() + with ZipFile(os.path.join(path, 'package.zip'), 'w') as zip_file: + for f in os.listdir(path): + if f == 'package.zip': + continue + zip_file.write(os.path.join(path, f), arcname = f) + return super(PackageView, self).dispatch(request, *args, **kwargs) diff --git a/redakcja/static/css/filelist.css b/redakcja/static/css/filelist.css index f25db1e2..e9b58ca7 100644 --- a/redakcja/static/css/filelist.css +++ b/redakcja/static/css/filelist.css @@ -119,6 +119,10 @@ a, a:visited, a:active { text-decoration: none; } +a.btn { + color: #fff; +} + a:hover { text-decoration: underline; } -- 2.20.1