From: Radek Czajka Date: Wed, 3 Nov 2010 12:59:38 +0000 (+0100) Subject: #933,#934: xmls and epubs to download X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/cd16ef42a2d7497b179c0b14e360398dcb604a5b #933,#934: xmls and epubs to download --- diff --git a/apps/catalogue/urls.py b/apps/catalogue/urls.py index 9371c0668..232513754 100644 --- a/apps/catalogue/urls.py +++ b/apps/catalogue/urls.py @@ -22,7 +22,9 @@ urlpatterns = patterns('catalogue.views', url(r'^szukaj/$', 'search', name='search'), # tools - url(r'^zegar', 'clock', name='clock'), + url(r'^zegar/$', 'clock', name='clock'), + url(r'^xmls.zip$', 'xmls', name='xmls'), + url(r'^epubs.tar$', 'epubs', name='epubs'), # Public interface. Do not change this URLs. url(r'^lektura/(?P[a-zA-Z0-9-]+)\.html$', 'book_text', name='book_text'), diff --git a/apps/catalogue/views.py b/apps/catalogue/views.py index 7b8050f29..de522e2c9 100644 --- a/apps/catalogue/views.py +++ b/apps/catalogue/views.py @@ -4,12 +4,14 @@ # import tempfile import zipfile +import tarfile import sys import pprint import traceback import re import itertools from operator import itemgetter +from datetime import datetime from django.conf import settings from django.template import RequestContext @@ -720,5 +722,47 @@ def clock(request): """ Provides server time for jquery.countdown, in a format suitable for Date.parse() """ - from datetime import datetime return HttpResponse(datetime.now().strftime('%Y/%m/%d %H:%M:%S')) + + +@cache.never_cache +def xmls(request): + """" + Create a zip archive with all XML files. + """ + temp = tempfile.TemporaryFile() + archive = zipfile.ZipFile(temp, 'w') + + for book in models.Book.objects.all(): + archive.write(book.xml_file.path, str('%s.xml' % book.slug)) + archive.close() + + response = HttpResponse(content_type='application/zip', mimetype='application/x-zip-compressed') + response['Content-Disposition'] = 'attachment; filename=xmls.zip' + response['Content-Length'] = temp.tell() + + temp.seek(0) + response.write(temp.read()) + return response + + +@cache.never_cache +def epubs(request): + """" + Create a tar archive with all EPUB files, segregated to directories. + """ + + temp = tempfile.TemporaryFile() + archive = tarfile.TarFile(fileobj=temp, mode='w') + + for book in models.Book.objects.exclude(epub_file=''): + archive.add(book.epub_file.path, (u'%s/%s.epub' % (book.get_extra_info_value()['author'], book.slug)).encode('utf-8')) + archive.close() + + response = HttpResponse(content_type='application/tar', mimetype='application/x-tar') + response['Content-Disposition'] = 'attachment; filename=epubs.tar' + response['Content-Length'] = temp.tell() + + temp.seek(0) + response.write(temp.read()) + return response