Tested for Django 1.7-1.11
[django-migdal.git] / migdal / templatetags / migdal_tags.py
1 # -*- coding: utf-8 -*-
2 # This file is part of PrawoKultury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 #
5 from django.shortcuts import get_object_or_404
6 from django_comments_xtd.models import XtdComment
7 import django_comments as comments
8 import django
9 from django import template
10 from migdal import app_settings
11 from migdal.models import Category, Entry
12
13 register = template.Library()
14
15
16 if django.VERSION < (1, 8):
17     # See https://docs.djangoproject.com/en/2.2/releases/1.8/#rendering-templates-loaded-by-get-template-with-a-context
18     context_for_get_template = template.Context
19 else:
20     context_for_get_template = lambda x: x
21
22
23
24 @register.simple_tag(takes_context=True)
25 def entry_begin(context, entry, detail=False):
26     t = template.loader.select_template((
27         'migdal/entry/%s/entry_begin.html' % entry.type,
28         'migdal/entry/entry_begin.html',
29     ))
30     context = {
31         'request': context['request'],
32         'object': entry,
33         'detail': detail,
34     }
35     return t.render(context_for_get_template(context))
36
37
38 @register.simple_tag(takes_context=True)
39 def entry_short(context, entry):
40     t = template.loader.select_template((
41         'migdal/entry/%s/entry_short.html' % entry.type,
42         'migdal/entry/entry_short.html',
43     ))
44     context = {
45         'request': context['request'],
46         'object': entry,
47     }
48     return t.render(context_for_get_template(context))
49
50
51 @register.simple_tag(takes_context=True)
52 def entry_promobox(context, entry, counter):
53     t = template.loader.select_template((
54         'migdal/entry/%s/entry_promobox.html' % entry.type,
55         'migdal/entry/entry_promobox.html',
56     ))
57     context = {
58         'request': context['request'],
59         'object': entry,
60         'counter': counter,
61     }
62     return t.render(context_for_get_template(context))
63
64
65 @register.inclusion_tag('migdal/categories.html', takes_context=True)
66 def categories(context, taxonomy):
67     context = {
68         'request': context['request'],
69         'object_list': Category.objects.filter(taxonomy=taxonomy).exclude(entry__isnull=True)
70     }
71     return context
72
73
74 @register.inclusion_tag('migdal/last_comments.html')
75 def last_comments(limit=app_settings.LAST_COMMENTS):
76     return {
77         'object_list': XtdComment.objects.filter(is_public=True, is_removed=False).order_by('-submit_date')[:limit]}
78
79
80 @register.inclusion_tag(['comments/form.html'])
81 def entry_comment_form(entry):
82     return {
83             'form': comments.get_form()(entry),
84             'next': entry.get_absolute_url(),
85         }
86
87
88 @register.simple_tag
89 def entry_url(slug, lang='pl'):
90     entry = get_object_or_404(Entry, **{'slug_%s' % lang: slug})
91     return entry.get_absolute_url()