From 007edbf0d879c5aff6a883c64e51955ad1645303 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Wed, 4 Jan 2012 14:18:05 +0100 Subject: [PATCH 1/1] add method for splitting a book --- apps/catalogue/models/book.py | 12 ++++++++++++ apps/catalogue/tests/__init__.py | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/apps/catalogue/models/book.py b/apps/catalogue/models/book.py index 89c6267c..13be963d 100755 --- a/apps/catalogue/models/book.py +++ b/apps/catalogue/models/book.py @@ -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 # ============= diff --git a/apps/catalogue/tests/__init__.py b/apps/catalogue/tests/__init__.py index b03701f8..600dbd20 100755 --- a/apps/catalogue/tests/__init__.py +++ b/apps/catalogue/tests/__init__.py @@ -70,3 +70,15 @@ class ManipulationTests(TestCase): self.assertEqual(Book.objects.all().count(), 2) self.assertEqual(self.book1.materialize(), 'book 1') self.assertEqual(self.book2.materialize(), 'book 2') + + def test_split_book(self): + self.book1.chunk_set.create(number=2, title='Second chunk', + slug='book3') + self.book1[1].commit('I survived!') + self.assertEqual(len(self.book1), 2) + self.book1.split() + self.assertEqual(set([b.slug for b in Book.objects.all()]), + set(['book2', '1', 'book3'])) + self.assertEqual( + Book.objects.get(slug='book3').materialize(), + 'I survived!') -- 2.20.1