From: Radek Czajka Date: Thu, 1 Jul 2010 10:59:16 +0000 (+0200) Subject: Fixed #716: empty theme error X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/90a3704b70f16b87bc17b3a1f2c6d497316c9019?ds=sidebyside;hp=--cc Fixed #716: empty theme error --- 90a3704b70f16b87bc17b3a1f2c6d497316c9019 diff --git a/apps/catalogue/models.py b/apps/catalogue/models.py index a7e04f1a2..922828440 100644 --- a/apps/catalogue/models.py +++ b/apps/catalogue/models.py @@ -403,25 +403,30 @@ class Book(models.Model): # Extract fragments closed_fragments, open_fragments = html.extract_fragments(book.html_file.path) for fragment in closed_fragments.values(): - text = fragment.to_string() - short_text = '' - if (len(MarkupString(text)) > 240): - short_text = unicode(MarkupString(text)[:160]) - new_fragment, created = Fragment.objects.get_or_create(anchor=fragment.id, book=book, - defaults={'text': text, 'short_text': short_text}) - try: theme_names = [s.strip() for s in fragment.themes.split(',')] except AttributeError: continue themes = [] for theme_name in theme_names: + if not theme_name: + continue tag, created = Tag.objects.get_or_create(slug=slughifi(theme_name), category='theme') if created: tag.name = theme_name tag.sort_key = slughifi(theme_name) tag.save() themes.append(tag) + if not themes: + continue + + text = fragment.to_string() + short_text = '' + if (len(MarkupString(text)) > 240): + short_text = unicode(MarkupString(text)[:160]) + new_fragment, created = Fragment.objects.get_or_create(anchor=fragment.id, book=book, + defaults={'text': text, 'short_text': short_text}) + new_fragment.save() new_fragment.tags = set(book_tags + themes + [book_tag]) diff --git a/apps/catalogue/tests/book_import.py b/apps/catalogue/tests/book_import.py index 3cb94cb98..cb63fc069 100644 --- a/apps/catalogue/tests/book_import.py +++ b/apps/catalogue/tests/book_import.py @@ -78,6 +78,32 @@ class BookImportLogicTests(WLTestCase): self.assert_(('theme', 'love') in [ (tag.category, tag.slug) for tag in book.fragments.all()[0].tags ]) + def test_book_with_empty_theme(self): + """ empty themes should be ignored """ + + BOOK_TEXT = """ + + , Love , , Ala ma kota + + """ + + book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info) + self.assert_([('theme', 'love')], + [ (tag.category, tag.slug) for tag in book.fragments.all()[0].tags.filter(category='theme') ]) + + def test_book_with_no_theme(self): + """ fragments with no themes shouldn't be created at all """ + + BOOK_TEXT = """ + + Ala ma kota + + """ + + book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info) + self.assertEqual(book.fragments.count(), 0) + self.assertEqual(book.tags.filter(category='theme').count(), 0) + def test_book_replace_title(self): BOOK_TEXT = """""" book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)