From 0e8c1e1029c9a8039600d87553f206410647e6b4 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Wed, 16 Jun 2010 17:01:30 +0200 Subject: [PATCH] allow multiple tags (#303) --- apps/catalogue/models.py | 29 +++++++++++-------- apps/catalogue/tests/book_import.py | 21 ++++++++++++++ apps/catalogue/tests/templatetags.py | 8 ++--- .../templates/catalogue/book_detail.html | 9 +++--- 4 files changed, 47 insertions(+), 20 deletions(-) diff --git a/apps/catalogue/models.py b/apps/catalogue/models.py index cf270f297..90ecb1a3c 100644 --- a/apps/catalogue/models.py +++ b/apps/catalogue/models.py @@ -327,18 +327,23 @@ class Book(models.Model): book.save() book_tags = [] - for category in ('kind', 'genre', 'author', 'epoch'): - tag_name = getattr(book_info, category) - tag_sort_key = tag_name - if category == 'author': - tag_sort_key = tag_name.last_name - tag_name = ' '.join(tag_name.first_names) + ' ' + tag_name.last_name - tag, created = Tag.objects.get_or_create(slug=slughifi(tag_name), category=category) - if created: - tag.name = tag_name - tag.sort_key = slughifi(tag_sort_key) - tag.save() - book_tags.append(tag) + categories = (('kinds', 'kind'), ('genres', 'genre'), ('authors', 'author'), ('epochs', 'epoch')) + for field_name, category in categories: + try: + tag_names = getattr(book_info, field_name) + except: + tag_names = [getattr(book_info, category)] + for tag_name in tag_names: + tag_sort_key = tag_name + if category == 'author': + tag_sort_key = tag_name.last_name + tag_name = ' '.join(tag_name.first_names) + ' ' + tag_name.last_name + tag, created = Tag.objects.get_or_create(slug=slughifi(tag_name), category=category) + if created: + tag.name = tag_name + tag.sort_key = slughifi(tag_sort_key) + tag.save() + book_tags.append(tag) book.tags = book_tags diff --git a/apps/catalogue/tests/book_import.py b/apps/catalogue/tests/book_import.py index 50a8cb685..e5fa031d4 100644 --- a/apps/catalogue/tests/book_import.py +++ b/apps/catalogue/tests/book_import.py @@ -106,3 +106,24 @@ class BookImportLogicTests(WLTestCase): # the old tag shouldn't disappear models.Tag.objects.get(slug="jim-lazy", category="author") + + def test_multiple_tags(self): + BOOK_TEXT = """""" + self.book_info.authors = self.book_info.author, PersonStub(("Joe",), "Dilligent"), + self.book_info.kinds = self.book_info.kind, 'Y-Kind', + self.book_info.genres = self.book_info.genre, 'Y-Genre', + self.book_info.epochs = self.book_info.epoch, 'Y-Epoch', + + self.expected_tags.extend([ + ('author', 'joe-dilligent'), + ('genre', 'y-genre'), + ('epoch', 'y-epoch'), + ('kind', 'y-kind'), + ]) + self.expected_tags.sort() + + book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info) + tags = [ (tag.category, tag.slug) for tag in book.tags ] + tags.sort() + + self.assertEqual(tags, self.expected_tags) diff --git a/apps/catalogue/tests/templatetags.py b/apps/catalogue/tests/templatetags.py index 5b1283c10..7a2ac36b9 100644 --- a/apps/catalogue/tests/templatetags.py +++ b/apps/catalogue/tests/templatetags.py @@ -10,11 +10,11 @@ class BookDescTests(WLTestCase): def setUp(self): WLTestCase.setUp(self) - author = PersonStub(("Common",), "Man") + authors = PersonStub(("Common",), "Man"), PersonStub(("Jane",), "Doe") - child_info = BookInfoStub(author=author, genre="Genre", epoch='Epoch', kind="Kind", + child_info = BookInfoStub(authors=authors, genre="Genre", epoch='Epoch', kind="Kind", **info_args(u"Child")) - parent_info = BookInfoStub(author=author, genre="Genre", epoch='Epoch', kind="Kind", + parent_info = BookInfoStub(authors=authors, genre="Genre", epoch='Epoch', kind="Kind", parts=[child_info.url], **info_args(u"Parent")) @@ -24,4 +24,4 @@ class BookDescTests(WLTestCase): def test_book_desc(self): """ book description should return authors, ancestors, book """ - self.assertEqual(catalogue_tags.book_title(self.child), 'Common Man, Parent, Child') + self.assertEqual(catalogue_tags.book_title(self.child), 'Jane Doe, Common Man, Parent, Child') diff --git a/wolnelektury/templates/catalogue/book_detail.html b/wolnelektury/templates/catalogue/book_detail.html index 27f2e7287..4a8a9313c 100644 --- a/wolnelektury/templates/catalogue/book_detail.html +++ b/wolnelektury/templates/catalogue/book_detail.html @@ -83,27 +83,28 @@

{% trans "Details" %}

-- 2.20.1