fnp
/
wolnelektury.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
4c89688
)
url links to zip packages
author
Marcin Koziej
<marcin.koziej@nowoczesnapolska.org.pl>
Wed, 26 Oct 2011 13:24:30 +0000
(15:24 +0200)
committer
Marcin Koziej
<marcin.koziej@nowoczesnapolska.org.pl>
Wed, 26 Oct 2011 13:47:14 +0000
(15:47 +0200)
apps/catalogue/models.py
patch
|
blob
|
history
apps/catalogue/tests/bookmedia.py
patch
|
blob
|
history
apps/catalogue/urls.py
patch
|
blob
|
history
apps/catalogue/utils.py
patch
|
blob
|
history
apps/catalogue/views.py
patch
|
blob
|
history
diff --git
a/apps/catalogue/models.py
b/apps/catalogue/models.py
index
471c661
..
a9445a2
100644
(file)
--- 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)
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,
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
9ddd6aa
..
5f7970e
100644
(file)
--- a/
apps/catalogue/tests/bookmedia.py
+++ b/
apps/catalogue/tests/bookmedia.py
@@
-1,9
+1,9
@@
# -*- coding: utf-8 -*-
# -*- 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 django.core.files.base import ContentFile
from catalogue.test_utils import *
-from catalogue import models
+from catalogue import models
, utils
class BookMediaTests(WLTestCase):
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')
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
cb04ba3
..
7fd265f
100644
(file)
--- 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'),
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'),
# tools
url(r'^zegar/$', 'clock', name='clock'),
diff --git
a/apps/catalogue/utils.py
b/apps/catalogue/utils.py
index
d5ef2b7
..
369656e
100644
(file)
--- 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 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
from fcntl import flock, LOCK_EX
from zipfile import ZipFile
@@
-69,19
+69,33
@@
class BookImportDocProvider(DocProvider):
class LockFile(object):
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 __init__(self, dir, objname):
self.lockname = path.join(dir, objname + ".lock")
- def __ent
ry
__(self):
+ def __ent
er
__(self):
self.lock = open(self.lockname, 'w')
flock(self.lock, LOCK_EX)
def __exit__(self, *err):
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()
self.lock.close()
- unlink(self.lockname)
def create_zip(paths, zip_slug):
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')
# 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):
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:
zip_file = path.join(settings.MEDIA_ROOT, 'zip', zip_slug + '.zip')
try:
unlink(zip_file)
except OSError as oe:
- if oe.errno != E
EXIS
T:
+ if oe.errno != E
NOEN
T:
raise oe
raise oe
diff --git
a/apps/catalogue/views.py
b/apps/catalogue/views.py
index
5c0f209
..
b3e012b
100644
(file)
--- 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))
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 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='/?='))