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