Minimal double reader.
[wolnelektury.git] / apps / catalogue / models / book.py
index c2b54e4..9025e3e 100644 (file)
@@ -75,7 +75,7 @@ class Book(models.Model):
 
         self.sort_key = sortify(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()
@@ -86,6 +86,11 @@ class Book(models.Model):
     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
@@ -189,20 +194,20 @@ class Book(models.Model):
         paths = map(lambda bm: (None, bm.file.path), bm)
         return create_zip(paths, "%s_%s" % (self.slug, format_))
 
-    def search_index(self, book_info=None, reuse_index=False, index_tags=True):
+    def search_index(self, book_info=None, index=None, index_tags=True, commit=True):
         import search
-        if reuse_index:
-            idx = search.ReusableIndex()
-        else:
-            idx = search.Index()
-            
-        idx.open()
+        if index is None:
+            index = search.Index()
         try:
-            idx.index_book(self, book_info)
+            index.index_book(self, book_info)
             if index_tags:
-                idx.index_tags()
-        finally:
-            idx.close()
+                index.index_tags()
+            if commit:
+                index.index.commit()
+        except Exception, e:
+            index.index.rollback()
+            raise e
+
 
     @classmethod
     def from_xml_file(cls, xml_file, **kwargs):
@@ -223,7 +228,7 @@ class Book(models.Model):
     @classmethod
     def from_text_and_meta(cls, raw_file, book_info, overwrite=False,
             dont_build=None, search_index=True,
-            search_index_tags=True, search_index_reuse=False):
+            search_index_tags=True):
         if dont_build is None:
             dont_build = set()
         dont_build = set.union(set(dont_build), set(app_settings.DONT_BUILD))
@@ -274,13 +279,14 @@ class Book(models.Model):
         cover_changed = old_cover != book.cover_info()
         obsolete_children = set(b for b in book.children.all()
                                 if b not in children)
+        notify_cover_changed = []
         for n, child_book in enumerate(children):
             new_child = child_book.parent != book
             child_book.parent = book
             child_book.parent_number = n
             child_book.save()
             if new_child or cover_changed:
-                child_book.parent_cover_changed()
+                notify_cover_changed.append(child_book)
         # Disown unfaithful children and let them cope on their own.
         for child in obsolete_children:
             child.parent = None
@@ -288,7 +294,7 @@ class Book(models.Model):
             child.save()
             tasks.fix_tree_tags.delay(child)
             if old_cover:
-                child.parent_cover_changed()
+                notify_cover_changed.append(child)
 
         # delete old fragments when overwriting
         book.fragments.all().delete()
@@ -309,8 +315,10 @@ class Book(models.Model):
                 getattr(book, '%s_file' % format_).build_delay()
 
         if not settings.NO_SEARCH_INDEX and search_index:
-            book.search_index(index_tags=search_index_tags, reuse_index=search_index_reuse)
-            #index_book.delay(book.id, book_info)
+            tasks.index_book.delay(book.id, book_info=book_info, index_tags=search_index_tags)
+
+        for child in notify_cover_changed:
+            child.parent_cover_changed()
 
         cls.published.send(sender=book)
         return book
@@ -383,6 +391,10 @@ class Book(models.Model):
             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:
@@ -478,19 +490,19 @@ class Book(models.Model):
 
     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 = [(name, Tag.create_url('author', slug))
+                    for name, slug in rel_info['tags']['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:
-            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:
-            names = [tag.name for tag in names]
-
+            names = [tag[0] for tag in names]
         return ', '.join(names)
 
     @classmethod
@@ -503,10 +515,11 @@ class Book(models.Model):
         """
         # 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',
-            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)
 
@@ -552,6 +565,7 @@ class Book(models.Model):
         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"),
@@ -561,7 +575,7 @@ class Book(models.Model):
     }
     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):