Attempt at Django 1.7
[wolnelektury.git] / apps / catalogue / models / book.py
index 414d1ef..bab6f58 100644 (file)
@@ -8,6 +8,7 @@ from django.core.cache import get_cache
 from django.db import models
 from django.db.models import permalink
 import django.dispatch
+from django.core.urlresolvers import reverse
 from django.utils.datastructures import SortedDict
 from django.utils.translation import ugettext_lazy as _
 import jsonfield
@@ -25,6 +26,16 @@ bofh_storage = BofhFileSystemStorage()
 permanent_cache = get_cache('permanent')
 
 
+def _cover_upload_to(i, n):
+    return 'book/cover/%s.jpg' % i.slug
+def _cover_thumb_upload_to(i, n):
+    return 'book/cover_thumb/%s.jpg' % i.slug,
+def _ebook_upload_to(upload_path):
+    def _upload_to(i, n):
+        return upload_path % i.slug
+    return _upload_to
+
+
 class Book(models.Model):
     """Represents a book imported from WL-XML."""
     title         = models.CharField(_('title'), max_length=120)
@@ -46,12 +57,12 @@ class Book(models.Model):
 
     cover = EbookField('cover', _('cover'),
             null=True, blank=True,
-            upload_to=lambda i, n: 'book/cover/%s.jpg' % i.slug,
+            upload_to=_cover_upload_to,
             storage=bofh_storage, max_length=255)
     # Cleaner version of cover for thumbs
     cover_thumb = EbookField('cover_thumb', _('cover thumbnail'), 
             null=True, blank=True,
-            upload_to=lambda i, n: 'book/cover_thumb/%s.jpg' % i.slug,
+            upload_to=_cover_thumb_upload_to,
             max_length=255)
     ebook_formats = constants.EBOOK_FORMATS
     formats = ebook_formats + ['html', 'xml']
@@ -321,18 +332,16 @@ class Book(models.Model):
             if old_cover:
                 notify_cover_changed.append(child)
 
-        # delete old fragments when overwriting
-        book.fragments.all().delete()
-        # Build HTML, fix the tree tags, build cover.
-        has_own_text = bool(book.html_file.build())
-        tasks.fix_tree_tags.delay(book)
+        # No saves beyond this point.
+
+        # Build cover.
         if 'cover' not in dont_build:
             book.cover.build_delay()
             book.cover_thumb.build_delay()
 
-        # No saves behind this point.
-
-        if has_own_text:
+        # Build HTML and ebooks.
+        book.html_file.build_delay()
+        if not children:
             for format_ in constants.EBOOK_FORMATS_WITHOUT_CHILDREN:
                 if format_ not in dont_build:
                     getattr(book, '%s_file' % format_).build_delay()
@@ -612,6 +621,14 @@ class Book(models.Model):
         audiences = sorted(set([self._audiences_pl.get(a, (99, a)) for a in audiences]))
         return [a[1] for a in audiences]
 
+    def stage_note(self):
+        stage = self.extra_info.get('stage')
+        if stage and stage < '0.4':
+            return (_('This work needs modernisation'),
+                    reverse('infopage', args=['wymagajace-uwspolczesnienia']))
+        else:
+            return None, None
+
     def choose_fragment(self):
         tag = self.book_tag()
         fragments = Fragment.tagged.with_any([tag])
@@ -626,9 +643,7 @@ class Book(models.Model):
 # add the file fields
 for format_ in Book.formats:
     field_name = "%s_file" % format_
-    upload_to = (lambda upload_path:
-            lambda i, n: upload_path % i.slug
-        )('book/%s/%%s.%s' % (format_, format_))
+    upload_to = _ebook_upload_to('book/%s/%%s.%s' % (format_, format_))
     EbookField(format_, _("%s file" % format_.upper()),
         upload_to=upload_to,
         storage=bofh_storage,