d12920f21692542ea44da72eef45f35c3c324496
[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 import app_settings
10 from migdal.models import Category, Entry
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, detail=False):
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         'detail': detail,
26     }
27     return t.render(template.Context(context))
28
29
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',
35     ))
36     context = {
37         'request': context['request'],
38         'object': entry,
39     }
40     return t.render(template.Context(context))
41
42
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',
48     ))
49     context = {
50         'request': context['request'],
51         'object': entry,
52         'counter': counter,
53     }
54     return t.render(template.Context(context))
55
56
57 @register.inclusion_tag('migdal/categories.html', takes_context=True)
58 def categories(context, taxonomy):
59     context = {
60         'request': context['request'],
61         'object_list': Category.objects.filter(taxonomy=taxonomy
62                 ).exclude(entry__isnull=True)
63     }
64     return context
65
66
67 @register.inclusion_tag('migdal/last_comments.html')
68 def last_comments(limit=app_settings.LAST_COMMENTS):
69     return {'object_list': 
70         XtdComment.objects.filter(is_public=True, is_removed=False).order_by('-submit_date')[:limit]}
71
72
73 @register.inclusion_tag(['comments/form.html'])
74 def entry_comment_form(entry):
75     return {
76             'form': comments.get_form()(entry),
77             'next': entry.get_absolute_url(),
78         }
79
80
81 class MenuItem(object):
82     html_id = None
83
84     def __init__(self, title, url, html_id=None):
85         self.title = title
86         self.url = url
87         self.html_id = html_id
88
89     def check_active(self, chooser, value):
90         self.active = chooser == 'url' and value == self.url
91
92
93 class ModelMenuItem(object):
94     def __init__(self, obj, title=None, html_id=None):
95         self.obj = obj
96         self.title = title or unicode(obj)
97         self.url = obj.get_absolute_url()
98         self.html_id = html_id
99
100     def check_active(self, chooser, value):
101         self.active = (chooser == 'object' and value == self.obj or
102                         chooser == 'objects' and self.obj in value)
103
104 class CategoryMenuItem(ModelMenuItem):
105     def check_active(self, chooser, value):
106         super(CategoryMenuItem, self).check_active(chooser, value)
107         self.active = (self.active or
108                        (chooser == 'object' and isinstance(value, Entry) and
109                         self.obj in value.categories.all()))
110
111
112 class EntryTypeMenuItem(object):
113     def __init__(self, title, type_, html_id=None):
114         self.type = type_
115         self.title = title
116         self.url = reverse('migdal_entry_list_%s' % type_)
117         self.html_id = html_id
118
119     def check_active(self, chooser, value):
120         self.active = (chooser == 'object' and isinstance(value, Entry)
121                         and value.type == self.type or
122                         chooser == 'entry_type' and value == self.type)
123
124 @register.inclusion_tag('migdal/menu.html', takes_context=True)
125 def main_menu(context, chooser=None, value=None):
126     items = [
127         #ModelMenuItem(Entry.objects.get(slug_pl='o-nas')),
128         #MenuItem(_(u'Events'), reverse('events')),
129     ]
130     # TODO: context-aware language switcher
131     if context['request'].LANGUAGE_CODE == 'pl':
132         items.append(MenuItem(u'en', '/en/', html_id='item-lang'))
133     else:
134         items.append(MenuItem(u'pl', '/', html_id='item-lang'))
135     for item in items:
136         item.check_active(chooser, value)
137     return {'items': items}