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
10 register = template.Library()
13 class RegistrationForm(UserCreationForm):
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)
19 class LoginForm(AuthenticationForm):
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)
35 return '%s%s' % (text[0].upper(), text[1:])
41 def title_from_tags(tags):
45 result[tag.category] = tag
48 class Flection(object):
49 def get_case(self, name, flection):
53 self = split_tags(tags)
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']
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')
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'))
74 # Przypadki ogólniejsze
76 title += u'Motyw %s' % unicode(self['theme'])
80 title += u' w %s' % flection.get_case(unicode(self['genre']), u'miejscownik')
82 title += unicode(self['genre'])
84 if 'kind' in self or 'author' in self or 'epoch' in self:
85 if 'genre' in self or 'theme' in self:
87 title += u' w %s ' % flection.get_case(unicode(self['kind']), u'miejscownik')
89 title += u' w twórczości '
91 title += u'%s ' % unicode(self.get('kind', u'twórczość'))
94 title += flection.get_case(unicode(self['author']), u'dopełniacz')
96 title += flection.get_case(unicode(self['epoch']), u'dopełniacz')
98 return capfirst(title)
102 def user_creation_form():
103 return RegistrationForm(prefix='registration').as_ul()
107 def authentication_form():
108 return LoginForm(prefix='login').as_ul()
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}
116 context['search_form'] = SearchForm(tags=tags)
120 @register.inclusion_tag('catalogue/_book.html')
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]
127 formats.append(u'<a href="%s">Czytaj online</a>' % book.html_file.url)
129 formats.append(u'<a href="%s">Plik PDF</a>' % book.pdf_file.url)
131 formats.append(u'<a href="%s">Plik ODT</a>' % book.odt_file.url)
133 return {'book': book, 'tags': tags, 'formats': formats}
137 def catalogue_url(parser, token):
138 bits = token.split_contents()
145 tags_to_remove.append(bit[1:])
147 tags_to_add.append(bit)
149 return CatalogueURLNode(tags_to_add, tags_to_remove)
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]
157 def render(self, context):
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]
166 tags_to_add.append(tag)
168 for tag_variable in self.tags_to_remove:
169 tag = tag_variable.resolve(context)
171 tags_to_remove += [t for t in tag]
173 tags_to_remove.append(tag)
175 tag_slugs = [tag.slug for tag in tags_to_add]
176 for tag in tags_to_remove:
178 tag_slugs.remove(tag.slug)
182 if len(tag_slugs) > 0:
183 return reverse('tagged_book_list', kwargs={'tags': '/'.join(tag_slugs)})
185 return reverse('main_page')
188 @register.inclusion_tag('catalogue/latest_blog_posts.html')
189 def latest_blog_posts(feed_url, posts_to_show=5):
193 feed = feedparser.parse(feed_url)
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] )
199 'title': feed['entries'][i].title,
200 'summary': feed['entries'][i].summary,
201 'link': feed['entries'][i].link,
204 return {'posts': posts}