allow multiple tags (#303)
authorRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Wed, 16 Jun 2010 15:01:30 +0000 (17:01 +0200)
committerRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Wed, 16 Jun 2010 15:01:30 +0000 (17:01 +0200)
apps/catalogue/models.py
apps/catalogue/tests/book_import.py
apps/catalogue/tests/templatetags.py
wolnelektury/templates/catalogue/book_detail.html

index cf270f2..90ecb1a 100644 (file)
@@ -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
 
index 50a8cb6..e5fa031 100644 (file)
@@ -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 = """<utwor />"""
+        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)
index 5b1283c..7a2ac36 100644 (file)
@@ -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')
index 27f2e72..4a8a931 100644 (file)
             <h2>{% trans "Details" %}</h2>
             <ul>
                 <li>
+                       
                     {% trans "Author" %}:
                     {% for tag in categories.author %}
-                    <a href="{{ tag.get_absolute_url }}">{{ tag }}</a>
+                    <a href="{{ tag.get_absolute_url }}">{{ tag }}</a>{% if not forloop.last %}, {% endif %}
                     {% endfor %}
                 </li>
                 <li>
                     {% trans "Epoch" %}:
                     {% for tag in categories.epoch %}
-                    <a href="{{ tag.get_absolute_url }}">{{ tag }}</a>
+                    <a href="{{ tag.get_absolute_url }}">{{ tag }}</a>{% if not forloop.last %}, {% endif %}
                     {% endfor %}
                 </li>
                 <li>
                     {% trans "Kind" %}:
                     {% for tag in categories.kind %}
-                    <a href="{{ tag.get_absolute_url }}">{{ tag }}</a>
+                    <a href="{{ tag.get_absolute_url }}">{{ tag }}</a>{% if not forloop.last %}, {% endif %}
                     {% endfor %}
                 </li>
                 <li>
                     {% trans "Genre" %}:
                     {% for tag in categories.genre %}
-                    <a href="{{ tag.get_absolute_url }}">{{ tag }}</a>
+                    <a href="{{ tag.get_absolute_url }}">{{ tag }}</a>{% if not forloop.last %}, {% endif %}
                     {% endfor %}
                 </li>
             </ul>