From 35ccafef8a83575ac0bf5901b58630033aa95d1e Mon Sep 17 00:00:00 2001 From: Marcin Koziej Date: Thu, 27 Oct 2011 13:54:47 +0200 Subject: [PATCH] removing zip pacjes in Book/BookMedia + test --- .../management/commands/importbooks.py | 3 -- apps/catalogue/models.py | 38 +++++++++++++------ apps/catalogue/tests/bookmedia.py | 18 ++++++++- apps/catalogue/views.py | 2 +- wolnelektury/settings.py | 2 + 5 files changed, 47 insertions(+), 16 deletions(-) diff --git a/apps/catalogue/management/commands/importbooks.py b/apps/catalogue/management/commands/importbooks.py index 62ead5373..f51214d72 100644 --- a/apps/catalogue/management/commands/importbooks.py +++ b/apps/catalogue/management/commands/importbooks.py @@ -101,9 +101,6 @@ class Command(BaseCommand): print "Importing %s.txt" % file_base book.save() - - # clean the generated zip packages. Is this the right place for this? - book.clean_zip_files() except Book.AlreadyExists, msg: print self.style.ERROR('%s: Book already imported. Skipping. To overwrite use --force.' % diff --git a/apps/catalogue/models.py b/apps/catalogue/models.py index a9445a24f..e4d359500 100644 --- a/apps/catalogue/models.py +++ b/apps/catalogue/models.py @@ -220,6 +220,9 @@ class BookMedia(models.Model): if slughifi(self.name) != slughifi(old.name): self.file.save(None, ExistingFile(self.file.path), save=False, leave=True) + # remove the zip package for book with modified media + remove_zip(self.book.slug) + super(BookMedia, self).save(*args, **kwargs) extra_info = self.get_extra_info_value() extra_info.update(self.read_meta()) @@ -518,6 +521,9 @@ class Book(models.Model): from tempfile import NamedTemporaryFile import os + # remove zip with all pdf files + remove_zip(settings.ALL_PDF_ZIP) + path, fname = os.path.realpath(self.xml_file.path).rsplit('/', 1) try: pdf_file = NamedTemporaryFile(delete=False) @@ -543,6 +549,9 @@ class Book(models.Model): # don't need an epub return + # remove zip package with all epub files + remove_zip(settings.ALL_EPUB_ZIP) + epub_file = StringIO() try: epub.transform(BookImportDocProvider(self), self.slug, output_file=epub_file) @@ -631,8 +640,12 @@ class Book(models.Model): paths = filter(lambda x: x is not None, map(lambda b: b.epub_file and b.epub_file.path or None, books)) - result = create_zip_task.delay(paths, settings.ALL_EPUB_ZIP) - return settings.MEDIA_URL + result.wait() + if settings.USE_CELERY: + result = create_zip_task.delay(paths, settings.ALL_EPUB_ZIP) + return result.wait() + else: + result = create_zip_task(paths, settings.ALL_EPUB_ZIP) + return result @staticmethod def zip_pdf(): @@ -640,20 +653,23 @@ class Book(models.Model): paths = filter(lambda x: x is not None, map(lambda b: b.pdf_file and b.pdf_file.path or None, books)) - result = create_zip_task.delay(paths, settings.ALL_PDF_ZIP) - return settings.MEDIA_URL + result.wait() + if settings.USE_CELERY: + result = create_zip_task.delay(paths, settings.ALL_PDF_ZIP) + return result.wait() + else: + result = create_zip_task(paths, settings.ALL_PDF_ZIP) + return result def zip_audiobooks(self): bm = BookMedia.objects.filter(book=self) paths = map(lambda bm: bm.file.path, bm) - result = create_zip_task.delay(paths, self.slug) - - return settings.MEDIA_URL + result.wait() + if settings.USE_CELERY: + result = create_zip_task.delay(paths, self.slug) + return result.wait() + else: + result = create_zip_task(paths, self.slug) + return result - def clean_zip_files(self): - remove_zip(self.slug) - remove_zip(settings.ALL_EPUB_ZIP) - remove_zip(settings.ALL_PDF_ZIP) @classmethod def from_xml_file(cls, xml_file, **kwargs): diff --git a/apps/catalogue/tests/bookmedia.py b/apps/catalogue/tests/bookmedia.py index 5f7970e84..7e75ac376 100644 --- a/apps/catalogue/tests/bookmedia.py +++ b/apps/catalogue/tests/bookmedia.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- from os.path import basename, exists, join, dirname -from django.core.files.base import ContentFile +from django.core.files.base import ContentFile, File from catalogue.test_utils import * from catalogue import models, utils @@ -97,3 +97,19 @@ class BookMediaTests(WLTestCase): utils.remove_zip('test-zip-slug') self.assertFalse(exists(join(settings.MEDIA_ROOT, url))) + + def test_remove_zip_on_media_change(self): + bm = models.BookMedia(book=self.book, type='ogg', name="Title") + bm.file.save(None, self.file) + bm.save() + + zip_url = self.book.zip_audiobooks() + self.assertEqual('zip/'+self.book.slug+'.zip', zip_url) + self.assertTrue(exists(join(settings.MEDIA_ROOT, zip_url))) + + bm2 = models.BookMedia(book=self.book, type='ogg', name="Other title") + bm2.file.save(None, self.file2) + bm2.name = "Title" + bm2.save() + # was the audiobook zip deleted? + self.assertFalse(exists(join(settings.MEDIA_ROOT, zip_url))) diff --git a/apps/catalogue/views.py b/apps/catalogue/views.py index b3e012be6..90ef8c2c1 100644 --- a/apps/catalogue/views.py +++ b/apps/catalogue/views.py @@ -785,4 +785,4 @@ def download_zip(request, format, slug): url = book.zip_audiobooks() else: raise Http404('No format specified for zip package') - return HttpResponseRedirect(urlquote_plus(url, safe='/?=')) + return HttpResponseRedirect(urlquote_plus(settings.MEDIA_URL + url, safe='/?=')) diff --git a/wolnelektury/settings.py b/wolnelektury/settings.py index e0f740fff..826d1110d 100644 --- a/wolnelektury/settings.py +++ b/wolnelektury/settings.py @@ -243,6 +243,8 @@ BROKER_USER = "guest" BROKER_PASSWORD = "guest" BROKER_VHOST = "/" +USE_CELERY = True + # Load localsettings, if they exist try: from localsettings import * -- 2.20.1