596c36d302568e91f320507e260e51b22112801c
[wolnelektury.git] / src / social / templatetags / social_tags.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
4 import re
5 from django import template
6 from django.utils.functional import lazy
7 from django.utils.cache import add_never_cache_headers
8 from catalogue.models import Book
9 from social.utils import likes, get_or_choose_cite
10 from ..models import Carousel, Cite
11
12 register = template.Library()
13
14
15 @register.simple_tag(takes_context=True)
16 def likes_book(context, book):
17     request = context['request']
18     return likes(request.user, book, request)
19
20
21 @register.simple_tag(takes_context=True)
22 def choose_cite(context, book_id=None, tag_ids=None):
23     request = context['request']
24     return get_or_choose_cite(request, book_id, tag_ids)
25
26
27 @register.simple_tag(takes_context=True)
28 def book_shelf_tags(context, book_id):
29     request = context['request']
30     if not request.user.is_authenticated:
31         return ''
32     book = Book.objects.get(pk=book_id)
33     lks = likes(request.user, book, request)
34
35     def get_value():
36         if not lks:
37             return ''
38         tags = book.tags.filter(category='set', user=request.user).exclude(name='')
39         if not tags:
40             return ''
41         ctx = {'tags': tags}
42         return template.loader.render_to_string('social/shelf_tags.html', ctx)
43     return lazy(get_value, str)()
44
45
46 @register.inclusion_tag('social/carousel.html', takes_context=True)
47 def carousel(context, slug):
48     # TODO: cache
49     try:
50         carousel = Carousel.objects.get(slug=slug)
51     except Carousel.DoesNotExist:
52         # TODO: add sanity check for install.
53         carousel = Carousel.objects.create(slug=slug)
54     banners = [
55             item.get_banner()
56             for item in carousel.carouselitem_set.all().select_related('banner')
57             ]
58
59     request = context['request']
60     if 'banner' in request.GET:
61         try:
62             banner_id = int(request.GET['banner'])
63         except (TypeError, ValueError):
64             pass
65         else:
66             try:
67                 index = [b.pk for b in banners].index(banner_id)
68             except ValueError:
69                 if request.user.is_staff:
70                     # Staff is allowed to preview any banner.
71                     try:
72                         banners.insert(0, Cite.objects.get(pk=banner_id))
73                     except Cite.DoesNotExist:
74                         pass
75             else:
76                 # Put selected banner to front.
77                 banners = [banners[index]] + banners[:index] + banners[index+1:]
78
79     return {
80         'carousel': carousel,
81         'banners': banners,
82     }
83
84
85 @register.inclusion_tag('social/embed_video.html')
86 def embed_video(url):
87     m = re.match(r'https://www.youtube.com/watch\?v=([^&;]+)', url)
88     return {
89         'youtube_id': m.group(1) if m else None,
90     }