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
# 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)
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"))
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')
<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>