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,
# -*- 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):
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)))
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'),
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
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')
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
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='/?='))