url links to zip packages
authorMarcin Koziej <marcin.koziej@nowoczesnapolska.org.pl>
Wed, 26 Oct 2011 13:24:30 +0000 (15:24 +0200)
committerMarcin Koziej <marcin.koziej@nowoczesnapolska.org.pl>
Wed, 26 Oct 2011 13:47:14 +0000 (15:47 +0200)
apps/catalogue/models.py
apps/catalogue/tests/bookmedia.py
apps/catalogue/urls.py
apps/catalogue/utils.py
apps/catalogue/views.py

index 471c661..a9445a2 100644 (file)
@@ -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,
index 9ddd6aa..5f7970e 100644 (file)
@@ -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)))
index cb04ba3..7fd265f 100644 (file)
@@ -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<slug>[a-zA-Z0-9-]+)\.zip', 'download_zip', {'format': 'audiobook'}, 'download_zip_audiobook'),
+
     # tools
     url(r'^zegar/$', 'clock', name='clock'),
 
index d5ef2b7..369656e 100644 (file)
@@ -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
 
 
index 5c0f209..b3e012b 100644 (file)
@@ -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='/?='))