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.
5 from random import randint
6 from django.db.models import Q
7 from django import template
8 from catalogue.models import Book
9 from social.models import Cite
10 from social.utils import likes, cites_for_tags
12 register = template.Library()
14 register.filter('likes', likes)
17 @register.assignment_tag(takes_context=True)
18 def choose_cite(context, ctx=None):
19 """Choose a cite for main page, for book or for set of tags."""
21 request = context['request']
22 assert request.user.is_staff
23 assert 'choose_cite' in request.GET
24 cite = Cite.objects.get(pk=request.GET['choose_cite'])
25 except (AssertionError, Cite.DoesNotExist):
27 cites = Cite.objects.all()
28 elif isinstance(ctx, Book):
29 cites = Cite.objects.filter(Q(book=ctx) | Q(book__ancestor=ctx))
31 cites = cites_for_tags(ctx)
32 stickies = cites.filter(sticky=True)
33 count = stickies.count()
35 cite = stickies[randint(0, count - 1)]
39 cite = cites[randint(0, count - 1)]
45 @register.inclusion_tag('social/cite_promo.html')
46 def render_cite(cite):
52 @register.inclusion_tag('social/cite_promo.html', takes_context=True)
53 def cite_promo(context, ctx=None, fallback=False):
55 'cite': choose_cite(context, ctx),
61 @register.inclusion_tag('social/shelf_tags.html', takes_context=True)
62 def shelf_tags(context, book):
63 user = context['request'].user
64 if not user.is_authenticated():
67 tags = book.tags.filter(category='set', user=user).exclude(name='')