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
9 register = template.Library()
12 class RegistrationForm(UserCreationForm):
14 "Returns this form rendered as HTML <li>s -- excluding the <ul></ul>."
15 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)
18 class LoginForm(AuthenticationForm):
20 "Returns this form rendered as HTML <li>s -- excluding the <ul></ul>."
21 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)
34 return '%s%s' % (text[0].upper(), text[1:])
40 def title_from_tags(tags):
44 result[tag.category] = tag
47 class Flection(object):
48 def get_case(self, name, flection):
52 self = split_tags(tags)
56 # Specjalny przypadek oglądania wszystkich lektur w danym zestawie
57 if len(self) == 1 and 'set' in self:
58 return u'Zestaw %s' % self['set']
60 # Specjalny przypadek "Twórczość w pozytywizmie", wtedy gdy tylko epoka
61 # jest wybrana przez użytkownika
62 if 'epoch' in self and len(self) == 1:
63 text = u'Twórczość w %s' % flection.get_case(unicode(self['epoch']), u'miejscownik')
66 # Specjalny przypadek "Dramat w twórczości Sofoklesa", wtedy gdy podane
67 # są tylko rodzaj literacki i autor
68 if 'kind' in self and 'author' in self and len(self) == 2:
69 text = u'%s w twórczości %s' % (unicode(self['kind']),
70 flection.get_case(unicode(self['author']), u'dopełniacz'))
73 # Przypadki ogólniejsze
75 title += u'Motyw %s' % unicode(self['theme'])
79 title += u' w %s' % flection.get_case(unicode(self['genre']), u'miejscownik')
81 title += unicode(self['genre'])
83 if 'kind' in self or 'author' in self or 'epoch' in self:
84 if 'genre' in self or 'theme' in self:
86 title += u' w %s ' % flection.get_case(unicode(self['kind']), u'miejscownik')
88 title += u' w twórczości '
90 title += u'%s ' % unicode(self.get('kind', u'twórczość'))
93 title += flection.get_case(unicode(self['author']), u'dopełniacz')
95 title += flection.get_case(unicode(self['epoch']), u'dopełniacz')
97 return capfirst(title)
101 def user_creation_form():
102 return RegistrationForm(prefix='registration').as_ul()
106 def authentication_form():
107 return LoginForm(prefix='login').as_ul()
110 @register.inclusion_tag('catalogue/breadcrumbs.html')
111 def breadcrumbs(tags, search_form=True):
112 from wolnelektury.catalogue.forms import SearchForm
113 context = {'tag_list': tags}
115 context['search_form'] = SearchForm(tags=tags)
120 def catalogue_url(parser, token):
121 bits = token.split_contents()
128 tags_to_remove.append(bit[1:])
130 tags_to_add.append(bit)
132 return CatalogueURLNode(tags_to_add, tags_to_remove)
135 class CatalogueURLNode(Node):
136 def __init__(self, tags_to_add, tags_to_remove):
137 self.tags_to_add = [Variable(tag) for tag in tags_to_add]
138 self.tags_to_remove = [Variable(tag) for tag in tags_to_remove]
140 def render(self, context):
144 for tag_variable in self.tags_to_add:
145 tag = tag_variable.resolve(context)
146 if isinstance(tag, (list, dict)):
147 tags_to_add += [t for t in tag]
149 tags_to_add.append(tag)
151 for tag_variable in self.tags_to_remove:
152 tag = tag_variable.resolve(context)
154 tags_to_remove += [t for t in tag]
156 tags_to_remove.append(tag)
158 tag_slugs = [tag.slug for tag in tags_to_add]
159 for tag in tags_to_remove:
161 tag_slugs.remove(tag.slug)
165 if len(tag_slugs) > 0:
166 return reverse('tagged_book_list', kwargs={'tags': '/'.join(tag_slugs)})
168 return reverse('main_page')
171 @register.inclusion_tag('catalogue/latest_blog_posts.html')
172 def latest_blog_posts(feed_url, posts_to_show=5):
176 feed = feedparser.parse(feed_url)
178 for i in range(posts_to_show):
179 pub_date = feed['entries'][i].updated_parsed
180 published = datetime.date(pub_date[0], pub_date[1], pub_date[2] )
182 'title': feed['entries'][i].title,
183 'summary': feed['entries'][i].summary,
184 'link': feed['entries'][i].link,
187 return {'posts': posts}