Added inclusion tag for book.
[wolnelektury.git] / 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 w danym zestawie
58     if len(self) == 1 and 'set' in self:
59         return u'Zestaw %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 wolnelektury.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.inclusion_tag('catalogue/_book.html')
121 def book(book):
122     tags = book.tags.filter(~Q(category__in=('set', 'theme')))
123     tags = [u'<a href="%s">%s</a>' % (tag.get_absolute_url(), tag.name) for tag in tags]
124     
125     formats = []
126     if book.html_file:
127         formats.append(u'<a href="%s">Czytaj online</a>' % book.html_file.url)
128     if book.pdf_file:
129         formats.append(u'<a href="%s">Plik PDF</a>' % book.pdf_file.url)
130     if book.odt_file:
131         formats.append(u'<a href="%s">Plik ODT</a>' % book.odt_file.url)
132         
133     return {'book': book, 'tags': tags, 'formats': formats}
134
135
136 @register.tag
137 def catalogue_url(parser, token):
138     bits = token.split_contents()
139     tag_name = bits[0]
140     
141     tags_to_add = []
142     tags_to_remove = []
143     for bit in bits[1:]:
144         if bit[0] == '-':
145             tags_to_remove.append(bit[1:])
146         else:
147             tags_to_add.append(bit)
148     
149     return CatalogueURLNode(tags_to_add, tags_to_remove)
150
151
152 class CatalogueURLNode(Node):
153     def __init__(self, tags_to_add, tags_to_remove):
154         self.tags_to_add = [Variable(tag) for tag in tags_to_add]
155         self.tags_to_remove = [Variable(tag) for tag in tags_to_remove]
156     
157     def render(self, context):
158         tags_to_add = []
159         tags_to_remove = []
160
161         for tag_variable in self.tags_to_add:
162             tag = tag_variable.resolve(context)
163             if isinstance(tag, (list, dict)):
164                 tags_to_add += [t for t in tag]
165             else:
166                 tags_to_add.append(tag)
167
168         for tag_variable in self.tags_to_remove:
169             tag = tag_variable.resolve(context)
170             if iterable(tag):
171                 tags_to_remove += [t for t in tag]
172             else:
173                 tags_to_remove.append(tag)
174             
175         tag_slugs = [tag.slug for tag in tags_to_add]
176         for tag in tags_to_remove:
177             try:
178                 tag_slugs.remove(tag.slug)
179             except KeyError:
180                 pass
181         
182         if len(tag_slugs) > 0:
183             return reverse('tagged_book_list', kwargs={'tags': '/'.join(tag_slugs)})
184         else:
185             return reverse('main_page')
186
187
188 @register.inclusion_tag('catalogue/latest_blog_posts.html')
189 def latest_blog_posts(feed_url, posts_to_show=5):
190     import feedparser
191     import datetime
192     
193     feed = feedparser.parse(feed_url)
194     posts = []
195     for i in range(posts_to_show):
196         pub_date = feed['entries'][i].updated_parsed
197         published = datetime.date(pub_date[0], pub_date[1], pub_date[2] )
198         posts.append({
199             'title': feed['entries'][i].title,
200             'summary': feed['entries'][i].summary,
201             'link': feed['entries'][i].link,
202             'date': published,
203             })
204     return {'posts': posts}
205