From 0c613164e94ea79fce559b2b1632691986a84f25 Mon Sep 17 00:00:00 2001 From: Marcin Koziej Date: Wed, 26 Oct 2011 15:24:30 +0200 Subject: [PATCH] url links to zip packages --- apps/catalogue/models.py | 1 - apps/catalogue/tests/bookmedia.py | 17 +++++++++++++++-- apps/catalogue/urls.py | 5 +++++ apps/catalogue/utils.py | 25 +++++++++++++++++++++---- apps/catalogue/views.py | 15 +++++++++++++++ 5 files changed, 56 insertions(+), 7 deletions(-) diff --git a/apps/catalogue/models.py b/apps/catalogue/models.py index 471c6617c..a9445a24f 100644 --- a/apps/catalogue/models.py +++ b/apps/catalogue/models.py @@ -521,7 +521,6 @@ class Book(models.Model): path, fname = os.path.realpath(self.xml_file.path).rsplit('/', 1) try: pdf_file = NamedTemporaryFile(delete=False) - print("%s -> %s" % (self.xml_file.path, pdf_file)) pdf.transform(BookImportDocProvider(self), file_path=str(self.xml_file.path), output_file=pdf_file, diff --git a/apps/catalogue/tests/bookmedia.py b/apps/catalogue/tests/bookmedia.py index 9ddd6aaa0..5f7970e84 100644 --- a/apps/catalogue/tests/bookmedia.py +++ b/apps/catalogue/tests/bookmedia.py @@ -1,9 +1,9 @@ # -*- coding: utf-8 -*- -from os.path import basename +from os.path import basename, exists, join, dirname from django.core.files.base import ContentFile from catalogue.test_utils import * -from catalogue import models +from catalogue import models, utils class BookMediaTests(WLTestCase): @@ -84,3 +84,16 @@ class BookMediaTests(WLTestCase): self.assertNotEqual(basename(bm2.file.name), 'title.ogg') self.assertEqual(bm.file.read(), 'X') self.assertEqual(bm2.file.read(), 'Y') + + def test_zip_audiobooks(self): + paths = [ + join(dirname(__file__), "files/fraszka-do-anusie.xml"), + join(dirname(__file__), "files/fraszki.xml") + ] + + url = utils.create_zip(paths, 'test-zip-slug') + self.assertEqual("zip/test-zip-slug.zip", url) + self.assertTrue(exists(join(settings.MEDIA_ROOT, url))) + + utils.remove_zip('test-zip-slug') + self.assertFalse(exists(join(settings.MEDIA_ROOT, url))) diff --git a/apps/catalogue/urls.py b/apps/catalogue/urls.py index cb04ba390..7fd265fdc 100644 --- a/apps/catalogue/urls.py +++ b/apps/catalogue/urls.py @@ -22,6 +22,11 @@ urlpatterns = patterns('catalogue.views', url(r'^jtags/$', 'json_tags_starting_with', name='jhint'), url(r'^szukaj/$', 'search', name='search'), + # zip + url(r'^zip/pdf/.*\.zip$', 'download_zip', {'format': 'pdf', 'slug': None}, 'download_zip_pdf'), + url(r'^zip/epub/.*\.zip$', 'download_zip', {'format': 'epub', 'slug': None}, 'download_zip_epub'), + url(r'^zip/audiobook/(?P[a-zA-Z0-9-]+)\.zip', 'download_zip', {'format': 'audiobook'}, 'download_zip_audiobook'), + # tools url(r'^zegar/$', 'clock', name='clock'), diff --git a/apps/catalogue/utils.py b/apps/catalogue/utils.py index d5ef2b789..369656e40 100644 --- a/apps/catalogue/utils.py +++ b/apps/catalogue/utils.py @@ -13,7 +13,7 @@ from django.utils.hashcompat import sha_constructor from django.conf import settings from celery.task import task from os import mkdir, path, unlink -from errno import EEXIST +from errno import EEXIST, ENOENT from fcntl import flock, LOCK_EX from zipfile import ZipFile @@ -69,19 +69,33 @@ class BookImportDocProvider(DocProvider): class LockFile(object): + """ + A file lock monitor class; createas an ${objname}.lock + file in directory dir, and locks it exclusively. + To be used in 'with' construct. + """ def __init__(self, dir, objname): self.lockname = path.join(dir, objname + ".lock") - def __entry__(self): + def __enter__(self): self.lock = open(self.lockname, 'w') flock(self.lock, LOCK_EX) def __exit__(self, *err): + try: + unlink(self.lockname) + except OSError as oe: + if oe.errno != oe.EEXIST: + raise oe self.lock.close() - unlink(self.lockname) def create_zip(paths, zip_slug): + """ + Creates a zip in MEDIA_ROOT/zip directory containing files from path. + Resulting archive filename is ${zip_slug}.zip + Returns it's path relative to MEDIA_ROOT (no initial slash) + """ # directory to store zip files zip_path = path.join(settings.MEDIA_ROOT, 'zip') @@ -102,11 +116,14 @@ def create_zip(paths, zip_slug): def remove_zip(zip_slug): + """ + removes the ${zip_slug}.zip file from zip store. + """ zip_file = path.join(settings.MEDIA_ROOT, 'zip', zip_slug + '.zip') try: unlink(zip_file) except OSError as oe: - if oe.errno != EEXIST: + if oe.errno != ENOENT: raise oe diff --git a/apps/catalogue/views.py b/apps/catalogue/views.py index 5c0f209a3..b3e012be6 100644 --- a/apps/catalogue/views.py +++ b/apps/catalogue/views.py @@ -768,6 +768,21 @@ def book_info(request, id, lang='pl'): return render_to_response('catalogue/book_info.html', locals(), context_instance=RequestContext(request)) + def tag_info(request, id): tag = get_object_or_404(models.Tag, id=id) return HttpResponse(tag.description) + + +def download_zip(request, format, slug): + url = None + if format == 'pdf': + url = models.Book.zip_pdf() + elif format == 'epub': + url = models.Book.zip_epub() + elif format == 'audiobook' and slug is not None: + book = models.Book.objects.get(slug=slug) + url = book.zip_audiobooks() + else: + raise Http404('No format specified for zip package') + return HttpResponseRedirect(urlquote_plus(url, safe='/?=')) -- 2.20.1