customized pdf generation.
[wolnelektury.git] / apps / catalogue / models.py
index 128fe81..b82879b 100644 (file)
@@ -8,10 +8,11 @@ from django.db import models
 from django.db.models import permalink, Q
 import django.dispatch
 from django.core.cache import cache
+from django.core.files.storage import DefaultStorage
 from django.utils.translation import ugettext_lazy as _
 from django.contrib.auth.models import User
-from django.core.files import File
 from django.template.loader import render_to_string
+from django.utils.datastructures import SortedDict
 from django.utils.safestring import mark_safe
 from django.utils.translation import get_language
 from django.core.urlresolvers import reverse
@@ -22,14 +23,12 @@ from django.conf import settings
 from newtagging.models import TagBase, tags_updated
 from newtagging import managers
 from catalogue.fields import JSONField, OverwritingFileField
-from catalogue.utils import ExistingFile, ORMDocProvider, create_zip, remove_zip
+from catalogue.utils import create_zip
+from shutil import copy
+from glob import glob
+import re
+from os import path
 
-from librarian import dcparser, html, epub, NoDublinCore
-import mutagen
-from mutagen import id3
-from slughifi import slughifi
-from sortify import sortify
-from os import unlink
 
 TAG_CATEGORIES = (
     ('author', _('author')),
@@ -176,21 +175,46 @@ class Tag(TagBase):
         return '/'.join((Tag.categories_dict[self.category], self.slug))
 
 
+def get_dynamic_path(media, filename, ext=None, maxlen=100):
+    from slughifi import slughifi
+
+    # how to put related book's slug here?
+    if not ext:
+        if media.type == 'daisy':
+            ext = 'daisy.zip'
+        else:
+            ext = media.type
+    if media is None or not media.name:
+        name = slughifi(filename.split(".")[0])
+    else:
+        name = slughifi(media.name)
+    return 'book/%s/%s.%s' % (ext, name[:maxlen-len('book/%s/.%s' % (ext, ext))-4], ext)
+
+
 # TODO: why is this hard-coded ?
 def book_upload_path(ext=None, maxlen=100):
-    def get_dynamic_path(media, filename, ext=ext):
-        # how to put related book's slug here?
-        if not ext:
-            if media.type == 'daisy':
-                ext = 'daisy.zip'
-            else:
-                ext = media.type
-        if not media.name:
-            name = slughifi(filename.split(".")[0])
-        else:
-            name = slughifi(media.name)
-        return 'book/%s/%s.%s' % (ext, name[:maxlen-len('book/%s/.%s' % (ext, ext))-4], ext)
-    return get_dynamic_path
+    return lambda *args: get_dynamic_path(*args, ext=ext, maxlen=maxlen)
+
+
+def get_customized_pdf_path(book, customizations):
+    """
+    Returns a MEDIA_ROOT relative path for a customized pdf. The name will contain a hash of customization options.
+    """
+    customizations.sort()
+    h = hash(tuple(customizations))
+    pdf_name = '%s-custom-%s' % (book.slug, h)
+    pdf_file = models.get_dynamic_path(None, pdf_name, ext='pdf')
+    return pdf_file
+
+
+def get_existing_customized_pdf(book):
+    """
+    Returns a list of paths to generated customized pdf of a book
+    """
+    pdf_glob = '%s-custom-' % (book.slug,)
+    pdf_glob = get_dynamic_path(None, pdf_glob, ext='pdf')
+    pdf_glob = re.sub(r"[.]([a-z0-9]+)$", "*.\\1", pdf_glob)
+    return glob(path.join(settings.MEDIA_ROOT, pdf_glob))
 
 
 class BookMedia(models.Model):
@@ -211,6 +235,9 @@ class BookMedia(models.Model):
         verbose_name_plural = _('book media')
 
     def save(self, *args, **kwargs):
+        from slughifi import slughifi
+        from catalogue.utils import ExistingFile, remove_zip
+
         try:
             old = BookMedia.objects.get(pk=self.pk)
         except BookMedia.DoesNotExist, e:
@@ -220,10 +247,11 @@ class BookMedia(models.Model):
             if slughifi(self.name) != slughifi(old.name):
                 self.file.save(None, ExistingFile(self.file.path), save=False, leave=True)
 
+        super(BookMedia, self).save(*args, **kwargs)
+
         # 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())
         self.set_extra_info_value(extra_info)
@@ -234,6 +262,8 @@ class BookMedia(models.Model):
         """
             Reads some metadata from the audiobook.
         """
+        import mutagen
+        from mutagen import id3
 
         artist_name = director_name = project = funded_by = ''
         if self.type == 'mp3':
@@ -266,6 +296,8 @@ class BookMedia(models.Model):
         """
             Reads source file SHA1 from audiobok metadata.
         """
+        import mutagen
+        from mutagen import id3
 
         if filetype == 'mp3':
             try:
@@ -305,6 +337,7 @@ class Book(models.Model):
     tags     = managers.TagDescriptor(Tag)
 
     html_built = django.dispatch.Signal()
+    published = django.dispatch.Signal()
 
     class AlreadyExists(Exception):
         pass
@@ -318,6 +351,8 @@ class Book(models.Model):
         return self.title
 
     def save(self, force_insert=False, force_update=False, reset_short_html=True, **kwargs):
+        from sortify import sortify
+
         self.sort_key = sortify(self.title)
 
         ret = super(Book, self).save(force_insert, force_update)
@@ -458,38 +493,52 @@ class Book(models.Model):
     has_daisy_file.short_description = 'DAISY'
     has_daisy_file.boolean = True
 
-    def build_pdf(self):
+    def build_pdf(self, customizations=None, file_name=None):
         """ (Re)builds the pdf file.
-
+        customizations - customizations which are passed to LaTeX class file.
+        file_name - save the pdf file under a different name and DO NOT save it in db.
         """
-        from librarian import pdf
         from tempfile import NamedTemporaryFile
-        import os
-
-        # remove zip with all pdf files
-        remove_zip(settings.ALL_PDF_ZIP)
+        from os import unlink
+        from django.core.files import File
+        from librarian import pdf
+        from catalogue.utils import ORMDocProvider, remove_zip
 
         try:
             pdf_file = NamedTemporaryFile(delete=False)
             pdf.transform(ORMDocProvider(self),
                       file_path=str(self.xml_file.path),
                       output_file=pdf_file,
+                      customizations=customizations
                       )
 
-            self.pdf_file.save('%s.pdf' % self.slug, File(open(pdf_file.name)))
+            if file_name is None:
+                # we'd like to be sure not to overwrite changes happening while
+                # (timely) pdf generation is taking place (async celery scenario)
+                current_self = Book.objects.get(id=self.id)
+                current_self.pdf_file.save('%s.pdf' % self.slug, File(open(pdf_file.name)))
+                self.pdf_file = current_self.pdf_file
+            else:
+                print "safing %s" % file_name
+                print "to: %s" % DefaultStorage().path(file_name)
+                DefaultStorage().save(file_name, File(open(pdf_file.name)))
         finally:
             unlink(pdf_file.name)
 
+        # remove cached downloadables
+        remove_zip(settings.ALL_PDF_ZIP)
+        for customized_pdf in get_existing_customized_pdf(self):
+            unlink(customized_pdf)
+
     def build_mobi(self):
         """ (Re)builds the MOBI file.
 
         """
-        from librarian import mobi
         from tempfile import NamedTemporaryFile
-        import os
-
-        # remove zip with all pdf files
-        remove_zip(settings.ALL_MOBI_ZIP)
+        from os import unlink
+        from django.core.files import File
+        from librarian import mobi
+        from catalogue.utils import ORMDocProvider, remove_zip
 
         try:
             mobi_file = NamedTemporaryFile(suffix='.mobi', delete=False)
@@ -502,6 +551,9 @@ class Book(models.Model):
         finally:
             unlink(mobi_file.name)
 
+        # remove zip with all mobi files
+        remove_zip(settings.ALL_MOBI_ZIP)
+
     def build_epub(self, remove_descendants=True):
         """ (Re)builds the epub file.
             If book has a parent, does nothing.
@@ -510,14 +562,13 @@ class Book(models.Model):
         from StringIO import StringIO
         from hashlib import sha1
         from django.core.files.base import ContentFile
+        from librarian import epub, NoDublinCore
+        from catalogue.utils import ORMDocProvider, remove_zip
 
         if self.parent:
             # 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(ORMDocProvider(self), self.slug, output_file=epub_file)
@@ -535,6 +586,9 @@ class Book(models.Model):
             child_book.save()
             book_descendants += list(child_book.children.all())
 
+        # remove zip package with all epub files
+        remove_zip(settings.ALL_EPUB_ZIP)
+
     def build_txt(self):
         from StringIO import StringIO
         from django.core.files.base import ContentFile
@@ -548,6 +602,9 @@ class Book(models.Model):
     def build_html(self):
         from tempfile import NamedTemporaryFile
         from markupstring import MarkupString
+        from django.core.files import File
+        from slughifi import slughifi
+        from librarian import html
 
         meta_tags = list(self.tags.filter(
             category__in=('author', 'epoch', 'genre', 'kind')))
@@ -601,40 +658,32 @@ class Book(models.Model):
         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))
-        result = create_zip.delay(paths, settings.ALL_EPUB_ZIP)
+    def zip_format(format_):
+        def pretty_file_name(book):
+            return "%s/%s.%s" % (
+                b.get_extra_info_value()['author'],
+                b.slug,
+                format_)
+
+        field_name = "%s_file" % format_
+        books = Book.objects.filter(parent=None).exclude(**{field_name: ""})
+        paths = [(pretty_file_name(b), getattr(b, field_name).path)
+                    for b in books]
+        result = create_zip.delay(paths,
+                    getattr(settings, "ALL_%s_ZIP" % format_.upper()))
         return result.wait()
 
-    @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))
-        result = create_zip.delay(paths, settings.ALL_PDF_ZIP)
-        return result.wait()
-
-    @staticmethod
-    def zip_mobi():
-        books = Book.objects.all()
-
-        paths = filter(lambda x: x is not None,
-                       map(lambda b: b.mobi_file and b.mobi_file.path or None, books))
-        result = create_zip.delay(paths, settings.ALL_MOBI_ZIP)
-        return settings.MEDIA_URL + result.wait()
-
     def zip_audiobooks(self):
-        bm = BookMedia.objects.filter(book=self)
-        paths = map(lambda bm: bm.file.path, bm)
+        bm = BookMedia.objects.filter(book=self, type='mp3')
+        paths = map(lambda bm: (None, bm.file.path), bm)
         result = create_zip.delay(paths, self.slug)
         return result.wait()
 
     @classmethod
     def from_xml_file(cls, xml_file, **kwargs):
+        from django.core.files import File
+        from librarian import dcparser
+
         # use librarian to parse meta-data
         book_info = dcparser.parse(xml_file)
 
@@ -650,6 +699,8 @@ class Book(models.Model):
     def from_text_and_meta(cls, raw_file, book_info, overwrite=False,
             build_epub=True, build_txt=True, build_pdf=True, build_mobi=True):
         import re
+        from slughifi import slughifi
+        from sortify import sortify
 
         # check for parts before we do anything
         children = []
@@ -744,6 +795,7 @@ class Book(models.Model):
         book.reset_tag_counter()
         book.reset_theme_counter()
 
+        cls.published.send(sender=book)
         return book
 
     def reset_tag_counter(self):
@@ -837,6 +889,56 @@ class Book(models.Model):
 
         return objects
 
+    @classmethod
+    def book_list(cls, filter=None):
+        """Generates a hierarchical listing of all books.
+
+        Books are optionally filtered with a test function.
+
+        """
+
+        books_by_parent = {}
+        books = cls.objects.all().order_by('parent_number', 'sort_key').only('title', 'parent', 'slug')
+        if filter:
+            books = books.filter(filter).distinct()
+            book_ids = set((book.pk for book in books))
+            for book in books:
+                parent = book.parent_id
+                if parent not in book_ids:
+                    parent = None
+                books_by_parent.setdefault(parent, []).append(book)
+        else:
+            for book in books:
+                books_by_parent.setdefault(book.parent_id, []).append(book)
+
+        orphans = []
+        books_by_author = SortedDict()
+        for tag in Tag.objects.filter(category='author'):
+            books_by_author[tag] = []
+
+        for book in books_by_parent.get(None,()):
+            authors = list(book.tags.filter(category='author'))
+            if authors:
+                for author in authors:
+                    books_by_author[author].append(book)
+            else:
+                orphans.append(book)
+
+        return books_by_author, orphans, books_by_parent
+
+    _audiences_pl = {
+        "SP1": (1, u"szkoła podstawowa"),
+        "SP2": (1, u"szkoła podstawowa"),
+        "P": (1, u"szkoła podstawowa"),
+        "G": (2, u"gimnazjum"),
+        "L": (3, u"liceum"),
+        "LP": (3, u"liceum"),
+    }
+    def audiences_pl(self):
+        audiences = self.get_extra_info_value().get('audiences', [])
+        audiences = sorted(set([self._audiences_pl[a] for a in audiences]))
+        return [a[1] for a in audiences]
+
 
 def _has_factory(ftype):
     has = lambda self: bool(getattr(self, "%s_file" % ftype))