+ return locals()
+
+
+@register.inclusion_tag('catalogue/fragment_promo.html')
+def fragment_promo(arg=None):
+ if arg is None:
+ fragments = Fragment.objects.all().order_by('?')
+ fragment = fragments[0] if fragments.exists() else None
+ elif isinstance(arg, Book):
+ fragment = arg.choose_fragment()
+ else:
+ fragments = Fragment.tagged.with_all(arg).order_by('?')
+ fragment = fragments[0] if fragments.exists() else None
+
+ return {
+ 'fragment': fragment,
+ }
+
+
+@register.inclusion_tag('catalogue/related_books.html')
+def related_books(book, limit=6):
+ related = list(Book.objects.filter(
+ common_slug=book.common_slug).exclude(pk=book.pk)[:limit])
+ limit -= len(related)
+ if limit:
+ related += Book.tagged.related_to(book,
+ Book.objects.exclude(common_slug=book.common_slug),
+ ignore_by_tag=book.book_tag())[:limit]
+ return {
+ 'books': related,
+ }
+
+
+@register.inclusion_tag('catalogue/menu.html')
+def catalogue_menu():
+ tags = Tag.objects.filter(
+ category__in=('author', 'epoch', 'genre', 'kind', 'theme')
+ ).exclude(book_count=0)
+ return split_tags(tags)
+
+
+
+@register.simple_tag
+def tag_url(category, slug):
+ return reverse('catalogue.views.tagged_object_list', args=[
+ '/'.join((Tag.categories_dict[category], slug))
+ ])