Refactored tag lists to template tag.
[wolnelektury.git] / apps / catalogue / templatetags / catalogue_tags.py
1 # -*- coding: utf-8 -*-
2 from django import template
3 from django.template import Node, Variable
4 from django.utils.encoding import smart_str
5 from django.core.urlresolvers import reverse
6 from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
7 from django.db.models import Q
8
9
10 register = template.Library()
11
12
13 class RegistrationForm(UserCreationForm):
14     def as_ul(self):
15         "Returns this form rendered as HTML <li>s -- excluding the <ul></ul>."
16         return self._html_output(u'<li>%(errors)s%(label)s %(field)s<span class="help-text">%(help_text)s</span></li>', u'<li>%s</li>', '</li>', u' %s', False)
17
18
19 class LoginForm(AuthenticationForm):
20     def as_ul(self):
21         "Returns this form rendered as HTML <li>s -- excluding the <ul></ul>."
22         return self._html_output(u'<li>%(errors)s%(label)s %(field)s<span class="help-text">%(help_text)s</span></li>', u'<li>%s</li>', '</li>', u' %s', False)
23
24
25 def iterable(obj):
26     try:
27         iter(obj)
28         return True
29     except TypeError:
30         return False
31
32
33 def capfirst(text):
34     try:
35         return '%s%s' % (text[0].upper(), text[1:])
36     except IndexError:
37         return ''
38
39
40 @register.simple_tag
41 def title_from_tags(tags):
42     def split_tags(tags):
43         result = {}
44         for tag in tags:
45             result[tag.category] = tag
46         return result
47     
48     class Flection(object):
49         def get_case(self, name, flection):
50             return name
51     flection = Flection()
52     
53     self = split_tags(tags)
54     
55     title = u''
56     
57     # Specjalny przypadek oglądania wszystkich lektur na danej półce
58     if len(self) == 1 and 'set' in self:
59         return u'Półka %s' % self['set']
60     
61     # Specjalny przypadek "Twórczość w pozytywizmie", wtedy gdy tylko epoka
62     # jest wybrana przez użytkownika
63     if 'epoch' in self and len(self) == 1:
64         text = u'Twórczość w %s' % flection.get_case(unicode(self['epoch']), u'miejscownik')
65         return capfirst(text)
66     
67     # Specjalny przypadek "Dramat w twórczości Sofoklesa", wtedy gdy podane
68     # są tylko rodzaj literacki i autor
69     if 'kind' in self and 'author' in self and len(self) == 2:
70         text = u'%s w twórczości %s' % (unicode(self['kind']), 
71             flection.get_case(unicode(self['author']), u'dopełniacz'))
72         return capfirst(text)
73     
74     # Przypadki ogólniejsze
75     if 'theme' in self:
76         title += u'Motyw %s' % unicode(self['theme'])
77     
78     if 'genre' in self:
79         if 'theme' in self:
80             title += u' w %s' % flection.get_case(unicode(self['genre']), u'miejscownik')
81         else:
82             title += unicode(self['genre'])
83             
84     if 'kind' in self or 'author' in self or 'epoch' in self:
85         if 'genre' in self or 'theme' in self:
86             if 'kind' in self:
87                 title += u' w %s ' % flection.get_case(unicode(self['kind']), u'miejscownik')
88             else:
89                 title += u' w twórczości '
90         else:
91             title += u'%s ' % unicode(self.get('kind', u'twórczość'))
92             
93     if 'author' in self:
94         title += flection.get_case(unicode(self['author']), u'dopełniacz')
95     elif 'epoch' in self:
96         title += flection.get_case(unicode(self['epoch']), u'dopełniacz')
97     
98     return capfirst(title)
99
100
101 @register.simple_tag
102 def user_creation_form():
103     return RegistrationForm(prefix='registration').as_ul()
104
105
106 @register.simple_tag
107 def authentication_form():
108     return LoginForm(prefix='login').as_ul()
109
110
111 @register.inclusion_tag('catalogue/breadcrumbs.html')
112 def breadcrumbs(tags, search_form=True):
113     from catalogue.forms import SearchForm
114     context = {'tag_list': tags}
115     if search_form:
116         context['search_form'] = SearchForm(tags=tags)
117     return context
118
119
120 @register.tag
121 def catalogue_url(parser, token):
122     bits = token.split_contents()
123     tag_name = bits[0]
124     
125     tags_to_add = []
126     tags_to_remove = []
127     for bit in bits[1:]:
128         if bit[0] == '-':
129             tags_to_remove.append(bit[1:])
130         else:
131             tags_to_add.append(bit)
132     
133     return CatalogueURLNode(tags_to_add, tags_to_remove)
134
135
136 class CatalogueURLNode(Node):
137     def __init__(self, tags_to_add, tags_to_remove):
138         self.tags_to_add = [Variable(tag) for tag in tags_to_add]
139         self.tags_to_remove = [Variable(tag) for tag in tags_to_remove]
140     
141     def render(self, context):
142         tags_to_add = []
143         tags_to_remove = []
144
145         for tag_variable in self.tags_to_add:
146             tag = tag_variable.resolve(context)
147             if isinstance(tag, (list, dict)):
148                 tags_to_add += [t for t in tag]
149             else:
150                 tags_to_add.append(tag)
151
152         for tag_variable in self.tags_to_remove:
153             tag = tag_variable.resolve(context)
154             if iterable(tag):
155                 tags_to_remove += [t for t in tag]
156             else:
157                 tags_to_remove.append(tag)
158             
159         tag_slugs = [tag.slug for tag in tags_to_add]
160         for tag in tags_to_remove:
161             try:
162                 tag_slugs.remove(tag.slug)
163             except KeyError:
164                 pass
165         
166         if len(tag_slugs) > 0:
167             return reverse('tagged_object_list', kwargs={'tags': '/'.join(tag_slugs)})
168         else:
169             return reverse('main_page')
170
171
172 @register.inclusion_tag('catalogue/latest_blog_posts.html')
173 def latest_blog_posts(feed_url, posts_to_show=5):
174     import feedparser
175     import datetime
176     
177     feed = feedparser.parse(feed_url)
178     posts = []
179     for i in range(posts_to_show):
180         pub_date = feed['entries'][i].updated_parsed
181         published = datetime.date(pub_date[0], pub_date[1], pub_date[2] )
182         posts.append({
183             'title': feed['entries'][i].title,
184             'summary': feed['entries'][i].summary,
185             'link': feed['entries'][i].link,
186             'date': published,
187             })
188     return {'posts': posts}
189
190
191 @register.inclusion_tag('catalogue/tag_list.html')
192 def tag_list(tags, choices=None):
193     if choices is None:
194         choices = []
195     tag_count = len(tags)
196     return locals()
197