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 import app_settings
10 from migdal.models import Category, Entry
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, detail=False):
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'],
27 return t.render(template.Context(context))
30 @register.simple_tag(takes_context=True)
31 def entry_short(context, entry):
32 t = template.loader.select_template((
33 'migdal/entry/%s/entry_short.html' % entry.type,
34 'migdal/entry/entry_short.html',
37 'request': context['request'],
40 return t.render(template.Context(context))
43 @register.simple_tag(takes_context=True)
44 def entry_promobox(context, entry, counter):
45 t = template.loader.select_template((
46 'migdal/entry/%s/entry_promobox.html' % entry.type,
47 'migdal/entry/entry_promobox.html',
50 'request': context['request'],
54 return t.render(template.Context(context))
57 @register.inclusion_tag('migdal/categories.html', takes_context=True)
58 def categories(context, taxonomy):
60 'request': context['request'],
61 'object_list': Category.objects.filter(taxonomy=taxonomy)
66 @register.inclusion_tag('migdal/last_comments.html')
67 def last_comments(limit=app_settings.LAST_COMMENTS):
68 return {'object_list':
69 XtdComment.objects.filter(is_public=True, is_removed=False).order_by('-submit_date')[:limit]}
72 @register.inclusion_tag(['comments/form.html'])
73 def entry_comment_form(entry):
75 'form': comments.get_form()(entry),
76 'next': entry.get_absolute_url(),
80 class MenuItem(object):
83 def __init__(self, title, url, html_id=None):
86 self.html_id = html_id
88 def check_active(self, chooser, value):
89 self.active = chooser == 'url' and value == self.url
92 class ModelMenuItem(object):
93 def __init__(self, obj, title=None, html_id=None):
95 self.title = title or unicode(obj)
96 self.url = obj.get_absolute_url()
97 self.html_id = html_id
99 def check_active(self, chooser, value):
100 self.active = (chooser == 'object' and value == self.obj or
101 chooser == 'objects' and self.obj in value)
103 class CategoryMenuItem(ModelMenuItem):
104 def check_active(self, chooser, value):
105 super(CategoryMenuItem, self).check_active(chooser, value)
106 self.active = (self.active or
107 (chooser == 'object' and isinstance(value, Entry) and
108 self.obj in value.categories.all()))
111 class EntryTypeMenuItem(object):
112 def __init__(self, title, type_, html_id=None):
115 self.url = reverse('migdal_entry_list_%s' % type_)
116 self.html_id = html_id
118 def check_active(self, chooser, value):
119 self.active = (chooser == 'object' and isinstance(value, Entry)
120 and value.type == self.type or
121 chooser == 'entry_type' and value == self.type)
123 @register.inclusion_tag('migdal/menu.html', takes_context=True)
124 def main_menu(context, chooser=None, value=None):
126 ModelMenuItem(Entry.objects.get(slug_pl='o-nas')),
127 EntryTypeMenuItem(_(u'Publications'), u'publications'),
128 MenuItem(_(u'Events'), reverse('events')),
129 CategoryMenuItem(Category.objects.get(slug_pl='stanowisko'),
130 title=_('Positions')),
131 CategoryMenuItem(Category.objects.get(slug_pl='pierwsza-pomoc'),
132 title=_('First aid in copyright')),
134 if context['request'].LANGUAGE_CODE == 'pl':
135 items.append(MenuItem(u'en', '/en/', html_id='item-lang'))
137 items.append(MenuItem(u'pl', '/', html_id='item-lang'))
139 item.check_active(chooser, value)
140 return {'items': items}