merge
[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', takes_context=True)
16 def cite_promo(context, ctx=None, fallback=False):
17     """Choose"""
18     try:
19         request = context['request']
20         assert request.user.is_staff
21         assert 'choose_cite' in request.GET
22         cite = Cite.objects.get(pk=request.GET['choose_cite'])
23     except AssertionError, Cite.DoesNotExist:
24         if ctx is None:
25             cites = Cite.objects.all()
26         elif isinstance(ctx, Book):
27             cites = ctx.cite_set.all()
28             if not cites.exists():
29                 cites = cites_for_tags([ctx.book_tag()])
30         else:
31             cites = cites_for_tags(ctx)
32         cite = cites.order_by('?')[0] if cites.exists() else None
33
34     return {
35         'cite': cite,
36         'fallback': fallback,
37         'ctx': ctx,
38     }
39
40
41 @register.inclusion_tag('social/shelf_tags.html', takes_context=True)
42 def shelf_tags(context, book):
43     user = context['request'].user
44     if not user.is_authenticated():
45         tags = []
46     else:
47         tags = book.tags.filter(category='set', user=user).exclude(name='')
48     return {'tags': tags}