f370f97aa470ce9f5e6df833a669e61bb199bf9a
[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_comments_xtd.models import XtdComment
6 import django_comments as comments
7 from django import template
8 from migdal import app_settings
9 from migdal.models import Category
10
11 register = template.Library()
12
13
14 @register.simple_tag(takes_context=True)
15 def entry_begin(context, entry, detail=False):
16     t = template.loader.select_template((
17         'migdal/entry/%s/entry_begin.html' % entry.type,
18         'migdal/entry/entry_begin.html',
19     ))
20     context = {
21         'request': context['request'],
22         'object': entry,
23         'detail': detail,
24     }
25     return t.render(template.Context(context))
26
27
28 @register.simple_tag(takes_context=True)
29 def entry_short(context, entry):
30     t = template.loader.select_template((
31         'migdal/entry/%s/entry_short.html' % entry.type,
32         'migdal/entry/entry_short.html',
33     ))
34     context = {
35         'request': context['request'],
36         'object': entry,
37     }
38     return t.render(template.Context(context))
39
40
41 @register.simple_tag(takes_context=True)
42 def entry_promobox(context, entry, counter):
43     t = template.loader.select_template((
44         'migdal/entry/%s/entry_promobox.html' % entry.type,
45         'migdal/entry/entry_promobox.html',
46     ))
47     context = {
48         'request': context['request'],
49         'object': entry,
50         'counter': counter,
51     }
52     return t.render(template.Context(context))
53
54
55 @register.inclusion_tag('migdal/categories.html', takes_context=True)
56 def categories(context, taxonomy):
57     context = {
58         'request': context['request'],
59         'object_list': Category.objects.filter(taxonomy=taxonomy).exclude(entry__isnull=True)
60     }
61     return context
62
63
64 @register.inclusion_tag('migdal/last_comments.html')
65 def last_comments(limit=app_settings.LAST_COMMENTS):
66     return {
67         'object_list': XtdComment.objects.filter(is_public=True, is_removed=False).order_by('-submit_date')[:limit]}
68
69
70 @register.inclusion_tag(['comments/form.html'])
71 def entry_comment_form(entry):
72     return {
73             'form': comments.get_form()(entry),
74             'next': entry.get_absolute_url(),
75         }