From cf57591257968b71cd9b7b6e2745a0d23f0066e0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Marek=20St=C4=99pniowski?= Date: Thu, 18 Sep 2008 13:29:18 +0200 Subject: [PATCH 1/1] Added a view to download zipped fragment books. --- apps/catalogue/urls.py | 7 ++-- apps/catalogue/views.py | 33 +++++++++++++++++-- .../catalogue/tagged_object_list.html | 4 +-- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/apps/catalogue/urls.py b/apps/catalogue/urls.py index 154a5aacb..11a63783f 100644 --- a/apps/catalogue/urls.py +++ b/apps/catalogue/urls.py @@ -6,13 +6,16 @@ urlpatterns = patterns('catalogue.views', url(r'^$', 'main_page', name='main_page'), url(r'^polki/$', 'user_shelves', name='user_shelves'), url(r'^polki/(?P[a-zA-Z0-9-]+)/usun/$', 'delete_shelf', name='delete_shelf'), + url(r'^polki/(?P[a-zA-Z0-9-]+)/zip$', 'download_shelf', name='download_shelf'), url(r'^lektury/', 'book_list', name='book_list'), url(r'^lektura/(?P[a-zA-Z0-9-]+)/polki/', 'book_sets', name='book_shelves'), url(r'^polki/nowa/$', 'new_set', name='new_set'), - url(r'^lektura/(?P[a-zA-Z0-9-]+)\.html$', 'book_text', name='book_text'), - url(r'^lektura/(?P[a-zA-Z0-9-]+)/$', 'book_detail', name='book_detail'), url(r'^tags/$', 'tags_starting_with', name='hint'), url(r'^szukaj/$', 'search', name='search'), + + # Public interface. Do not change this URLs. + url(r'^lektura/(?P[a-zA-Z0-9-]+)\.html$', 'book_text', name='book_text'), + url(r'^lektura/(?P[a-zA-Z0-9-]+)/$', 'book_detail', name='book_detail'), url(r'^(?P[a-zA-Z0-9-/]+)/$', 'tagged_object_list', name='tagged_object_list'), ) diff --git a/apps/catalogue/views.py b/apps/catalogue/views.py index 45dc7a029..de75803a4 100644 --- a/apps/catalogue/views.py +++ b/apps/catalogue/views.py @@ -1,4 +1,7 @@ # -*- coding: utf-8 -*- +import tempfile +import zipfile + from django.template import RequestContext from django.shortcuts import render_to_response, get_object_or_404 from django.http import HttpResponse, HttpResponseRedirect, Http404 @@ -13,6 +16,7 @@ from django.utils import simplejson from django.utils.functional import Promise from django.utils.encoding import force_unicode from django.views.decorators import cache +from django.core.servers.basehttp import FileWrapper from catalogue import models from catalogue import forms @@ -163,7 +167,7 @@ def tags_starting_with(request): # = Shelf management = # ==================== @login_required -@cache.cache_control(must_revalidate=True, max_age=3600, private=True) +@cache.never_cache def user_shelves(request): shelves = models.Tag.objects.filter(category='set', user=request.user) new_set_form = forms.NewSetForm() @@ -171,7 +175,7 @@ def user_shelves(request): context_instance=RequestContext(request)) -@cache.cache_control(must_revalidate=True, max_age=3600, private=True) +@cache.never_cache def book_sets(request, slug): book = get_object_or_404(models.Book, slug=slug) user_sets = models.Tag.objects.filter(category='set', user=request.user) @@ -197,6 +201,31 @@ def book_sets(request, slug): context_instance=RequestContext(request)) +@cache.cache_control(must_revalidate=True, max_age=1800) +def download_shelf(request, slug): + """" + Create a ZIP archive on disk and transmit it in chunks of 8KB, + without loading the whole file into memory. A similar approach can + be used for large dynamic PDF files. + """ + shelf = get_object_or_404(models.Tag, slug=slug, category='set') + + # Create a ZIP archive + temp = tempfile.TemporaryFile() + archive = zipfile.ZipFile(temp, 'w', zipfile.ZIP_DEFLATED) + for book in models.Book.tagged.with_all(shelf): + filename = book.html_file.path + archive.write(filename, str('%s.html' % book.slug)) + archive.close() + + wrapper = FileWrapper(temp) + response = HttpResponse(wrapper, content_type='application/zip') + response['Content-Disposition'] = 'attachment; filename=%s.zip' % shelf.slug + response['Content-Length'] = temp.tell() + temp.seek(0) + return response + + @login_required @require_POST @cache.never_cache diff --git a/wolnelektury/templates/catalogue/tagged_object_list.html b/wolnelektury/templates/catalogue/tagged_object_list.html index 769a5cb05..733911447 100644 --- a/wolnelektury/templates/catalogue/tagged_object_list.html +++ b/wolnelektury/templates/catalogue/tagged_object_list.html @@ -24,12 +24,12 @@

Zwiń opis ▲

{% endif %} - {% endwith %} {% if shelf_is_set %} - + Pobierz wszystkie książki z tej półki {% endif %} + {% endwith %}
    {% for book in object_list %}
  1. {{ book.short_html }}
  2. -- 2.20.1