1 # -*- coding: utf-8 -*-
5 from django import template
6 from django.template import Node, Variable
7 from django.utils.encoding import smart_str
8 from django.core.urlresolvers import reverse
9 from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
10 from django.db.models import Q
13 register = template.Library()
16 class RegistrationForm(UserCreationForm):
18 "Returns this form rendered as HTML <li>s -- excluding the <ul></ul>."
19 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)
22 class LoginForm(AuthenticationForm):
24 "Returns this form rendered as HTML <li>s -- excluding the <ul></ul>."
25 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)
38 return '%s%s' % (text[0].upper(), text[1:])
44 def simple_title(tags):
56 title.append("%s: %s" % (mapping[tag.category], tag.name))
57 return capfirst(', '.join(title))
61 def title_from_tags(tags):
65 result[tag.category] = tag
68 # TODO: Remove this after adding flection mechanism
69 return simple_title(tags)
71 class Flection(object):
72 def get_case(self, name, flection):
76 self = split_tags(tags)
80 # Specjalny przypadek oglądania wszystkich lektur na danej półce
81 if len(self) == 1 and 'set' in self:
82 return u'Półka %s' % self['set']
84 # Specjalny przypadek "Twórczość w pozytywizmie", wtedy gdy tylko epoka
85 # jest wybrana przez użytkownika
86 if 'epoch' in self and len(self) == 1:
87 text = u'Twórczość w %s' % flection.get_case(unicode(self['epoch']), u'miejscownik')
90 # Specjalny przypadek "Dramat w twórczości Sofoklesa", wtedy gdy podane
91 # są tylko rodzaj literacki i autor
92 if 'kind' in self and 'author' in self and len(self) == 2:
93 text = u'%s w twórczości %s' % (unicode(self['kind']),
94 flection.get_case(unicode(self['author']), u'dopełniacz'))
97 # Przypadki ogólniejsze
99 title += u'Motyw %s' % unicode(self['theme'])
103 title += u' w %s' % flection.get_case(unicode(self['genre']), u'miejscownik')
105 title += unicode(self['genre'])
107 if 'kind' in self or 'author' in self or 'epoch' in self:
108 if 'genre' in self or 'theme' in self:
110 title += u' w %s ' % flection.get_case(unicode(self['kind']), u'miejscownik')
112 title += u' w twórczości '
114 title += u'%s ' % unicode(self.get('kind', u'twórczość'))
117 title += flection.get_case(unicode(self['author']), u'dopełniacz')
118 elif 'epoch' in self:
119 title += flection.get_case(unicode(self['epoch']), u'dopełniacz')
121 return capfirst(title)
125 def user_creation_form():
126 return RegistrationForm(prefix='registration').as_ul()
130 def authentication_form():
131 return LoginForm(prefix='login').as_ul()
134 @register.inclusion_tag('catalogue/breadcrumbs.html')
135 def breadcrumbs(tags, search_form=True):
136 from catalogue.forms import SearchForm
137 context = {'tag_list': tags}
138 if search_form and len(tags) < 6:
139 context['search_form'] = SearchForm(tags=tags)
144 def catalogue_url(parser, token):
145 bits = token.split_contents()
152 tags_to_remove.append(bit[1:])
154 tags_to_add.append(bit)
156 return CatalogueURLNode(tags_to_add, tags_to_remove)
159 class CatalogueURLNode(Node):
160 def __init__(self, tags_to_add, tags_to_remove):
161 self.tags_to_add = [Variable(tag) for tag in tags_to_add]
162 self.tags_to_remove = [Variable(tag) for tag in tags_to_remove]
164 def render(self, context):
168 for tag_variable in self.tags_to_add:
169 tag = tag_variable.resolve(context)
170 if isinstance(tag, (list, dict)):
171 tags_to_add += [t for t in tag]
173 tags_to_add.append(tag)
175 for tag_variable in self.tags_to_remove:
176 tag = tag_variable.resolve(context)
178 tags_to_remove += [t for t in tag]
180 tags_to_remove.append(tag)
182 tag_slugs = [tag.slug for tag in tags_to_add]
183 for tag in tags_to_remove:
185 tag_slugs.remove(tag.slug)
189 if len(tag_slugs) > 0:
190 return reverse('tagged_object_list', kwargs={'tags': '/'.join(tag_slugs)})
192 return reverse('main_page')
195 @register.inclusion_tag('catalogue/latest_blog_posts.html')
196 def latest_blog_posts(feed_url, posts_to_show=5):
198 feed = feedparser.parse(str(feed_url))
200 for i in range(posts_to_show):
201 pub_date = feed['entries'][i].updated_parsed
202 published = datetime.date(pub_date[0], pub_date[1], pub_date[2] )
204 'title': feed['entries'][i].title,
205 'summary': feed['entries'][i].summary,
206 'link': feed['entries'][i].link,
209 return {'posts': posts}
214 @register.inclusion_tag('catalogue/tag_list.html')
215 def tag_list(tags, choices=None):
223 @register.inclusion_tag('catalogue/folded_tag_list.html')
224 def folded_tag_list(tags, choices=None):
227 some_tags_hidden = False
228 tag_count = len(tags)
233 shown_tags = [tag for tag in tags if tag.main_page]
234 if tag_count > len(shown_tags):
235 some_tags_hidden = True