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_comments_xtd.models import XtdComment
6 from django.contrib import comments
7 from django.core.urlresolvers import reverse
8 from django import template
9 from migdal.models import Category, Entry
10 from migdal import settings
11 from django.utils.translation import ugettext_lazy as _
13 register = template.Library()
16 @register.simple_tag(takes_context=True)
17 def entry_begin(context, entry):
18 t = template.loader.select_template((
19 'migdal/entry/%s/entry_begin.html' % entry.type,
20 'migdal/entry/entry_begin.html',
23 'request': context['request'],
26 return t.render(template.Context(context))
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',
36 'request': context['request'],
39 return t.render(template.Context(context))
42 @register.inclusion_tag('migdal/categories.html', takes_context=True)
43 def categories(context, taxonomy):
45 'request': context['request'],
46 'object_list': Category.objects.filter(taxonomy=taxonomy)
51 @register.inclusion_tag('migdal/last_comments.html')
52 def last_comments(limit=10):
53 return {'object_list':
54 XtdComment.objects.filter(is_public=True, is_removed=False).order_by('-submit_date')[:limit]}
57 @register.inclusion_tag(['comments/form.html'])
58 def entry_comment_form(entry):
60 'form': comments.get_form()(entry),
61 'next': entry.get_absolute_url(),
65 class MenuItem(object):
68 def __init__(self, title, url, html_id=None):
71 self.html_id = html_id
73 def check_active(self, chooser, value):
74 self.active = chooser == 'url' and value == self.url
77 class ModelMenuItem(object):
78 def __init__(self, obj, html_id=None):
80 self.title = unicode(obj)
81 self.url = obj.get_absolute_url()
82 self.html_id = html_id
84 def check_active(self, chooser, value):
85 self.active = (chooser == 'object' and value == self.obj or
86 chooser == 'objects' and self.obj in value)
89 class EntryTypeMenuItem(object):
90 def __init__(self, title, type_, html_id=None):
93 self.url = reverse('migdal_entry_list_%s' % type_)
94 self.html_id = html_id
96 def check_active(self, chooser, value):
97 self.active = (chooser == 'object' and isinstance(value, Entry)
98 and value.type == self.type or
99 chooser == 'entry_type' and value == self.type)
101 @register.inclusion_tag('migdal/menu.html', takes_context=True)
102 def main_menu(context, chooser=None, value=None):
104 ModelMenuItem(Entry.objects.get(slug_pl='o-nas')),
105 EntryTypeMenuItem(_(u'Publications'), u'publications'),
106 MenuItem(_(u'Events'), reverse('events')),
107 ModelMenuItem(Category.objects.get(slug_pl='stanowisko')),
108 ModelMenuItem(Category.objects.get(slug_pl='pierwsza-pomoc')),
110 if context['request'].LANGUAGE_CODE == 'pl':
111 items.append(MenuItem(u'en', '/en/', html_id='item-lang'))
113 items.append(MenuItem(u'pl', '/', html_id='item-lang'))
115 item.check_active(chooser, value)
116 return {'items': items}