9c7b15b7fbda0c63b638bf5fe6829efda3babfe3
[prawokultury.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 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 _
12
13 register = template.Library()
14
15
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',
21     ))
22     context = {
23         'request': context['request'],
24         'object': entry,
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):
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     }
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)
60     }
61     return context
62
63
64 @register.inclusion_tag('migdal/last_comments.html')
65 def last_comments(limit=10):
66     return {'object_list': 
67         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         }
76
77
78 class MenuItem(object):
79     html_id = None
80
81     def __init__(self, title, url, html_id=None):
82         self.title = title
83         self.url = url
84         self.html_id = html_id
85
86     def check_active(self, chooser, value):
87         self.active = chooser == 'url' and value == self.url
88
89
90 class ModelMenuItem(object):
91     def __init__(self, obj, html_id=None):
92         self.obj = obj
93         self.title = unicode(obj)
94         self.url = obj.get_absolute_url()
95         self.html_id = html_id
96
97     def check_active(self, chooser, value):
98         self.active = (chooser == 'object' and value == self.obj or
99                         chooser == 'objects' and self.obj in value)
100
101
102 class EntryTypeMenuItem(object):
103     def __init__(self, title, type_, html_id=None):
104         self.type = type_
105         self.title = title
106         self.url = reverse('migdal_entry_list_%s' % type_)
107         self.html_id = html_id
108
109     def check_active(self, chooser, value):
110         self.active = (chooser == 'object' and isinstance(value, Entry)
111                         and value.type == self.type or
112                         chooser == 'entry_type' and value == self.type)
113
114 @register.inclusion_tag('migdal/menu.html', takes_context=True)
115 def main_menu(context, chooser=None, value=None):
116     items = [
117         ModelMenuItem(Entry.objects.get(slug_pl='o-nas')),
118         EntryTypeMenuItem(_(u'Publications'), u'publications'),
119         MenuItem(_(u'Events'), reverse('events')),
120         ModelMenuItem(Category.objects.get(slug_pl='stanowisko')),
121         ModelMenuItem(Category.objects.get(slug_pl='pierwsza-pomoc')),
122     ]
123     if context['request'].LANGUAGE_CODE == 'pl':
124         items.append(MenuItem(u'en', '/en/', html_id='item-lang'))
125     else:
126         items.append(MenuItem(u'pl', '/', html_id='item-lang'))
127     for item in items:
128         item.check_active(chooser, value)
129     return {'items': items}