1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
8 from django import template
9 from django.template import Node, Variable
10 from django.utils.encoding import smart_str
11 from django.core.urlresolvers import reverse
12 from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
13 from django.db.models import Q
14 from django.conf import settings
17 register = template.Library()
20 class RegistrationForm(UserCreationForm):
22 "Returns this form rendered as HTML <li>s -- excluding the <ul></ul>."
23 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)
26 class LoginForm(AuthenticationForm):
28 "Returns this form rendered as HTML <li>s -- excluding the <ul></ul>."
29 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)
42 return '%s%s' % (text[0].upper(), text[1:])
48 def simple_title(tags):
60 title.append("%s: %s" % (mapping[tag.category], tag.name))
61 return capfirst(', '.join(title))
65 def book_title(book, html_links=False):
66 names = list(book.tags.filter(category='author'))
72 names.extend(reversed(books))
75 names = ['<a href="%s">%s</a>' % (tag.get_absolute_url(), tag.name) for tag in names]
77 names = [tag.name for tag in names]
79 return ', '.join(names)
83 def book_title_html(book):
84 return book_title(book, html_links=True)
88 def title_from_tags(tags):
92 result[tag.category] = tag
95 # TODO: Remove this after adding flection mechanism
96 return simple_title(tags)
98 class Flection(object):
99 def get_case(self, name, flection):
101 flection = Flection()
103 self = split_tags(tags)
107 # Specjalny przypadek oglądania wszystkich lektur na danej półce
108 if len(self) == 1 and 'set' in self:
109 return u'Półka %s' % self['set']
111 # Specjalny przypadek "Twórczość w pozytywizmie", wtedy gdy tylko epoka
112 # jest wybrana przez użytkownika
113 if 'epoch' in self and len(self) == 1:
114 text = u'Twórczość w %s' % flection.get_case(unicode(self['epoch']), u'miejscownik')
115 return capfirst(text)
117 # Specjalny przypadek "Dramat w twórczości Sofoklesa", wtedy gdy podane
118 # są tylko rodzaj literacki i autor
119 if 'kind' in self and 'author' in self and len(self) == 2:
120 text = u'%s w twórczości %s' % (unicode(self['kind']),
121 flection.get_case(unicode(self['author']), u'dopełniacz'))
122 return capfirst(text)
124 # Przypadki ogólniejsze
126 title += u'Motyw %s' % unicode(self['theme'])
130 title += u' w %s' % flection.get_case(unicode(self['genre']), u'miejscownik')
132 title += unicode(self['genre'])
134 if 'kind' in self or 'author' in self or 'epoch' in self:
135 if 'genre' in self or 'theme' in self:
137 title += u' w %s ' % flection.get_case(unicode(self['kind']), u'miejscownik')
139 title += u' w twórczości '
141 title += u'%s ' % unicode(self.get('kind', u'twórczość'))
144 title += flection.get_case(unicode(self['author']), u'dopełniacz')
145 elif 'epoch' in self:
146 title += flection.get_case(unicode(self['epoch']), u'dopełniacz')
148 return capfirst(title)
152 def user_creation_form():
153 return RegistrationForm(prefix='registration').as_ul()
157 def authentication_form():
158 return LoginForm(prefix='login').as_ul()
161 @register.inclusion_tag('catalogue/breadcrumbs.html')
162 def breadcrumbs(tags, search_form=True):
163 from catalogue.forms import SearchForm
164 context = {'tag_list': tags}
166 max_tag_list = settings.MAX_TAG_LIST
167 except AttributeError:
169 if search_form and (max_tag_list == -1 or len(tags) < max_tag_list):
170 context['search_form'] = SearchForm(tags=tags)
175 def catalogue_url(parser, token):
176 bits = token.split_contents()
183 tags_to_remove.append(bit[1:])
185 tags_to_add.append(bit)
187 return CatalogueURLNode(tags_to_add, tags_to_remove)
190 class CatalogueURLNode(Node):
191 def __init__(self, tags_to_add, tags_to_remove):
192 self.tags_to_add = [Variable(tag) for tag in tags_to_add]
193 self.tags_to_remove = [Variable(tag) for tag in tags_to_remove]
195 def render(self, context):
199 for tag_variable in self.tags_to_add:
200 tag = tag_variable.resolve(context)
201 if isinstance(tag, (list, dict)):
202 tags_to_add += [t for t in tag]
204 tags_to_add.append(tag)
206 for tag_variable in self.tags_to_remove:
207 tag = tag_variable.resolve(context)
209 tags_to_remove += [t for t in tag]
211 tags_to_remove.append(tag)
213 tag_slugs = [tag.url_chunk for tag in tags_to_add]
214 for tag in tags_to_remove:
216 tag_slugs.remove(tag.url_chunk)
220 if len(tag_slugs) > 0:
221 return reverse('tagged_object_list', kwargs={'tags': '/'.join(tag_slugs)})
223 return reverse('main_page')
226 @register.inclusion_tag('catalogue/latest_blog_posts.html')
227 def latest_blog_posts(feed_url, posts_to_show=5):
229 feed = feedparser.parse(str(feed_url))
231 for i in range(posts_to_show):
232 pub_date = feed['entries'][i].updated_parsed
233 published = datetime.date(pub_date[0], pub_date[1], pub_date[2] )
235 'title': feed['entries'][i].title,
236 'summary': feed['entries'][i].summary,
237 'link': feed['entries'][i].link,
240 return {'posts': posts}
245 @register.inclusion_tag('catalogue/tag_list.html')
246 def tag_list(tags, choices=None):
254 @register.inclusion_tag('catalogue/folded_tag_list.html')
255 def folded_tag_list(tags, choices=None):
256 tags = [tag for tag in tags if tag.count]
259 some_tags_hidden = False
260 tag_count = len(tags)
265 shown_tags = [tag for tag in tags if tag.main_page]
266 if tag_count > len(shown_tags):
267 some_tags_hidden = True