Merge branch 'master' into sunburnt
[wolnelektury.git] / apps / catalogue / models / book.py
index e6f08a9..5bc1e10 100644 (file)
@@ -189,20 +189,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()
+            if commit:
+                index.index.commit()
+        except Exception, e:
+            index.index.rollback()
+            raise e
+
 
     @classmethod
     def from_xml_file(cls, xml_file, **kwargs):
@@ -223,7 +223,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,12 +274,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 cover_changed:
-                child_book.parent_cover_changed()
+            if new_child or 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
@@ -287,7 +289,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()
@@ -308,8 +310,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