Update picture viewer.
[wolnelektury.git] / apps / catalogue / models / book.py
index 5bc1e10..3c50a96 100644 (file)
@@ -3,7 +3,7 @@
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
 import re
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
 import re
-from django.conf import settings as settings
+from django.conf import settings
 from django.core.cache import get_cache
 from django.db import models
 from django.db.models import permalink
 from django.core.cache import get_cache
 from django.db import models
 from django.db.models import permalink
@@ -14,7 +14,7 @@ import jsonfield
 from catalogue import constants
 from catalogue.fields import EbookField
 from catalogue.models import Tag, Fragment, BookMedia
 from catalogue import constants
 from catalogue.fields import EbookField
 from catalogue.models import Tag, Fragment, BookMedia
-from catalogue.utils import create_zip, split_tags, book_upload_path
+from catalogue.utils import create_zip, split_tags, book_upload_path, related_tag_name
 from catalogue import app_settings
 from catalogue import tasks
 from newtagging import managers
 from catalogue import app_settings
 from catalogue import tasks
 from newtagging import managers
@@ -27,6 +27,7 @@ class Book(models.Model):
     """Represents a book imported from WL-XML."""
     title         = models.CharField(_('title'), max_length=120)
     sort_key = models.CharField(_('sort key'), max_length=120, db_index=True, editable=False)
     """Represents a book imported from WL-XML."""
     title         = models.CharField(_('title'), max_length=120)
     sort_key = models.CharField(_('sort key'), max_length=120, db_index=True, editable=False)
+    sort_key_author = models.CharField(_('sort key by author'), max_length=120, db_index=True, editable=False, default=u'')
     slug = models.SlugField(_('slug'), max_length=120, db_index=True,
             unique=True)
     common_slug = models.SlugField(_('slug'), max_length=120, db_index=True)
     slug = models.SlugField(_('slug'), max_length=120, db_index=True,
             unique=True)
     common_slug = models.SlugField(_('slug'), max_length=120, db_index=True)
@@ -36,7 +37,7 @@ class Book(models.Model):
     created_at    = models.DateTimeField(_('creation date'), auto_now_add=True, db_index=True)
     changed_at    = models.DateTimeField(_('creation date'), auto_now=True, db_index=True)
     parent_number = models.IntegerField(_('parent number'), default=0)
     created_at    = models.DateTimeField(_('creation date'), auto_now_add=True, db_index=True)
     changed_at    = models.DateTimeField(_('creation date'), auto_now=True, db_index=True)
     parent_number = models.IntegerField(_('parent number'), default=0)
-    extra_info    = jsonfield.JSONField(_('extra information'), default='{}')
+    extra_info    = jsonfield.JSONField(_('extra information'), default={})
     gazeta_link   = models.CharField(blank=True, max_length=240)
     wiki_link     = models.CharField(blank=True, max_length=240)
     # files generated during publication
     gazeta_link   = models.CharField(blank=True, max_length=240)
     wiki_link     = models.CharField(blank=True, max_length=240)
     # files generated during publication
@@ -74,8 +75,9 @@ class Book(models.Model):
         from sortify import sortify
 
         self.sort_key = sortify(self.title)
         from sortify import sortify
 
         self.sort_key = sortify(self.title)
+        self.title = unicode(self.title) # ???
 
 
-        ret = super(Book, self).save(force_insert, force_update)
+        ret = super(Book, self).save(force_insert, force_update, **kwargs)
 
         if reset_short_html:
             self.reset_short_html()
 
         if reset_short_html:
             self.reset_short_html()
@@ -86,10 +88,21 @@ class Book(models.Model):
     def get_absolute_url(self):
         return ('catalogue.views.book_detail', [self.slug])
 
     def get_absolute_url(self):
         return ('catalogue.views.book_detail', [self.slug])
 
+    @staticmethod
+    @permalink
+    def create_url(slug):
+        return ('catalogue.views.book_detail', [slug])
+
     @property
     def name(self):
         return self.title
 
     @property
     def name(self):
         return self.title
 
+    def language_code(self):
+        return constants.LANGUAGES_3TO2.get(self.language, self.language)
+
+    def language_name(self):
+        return dict(settings.LANGUAGES).get(self.language_code(), "")
+
     def book_tag_slug(self):
         return ('l-' + self.slug)[:120]
 
     def book_tag_slug(self):
         return ('l-' + self.slug)[:120]
 
@@ -135,6 +148,14 @@ class Book(models.Model):
         for fragm in self.fragments.all().iterator():
             fragm.reset_short_html()
 
         for fragm in self.fragments.all().iterator():
             fragm.reset_short_html()
 
+        try: 
+            author = self.tags.filter(category='author')[0].sort_key
+        except IndexError:
+            author = u''
+        type(self).objects.filter(pk=self.pk).update(sort_key_author=author)
+
+
+
     def has_description(self):
         return len(self.description) > 0
     has_description.short_description = _('description')
     def has_description(self):
         return len(self.description) > 0
     has_description.short_description = _('description')
@@ -196,7 +217,7 @@ class Book(models.Model):
         try:
             index.index_book(self, book_info)
             if index_tags:
         try:
             index.index_book(self, book_info)
             if index_tags:
