1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
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
12 register = template.Library()
15 @register.simple_tag(takes_context=True)
16 def likes_book(context, book):
17 request = context['request']
18 return likes(request.user, book, request)
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)
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():
32 book = Book.objects.get(pk=book_id)
33 lks = likes(request.user, book, request)
38 tags = book.tags.filter(category='set', user=request.user).exclude(name='')
42 return template.loader.render_to_string('social/shelf_tags.html', ctx)
43 return lazy(get_value, str)()
46 @register.inclusion_tag('social/carousel.html', takes_context=True)
47 def carousel(context, slug):
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)
56 for item in carousel.carouselitem_set.all().select_related('banner')
59 request = context['request']
60 if 'banner' in request.GET:
62 banner_id = int(request.GET['banner'])
63 except (TypeError, ValueError):
67 index = [b.pk for b in banners].index(banner_id)
69 if request.user.is_staff:
70 # Staff is allowed to preview any banner.
72 banners.insert(0, Cite.objects.get(pk=banner_id))
73 except Cite.DoesNotExist:
76 # Put selected banner to front.
77 banners = [banners[index]] + banners[:index] + banners[index+1:]
85 @register.inclusion_tag('social/embed_video.html')
87 m = re.match(r'https://www.youtube.com/watch\?v=([^&;]+)', url)
89 'youtube_id': m.group(1) if m else None,