From: Guillaume Pellerin Date: Wed, 15 May 2013 09:27:11 +0000 (+0200) Subject: add modules X-Git-Tag: 1.4.5~20 X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=e2f40c9ab3f8ccd5f8bd991763c4c381065e33bb;p=telemeta.git add modules --- diff --git a/telemeta/views/collection.py b/telemeta/views/collection.py index 7fb0def4..e4821e21 100644 --- a/telemeta/views/collection.py +++ b/telemeta/views/collection.py @@ -160,3 +160,29 @@ class CollectionView(object): formset = MediaCollectionRelatedFormSet(instance=collection) return render(request, template, {'collection': collection, 'formset': formset,}) + + +class CollectionPackageView(DetailView): + + model = MediaCollection + + def render_to_reponse(self, context): + """ + Create a ZIP file 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. + """ + collection = self.get_object() + temp = tempfile.TemporaryFile() + archive = zipfile.ZipFile(temp, 'w', zipfile.ZIP_DEFLATED) + for item in collection.items.all(): + ext = item.file.path.splitext()[1] + archive.write(item.file, '%s.%s' % (code, ext)) + archive.close() + wrapper = FileWrapper(temp) + response = HttpResponse(wrapper, content_type='application/zip') + response['Content-Disposition'] = "attachment; filename=%s.%s" % \ + (item.code, 'zip') + response['Content-Length'] = temp.tell() + temp.seek(0) + return response diff --git a/telemeta/views/core.py b/telemeta/views/core.py index f57f35e0..9edf6d33 100644 --- a/telemeta/views/core.py +++ b/telemeta/views/core.py @@ -41,6 +41,8 @@ import csv import time import random import datetime +import tempfile +import zipfile import timeside from jsonrpc import jsonrpc_method @@ -107,8 +109,8 @@ def nginx_media_accel(request, filename): response = HttpResponse() url = settings.MEDIA_URL + filename - # let nginx determine the correct content type - response['Content-Type'] = "" + # let nginx determine the correct content type + response['Content-Type'] = "" response['X-Accel-Redirect'] = url return response