add method for splitting a book
[redakcja.git] / apps / catalogue / models / book.py
index 7154775..13be963 100755 (executable)
@@ -209,6 +209,18 @@ class Book(models.Model):
         assert not other.chunk_set.exists()
         other.delete()
 
+    def split(self):
+        """Splits all the chunks into separate books."""
+        self.title
+        for chunk in self:
+            book = Book.objects.create(title=chunk.title, slug=chunk.slug,
+                    public=self.public, gallery=self.gallery)
+            book[0].delete()
+            chunk.book = book
+            chunk.number = 1
+            chunk.save()
+        assert not self.chunk_set.exists()
+        self.delete()
 
     # State & cache
     # =============
@@ -271,7 +283,8 @@ class Book(models.Model):
     def get_on_track(self):
         if self.published:
             return -1
-        stages = [ch.stage.ordering for ch in self if ch.stage is not None]
+        stages = [ch.stage.ordering if ch.stage is not None else 0
+                    for ch in self]
         if not len(stages):
             return 0
         return min(stages)