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 simple_title(tags):
53 title.append("%s: %s" % (mapping[tag.category], tag.name))
54 return capfirst(', '.join(title))
58 def title_from_tags(tags):
62 result[tag.category] = tag
65 # TODO: Remove this after adding flection mechanism
66 return simple_title(tags)
68 class Flection(object):
69 def get_case(self, name, flection):
73 self = split_tags(tags)
77 # Specjalny przypadek oglądania wszystkich lektur na danej półce
78 if len(self) == 1 and 'set' in self:
79 return u'Półka %s' % self['set']
81 # Specjalny przypadek "Twórczość w pozytywizmie", wtedy gdy tylko epoka
82 # jest wybrana przez użytkownika
83 if 'epoch' in self and len(self) == 1:
84 text = u'Twórczość w %s' % flection.get_case(unicode(self['epoch']), u'miejscownik')
87 # Specjalny przypadek "Dramat w twórczości Sofoklesa", wtedy gdy podane
88 # są tylko rodzaj literacki i autor
89 if 'kind' in self and 'author' in self and len(self) == 2:
90 text = u'%s w twórczości %s' % (unicode(self['kind']),
91 flection.get_case(unicode(self['author']), u'dopełniacz'))
94 # Przypadki ogólniejsze
96 title += u'Motyw %s' % unicode(self['theme'])
100 title += u' w %s' % flection.get_case(unicode(self['genre']), u'miejscownik')
102 title += unicode(self['genre'])
104 if 'kind' in self or 'author' in self or 'epoch' in self:
105 if 'genre' in self or 'theme' in self:
107 title += u' w %s ' % flection.get_case(unicode(self['kind']), u'miejscownik')
109 title += u' w twórczości '
111 title += u'%s ' % unicode(self.get('kind', u'twórczość'))
114 title += flection.get_case(unicode(self['author']), u'dopełniacz')
115 elif 'epoch' in self:
116 title += flection.get_case(unicode(self['epoch']), u'dopełniacz')
118 return capfirst(title)
122 def user_creation_form():
123 return RegistrationForm(prefix='registration').as_ul()
127 def authentication_form():
128 return LoginForm(prefix='login').as_ul()
131 @register.inclusion_tag('catalogue/breadcrumbs.html')
132 def breadcrumbs(tags, search_form=True):
133 from catalogue.forms import SearchForm
134 context = {'tag_list': tags}
135 if search_form and len(tags) < 6:
136 context['search_form'] = SearchForm(tags=tags)
141 def catalogue_url(parser, token):
142 bits = token.split_contents()
149 tags_to_remove.append(bit[1:])
151 tags_to_add.append(bit)
153 return CatalogueURLNode(tags_to_add, tags_to_remove)
156 class CatalogueURLNode(Node):
157 def __init__(self, tags_to_add, tags_to_remove):
158 self.tags_to_add = [Variable(tag) for tag in tags_to_add]
159 self.tags_to_remove = [Variable(tag) for tag in tags_to_remove]
161 def render(self, context):
165 for tag_variable in self.tags_to_add:
166 tag = tag_variable.resolve(context)
167 if isinstance(tag, (list, dict)):
168 tags_to_add += [t for t in tag]
170 tags_to_add.append(tag)
172 for tag_variable in self.tags_to_remove:
173 tag = tag_variable.resolve(context)
175 tags_to_remove += [t for t in tag]
177 tags_to_remove.append(tag)
179 tag_slugs = [tag.slug for tag in tags_to_add]
180 for tag in tags_to_remove:
182 tag_slugs.remove(tag.slug)
186 if len(tag_slugs) > 0:
187 return reverse('tagged_object_list', kwargs={'tags': '/'.join(tag_slugs)})
189 return reverse('main_page')
192 @register.inclusion_tag('catalogue/latest_blog_posts.html')
193 def latest_blog_posts(feed_url, posts_to_show=5):
197 feed = feedparser.parse(feed_url)
199 for i in range(posts_to_show):
200 pub_date = feed['entries'][i].updated_parsed
201 published = datetime.date(pub_date[0], pub_date[1], pub_date[2] )
203 'title': feed['entries'][i].title,
204 'summary': feed['entries'][i].summary,
205 'link': feed['entries'][i].link,
208 return {'posts': posts}
211 @register.inclusion_tag('catalogue/tag_list.html')
212 def tag_list(tags, choices=None):
220 @register.inclusion_tag('catalogue/folded_tag_list.html')
221 def folded_tag_list(tags, choices=None):
224 some_tags_hidden = False
225 tag_count = len(tags)
230 shown_tags = [tag for tag in tags if tag.main_page]
231 if tag_count > len(shown_tags):
232 some_tags_hidden = True