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.
5 from django.shortcuts import get_object_or_404
6 from django_comments_xtd.models import XtdComment
7 import django_comments as comments
9 from django import template
10 from migdal import app_settings
11 from migdal.models import Category, Entry
13 register = template.Library()
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
20 context_for_get_template = lambda x: x
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',
31 'request': context['request'],
35 return t.render(context_for_get_template(context))
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',
45 'request': context['request'],
48 return t.render(context_for_get_template(context))
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',
58 'request': context['request'],
62 return t.render(context_for_get_template(context))
65 @register.inclusion_tag('migdal/categories.html', takes_context=True)
66 def categories(context, taxonomy):
68 'request': context['request'],
69 'object_list': Category.objects.filter(taxonomy=taxonomy).exclude(entry__isnull=True)
74 @register.inclusion_tag('migdal/last_comments.html')
75 def last_comments(limit=app_settings.LAST_COMMENTS):
77 'object_list': XtdComment.objects.filter(is_public=True, is_removed=False).order_by('-submit_date')[:limit]}
80 @register.inclusion_tag(['comments/form.html'])
81 def entry_comment_form(entry):
83 'form': comments.get_form()(entry),
84 'next': entry.get_absolute_url(),
89 def entry_url(slug, lang='pl'):
90 entry = get_object_or_404(Entry, **{'slug_%s' % lang: slug})
91 return entry.get_absolute_url()