removing zip pacjes in Book/BookMedia + test
[wolnelektury.git] / apps / catalogue / models.py
index c5ca5df..e4d3595 100644 (file)
@@ -22,7 +22,7 @@ from django.conf import settings
 from newtagging.models import TagBase, tags_updated
 from newtagging import managers
 from catalogue.fields import JSONField, OverwritingFileField
 from newtagging.models import TagBase, tags_updated
 from newtagging import managers
 from catalogue.fields import JSONField, OverwritingFileField
-from catalogue.utils import ExistingFile, BookImportDocProvider
+from catalogue.utils import ExistingFile, BookImportDocProvider, create_zip_task, remove_zip
 
 from librarian import dcparser, html, epub, NoDublinCore
 import mutagen
 
 from librarian import dcparser, html, epub, NoDublinCore
 import mutagen
@@ -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)
 
             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())
         super(BookMedia, self).save(*args, **kwargs)
         extra_info = self.get_extra_info_value()
         extra_info.update(self.read_meta())
@@ -518,10 +521,12 @@ class Book(models.Model):
         from tempfile import NamedTemporaryFile
         import os
 
         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)
         path, fname = os.path.realpath(self.xml_file.path).rsplit('/', 1)
         try:
             pdf_file = NamedTemporaryFile(delete=False)
-
             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,
@@ -531,7 +536,6 @@ class Book(models.Model):
         finally:
             unlink(pdf_file.name)
 
         finally:
             unlink(pdf_file.name)
 
-
     def build_epub(self, remove_descendants=True):
         """ (Re)builds the epub file.
             If book has a parent, does nothing.
     def build_epub(self, remove_descendants=True):
         """ (Re)builds the epub file.
             If book has a parent, does nothing.
@@ -545,6 +549,9 @@ class Book(models.Model):
             # don't need an epub
             return
 
             # 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)
         epub_file = StringIO()
         try:
             epub.transform(BookImportDocProvider(self), self.slug, output_file=epub_file)
@@ -627,6 +634,42 @@ class Book(models.Model):
             return True
         return False
 
             return True
         return False
 
+    @staticmethod
+    def zip_epub():
+        books = Book.objects.all()
+
+        paths = filter(lambda x: x is not None,
+                       map(lambda b: b.epub_file and b.epub_file.path or None, books))
+        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():
+        books = Book.objects.all()
+
+        paths = filter(lambda x: x is not None,
+                       map(lambda b: b.pdf_file and b.pdf_file.path or None, books))
+        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)
+        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
+
 
     @classmethod
     def from_xml_file(cls, xml_file, **kwargs):
 
     @classmethod
     def from_xml_file(cls, xml_file, **kwargs):