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 na danej półce
58 if len(self) == 1 and 'set' in self:
59 return u'Półka %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)
121 def catalogue_url(parser, token):
122 bits = token.split_contents()
129 tags_to_remove.append(bit[1:])
131 tags_to_add.append(bit)
133 return CatalogueURLNode(tags_to_add, tags_to_remove)
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]
141 def render(self, context):
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]
150 tags_to_add.append(tag)
152 for tag_variable in self.tags_to_remove:
153 tag = tag_variable.resolve(context)
155 tags_to_remove += [t for t in tag]
157 tags_to_remove.append(tag)
159 tag_slugs = [tag.slug for tag in tags_to_add]
160 for tag in tags_to_remove:
162 tag_slugs.remove(tag.slug)
166 if len(tag_slugs) > 0:
167 return reverse('tagged_object_list', kwargs={'tags': '/'.join(tag_slugs)})
169 return reverse('main_page')
172 @register.inclusion_tag('catalogue/latest_blog_posts.html')
173 def latest_blog_posts(feed_url, posts_to_show=5):
177 feed = feedparser.parse(feed_url)
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] )
183 'title': feed['entries'][i].title,
184 'summary': feed['entries'][i].summary,
185 'link': feed['entries'][i].link,
188 return {'posts': posts}