+@register.inclusion_tag('catalogue/book_wide.html', takes_context=True)
+def book_wide(context, book):
+ book_themes = book.related_themes()
+ extra_info = book.extra_info
+ hide_about = extra_info.get('about', '').startswith('http://wiki.wolnepodreczniki.pl')
+
+ return {
+ 'book': book,
+ 'main_link': reverse('book_text', args=[book.slug]) if book.html_file else None,
+ 'related': book.related_info(),
+ 'extra_info': extra_info,
+ 'hide_about': hide_about,
+ 'themes': book_themes,
+ 'request': context.get('request'),
+ }
+
+
+@register.inclusion_tag('catalogue/book_short.html', takes_context=True)
+def book_short(context, book):
+ return {
+ 'book': book,
+ 'main_link': book.get_absolute_url(),
+ 'related': book.related_info(),
+ 'request': context.get('request'),
+ }
+
+
+@register.inclusion_tag('catalogue/book_mini_box.html')
+def book_mini(book):
+ author_str = ", ".join(name
+ for name, url in book.related_info()['tags']['author'])
+ return {
+ 'book': book,
+ 'author_str': author_str,
+ }
+
+
+@register.inclusion_tag('catalogue/work-list.html', takes_context=True)
+def work_list(context, object_list):
+ request = context.get('request')
+ if object_list:
+ object_type = type(object_list[0]).__name__
+ 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, random=1):
+ cache_key = "catalogue.related_books.%d.%d" % (book.id, limit - random)
+ related = cache.get(cache_key)
+ if related is None:
+ related = list(Book.objects.filter(
+ common_slug=book.common_slug).exclude(pk=book.pk)[:limit])
+ limit -= len(related)
+ if limit > random:
+ related += Book.tagged.related_to(book,
+ Book.objects.exclude(common_slug=book.common_slug),
+ ignore_by_tag=book.book_tag())[:limit-random]
+ cache.set(cache_key, related, 1800)
+ if random:
+ random_books = Book.objects.exclude(
+ pk__in=[b.pk for b in related] + [book.pk])
+ if random == 1:
+ count = random_books.count()
+ if count:
+ related.append(random_books[randint(0, count - 1)])
+ else:
+ related += list(random_books.order_by('?')[:random])
+ return {
+ 'books': related,
+ }