display changes, mostly in book boxes
[wolnelektury.git] / apps / social / templatetags / social_tags.py
1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 #
5 from django import template
6 from catalogue.models import Book
7 from social.models import Cite
8 from social.utils import likes, cites_for_tags
9
10 register = template.Library()
11
12 register.filter('likes', likes)
13
14
15 @register.inclusion_tag('social/cite_promo.html')
16 def cite_promo(ctx=None, fallback=False):
17     """Choose"""
18     if ctx is None:
19         cites = Cite.objects.all()
20     elif isinstance(ctx, Book):
21         cites = ctx.cite_set.all()
22         if not cites.exists():
23             cites = cites_for_tags([ctx.book_tag()])
24     else:
25         cites = cites_for_tags(ctx)
26
27     return {
28         'cite': cites.order_by('?')[0] if cites.exists() else None,
29         'fallback': fallback,
30         'ctx': ctx,
31     }
32
33
34 @register.inclusion_tag('social/shelf_tags.html', takes_context=True)
35 def shelf_tags(context, book):
36     user = context['request'].user
37     if not user.is_authenticated():
38         tags = []
39     else:
40         tags = book.tags.filter(category='set', user=user).exclude(name='')
41     return {'tags': tags}