-                idx.index_tags()
+                index.index_tags()
             if commit:
                 index.index.commit()
         except Exception, e:
             if commit:
                 index.index.commit()
         except Exception, e:
@@ -386,6 +407,10 @@ class Book(models.Model):
             for child in self.children.all():
                 child.parent_cover_changed()
 
             for child in self.children.all():
                 child.parent_cover_changed()
 
+    def other_versions(self):
+        """Find other versions (i.e. in other languages) of the book."""
+        return type(self).objects.filter(common_slug=self.common_slug).exclude(pk=self.pk)
+
     def related_info(self):
         """Keeps info about related objects (tags, media) in cache field."""
         if self._related_info is not None:
     def related_info(self):
         """Keeps info about related objects (tags, media) in cache field."""
         if self._related_info is not None:
@@ -397,8 +422,15 @@ class Book(models.Model):
                     'author', 'kind', 'genre', 'epoch'))
             tags = split_tags(tags)
             for category in tags:
                     'author', 'kind', 'genre', 'epoch'))
             tags = split_tags(tags)
             for category in tags:
-                rel['tags'][category] = [
-                        (t.name, t.slug) for t in tags[category]]
+                cat = []
+                for tag in tags[category]:
+                    tag_info = {'slug': tag.slug, 'name': tag.name}
+                    for lc, ln in settings.LANGUAGES:
+                        tag_name = getattr(tag, "name_%s" % lc)
+                        if tag_name:
+                            tag_info["name_%s" % lc] = tag_name
+                    cat.append(tag_info)
+                rel['tags'][category] = cat
 
             for media_format in BookMedia.formats:
                 rel['media'][media_format] = self.has_media(media_format)
 
             for media_format in BookMedia.formats:
                 rel['media'][media_format] = self.has_media(media_format)
@@ -481,19 +513,21 @@ class Book(models.Model):
 
     def pretty_title(self, html_links=False):
         book = self
 
     def pretty_title(self, html_links=False):
         book = self
-        names = list(book.tags.filter(category='author'))
-
-        books = []
-        while book:
-            books.append(book)
-            book = book.parent
-        names.extend(reversed(books))
+        rel_info = book.related_info()
+        names = [(related_tag_name(tag), Tag.create_url('author', tag['slug']))
+                    for tag in rel_info['tags'].get('author', ())]
+        import logging
+        logging.info("%s, %s" % (book.slug, unicode(rel_info['tags'].get('author', ()))))
+        if 'parents' in rel_info:
+            books = [(name, Book.create_url(slug))
+                        for name, slug in rel_info['parents']]
+            names.extend(reversed(books))
+        names.append((self.title, self.get_absolute_url()))
 
         if html_links:
 
         if html_links:
-            names = ['<a href="%s">%s</a>' % (tag.get_absolute_url(), tag.name) for tag in names]
+            names = ['<a href="%s">%s</a>' % (tag[1], tag[0]) for tag in names]
         else:
         else:
-            names = [tag.name for tag in names]
-
+            names = [tag[0] for tag in names]
         return ', '.join(names)
 
     @classmethod
         return ', '.join(names)
 
     @classmethod
@@ -506,10 +540,11 @@ class Book(models.Model):
         """
         # get relevant books and their tags
         objects = cls.tagged.with_all(tags)
         """
         # get relevant books and their tags
         objects = cls.tagged.with_all(tags)
+        parents = objects.filter(html_file='').only('slug')
         # eliminate descendants
         l_tags = Tag.objects.filter(category='book',
         # eliminate descendants
         l_tags = Tag.objects.filter(category='book',
-            slug__in=[book.book_tag_slug() for book in objects.iterator()])
-        descendants_keys = [book.pk for book in cls.tagged.with_any(l_tags).iterator()]
+            slug__in=[book.book_tag_slug() for book in parents.iterator()])
+        descendants_keys = [book.pk for book in cls.tagged.with_any(l_tags).only('pk').iterator()]
         if descendants_keys:
             objects = objects.exclude(pk__in=descendants_keys)
 
         if descendants_keys:
             objects = objects.exclude(pk__in=descendants_keys)
 
@@ -555,6 +590,7 @@ class Book(models.Model):
         return books_by_author, orphans, books_by_parent
 
     _audiences_pl = {
         return books_by_author, orphans, books_by_parent
 
     _audiences_pl = {
+        "SP": (1, u"szkoła podstawowa"),
         "SP1": (1, u"szkoła podstawowa"),
         "SP2": (1, u"szkoła podstawowa"),
         "P": (1, u"szkoła podstawowa"),
         "SP1": (1, u"szkoła podstawowa"),
         "SP2": (1, u"szkoła podstawowa"),
         "P": (1, u"szkoła podstawowa"),
@@ -564,7 +600,7 @@ class Book(models.Model):
     }
     def audiences_pl(self):
         audiences = self.extra_info.get('audiences', [])
     }
     def audiences_pl(self):
         audiences = self.extra_info.get('audiences', [])
-        audiences = sorted(set([self._audiences_pl[a] for a in audiences]))
+        audiences = sorted(set([self._audiences_pl.get(a, (99, a)) for a in audiences]))
         return [a[1] for a in audiences]
 
     def choose_fragment(self):
         return [a[1] for a in audiences]
 
     def choose_fragment(self):