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.
5 from random import randint
6 from urlparse import urlparse
8 from django.conf import settings
9 from django import template
10 from django.template import Node, Variable, Template, Context
11 from django.core.urlresolvers import reverse
12 from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
13 from django.utils.cache import add_never_cache_headers
14 from django.utils.translation import ugettext as _
16 from ssify import ssi_variable
17 from catalogue.models import Book, BookMedia, Fragment, Tag, Source
18 from catalogue.constants import LICENSES
20 register = template.Library()
23 class RegistrationForm(UserCreationForm):
25 "Returns this form rendered as HTML <li>s -- excluding the <ul></ul>."
26 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)
29 class LoginForm(AuthenticationForm):
31 "Returns this form rendered as HTML <li>s -- excluding the <ul></ul>."
32 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)
45 return '%s%s' % (text[0].upper(), text[1:])
51 def html_title_from_tags(tags):
53 return title_from_tags(tags)
54 template = Template("{{ category }}: <a href='{{ tag.get_absolute_url }}'>{{ tag.name }}</a>")
55 return capfirst(",<br/>".join(
56 template.render(Context({'tag': tag, 'category': _(tag.category)})) for tag in tags))
59 def simple_title(tags):
62 title.append("%s: %s" % (_(tag.category), tag.name))
63 return capfirst(', '.join(title))
67 def book_title(book, html_links=False):
68 return book.pretty_title(html_links)
72 def book_title_html(book):
73 return book_title(book, html_links=True)
77 def title_from_tags(tags):
81 result[tag.category] = tag
84 # TODO: Remove this after adding flection mechanism
85 return simple_title(tags)
87 class Flection(object):
88 def get_case(self, name, flection):
92 self = split_tags(tags)
96 # Specjalny przypadek oglądania wszystkich lektur na danej półce
97 if len(self) == 1 and 'set' in self:
98 return u'Półka %s' % self['set']
100 # Specjalny przypadek "Twórczość w pozytywizmie", wtedy gdy tylko epoka
101 # jest wybrana przez użytkownika
102 if 'epoch' in self and len(self) == 1:
103 text = u'Twórczość w %s' % flection.get_case(unicode(self['epoch']), u'miejscownik')
104 return capfirst(text)
106 # Specjalny przypadek "Dramat w twórczości Sofoklesa", wtedy gdy podane
107 # są tylko rodzaj literacki i autor
108 if 'kind' in self and 'author' in self and len(self) == 2:
109 text = u'%s w twórczości %s' % (unicode(self['kind']),
110 flection.get_case(unicode(self['author']), u'dopełniacz'))
111 return capfirst(text)
113 # Przypadki ogólniejsze
115 title += u'Motyw %s' % unicode(self['theme'])
119 title += u' w %s' % flection.get_case(unicode(self['genre']), u'miejscownik')
121 title += unicode(self['genre'])
123 if 'kind' in self or 'author' in self or 'epoch' in self:
124 if 'genre' in self or 'theme' in self:
126 title += u' w %s ' % flection.get_case(unicode(self['kind']), u'miejscownik')
128 title += u' w twórczości '
130 title += u'%s ' % unicode(self.get('kind', u'twórczość'))
133 title += flection.get_case(unicode(self['author']), u'dopełniacz')
134 elif 'epoch' in self:
135 title += flection.get_case(unicode(self['epoch']), u'dopełniacz')
137 return capfirst(title)
141 def book_tree(book_list, books_by_parent):
142 text = "".join("<li><a href='%s'>%s</a>%s</li>" % (
143 book.get_absolute_url(), book.title, book_tree(books_by_parent.get(book, ()), books_by_parent)
144 ) for book in book_list)
147 return "<ol>%s</ol>" % text
152 def audiobook_tree(book_list, books_by_parent):
153 text = "".join("<li><a class='open-player' href='%s'>%s</a>%s</li>" % (
154 reverse("book_player", args=[book.slug]), book.title, audiobook_tree(books_by_parent.get(book, ()), books_by_parent)
155 ) for book in book_list)
158 return "<ol>%s</ol>" % text
163 def book_tree_texml(book_list, books_by_parent, depth=1):
165 <cmd name='hspace'><parm>%(depth)dem</parm></cmd>%(title)s
166 <spec cat='align' /><cmd name="note"><parm>%(audiences)s</parm></cmd>
167 <spec cat='align' /><cmd name="note"><parm>%(audiobook)s</parm></cmd>
173 "audiences": ", ".join(book.audiences_pl()),
174 "audiobook": "audiobook" if book.has_media('mp3') else "",
175 "children": book_tree_texml(books_by_parent.get(book.id, ()), books_by_parent, depth + 1)
176 } for book in book_list)
180 def book_tree_csv(author, book_list, books_by_parent, depth=1, max_depth=3, delimeter="\t"):
181 def quote_if_necessary(s):
184 s.replace('"', '\\"')
189 return "".join("""%(author)s%(d)s%(preindent)s%(title)s%(d)s%(postindent)s%(audiences)s%(d)s%(audiobook)s
192 "preindent": delimeter * (depth - 1),
193 "postindent": delimeter * (max_depth - depth),
195 "author": quote_if_necessary(author.name),
196 "title": quote_if_necessary(book.title),
197 "audiences": ", ".join(book.audiences_pl()),
198 "audiobook": "audiobook" if book.has_media('mp3') else "",
199 "children": book_tree_csv(author, books_by_parent.get(book.id, ()), books_by_parent, depth + 1)
200 } for book in book_list)
203 def all_editors(extra_info):
205 if 'editors' in extra_info:
206 editors += extra_info['editors']
207 if 'technical_editors' in extra_info:
208 editors += extra_info['technical_editors']
209 # support for extra_info-s from librarian<1.2
210 if 'editor' in extra_info:
211 editors.append(extra_info['editor'])
212 if 'technical_editor' in extra_info:
213 editors.append(extra_info['technical_editor'])
215 ' '.join(p.strip() for p in person.rsplit(',', 1)[::-1])
216 for person in sorted(set(editors)))
220 def user_creation_form():
221 return RegistrationForm(prefix='registration').as_ul()
225 def authentication_form():
226 return LoginForm(prefix='login').as_ul()
230 def catalogue_url(parser, token):
231 bits = token.split_contents()
237 tags_to_remove.append(bit[1:])
239 tags_to_add.append(bit)
241 return CatalogueURLNode(tags_to_add, tags_to_remove)
244 class CatalogueURLNode(Node):
245 def __init__(self, tags_to_add, tags_to_remove):
246 self.tags_to_add = [Variable(tag) for tag in tags_to_add]
247 self.tags_to_remove = [Variable(tag) for tag in tags_to_remove]
249 def render(self, context):
253 for tag_variable in self.tags_to_add:
254 tag = tag_variable.resolve(context)
255 if isinstance(tag, (list, dict)):
256 tags_to_add += [t for t in tag]
258 tags_to_add.append(tag)
260 for tag_variable in self.tags_to_remove:
261 tag = tag_variable.resolve(context)
263 tags_to_remove += [t for t in tag]
265 tags_to_remove.append(tag)
267 tag_slugs = [tag.url_chunk for tag in tags_to_add]
268 for tag in tags_to_remove:
270 tag_slugs.remove(tag.url_chunk)
274 if len(tag_slugs) > 0:
275 return reverse('tagged_object_list', kwargs={'tags': '/'.join(tag_slugs)})
277 return reverse('main_page')
280 @register.inclusion_tag('catalogue/tag_list.html')
281 def tag_list(tags, choices=None):
284 if len(tags) == 1 and tags[0].category not in [t.category for t in choices]:
289 @register.inclusion_tag('catalogue/inline_tag_list.html')
290 def inline_tag_list(tags, choices=None):
291 return tag_list(tags, choices)
294 @register.inclusion_tag('catalogue/collection_list.html')
295 def collection_list(collections):
299 @register.inclusion_tag('catalogue/book_info.html')
304 @register.inclusion_tag('catalogue/work-list.html', takes_context=True)
305 def work_list(context, object_list):
306 request = context.get('request')
310 @register.inclusion_tag('catalogue/related_books.html', takes_context=True)
311 def related_books(context, book, limit=6, random=1, taken=0):
312 limit = limit - taken
313 related = Book.tagged.related_to(book,
314 Book.objects.exclude(common_slug=book.common_slug)
315 ).exclude(ancestor=book)[:limit-random]
316 random_excluded = [b.pk for b in related] + [book.pk]
318 'request': context['request'],
321 'random_excluded': random_excluded,
325 @register.inclusion_tag('catalogue/menu.html')
326 def catalogue_menu():
327 return {'categories': [
328 ('author', _('Authors'), 'autorzy'),
329 ('genre', _('Genres'), 'gatunki'),
330 ('kind', _('Kinds'), 'rodzaje'),
331 ('epoch', _('Epochs'), 'epoki'),
332 ('theme', _('Themes'), 'motywy'),
337 def download_audio(book, daisy=True):
339 if book.has_media('mp3'):
340 links.append("<a href='%s'>%s</a>" %
341 (reverse('download_zip_mp3', args=[book.slug]),
342 BookMedia.formats['mp3'].name))
343 if book.has_media('ogg'):
344 links.append("<a href='%s'>%s</a>" %
345 (reverse('download_zip_ogg', args=[book.slug]),
346 BookMedia.formats['ogg'].name))
347 if daisy and book.has_media('daisy'):
348 for dsy in book.get_media('daisy'):
349 links.append("<a href='%s'>%s</a>" %
350 (dsy.file.url, BookMedia.formats['daisy'].name))
351 return ", ".join(links)
354 @register.inclusion_tag("catalogue/snippets/custom_pdf_link_li.html")
355 def custom_pdf_link_li(book):
358 'NO_CUSTOM_PDF': settings.NO_CUSTOM_PDF,
362 @register.inclusion_tag("catalogue/snippets/license_icon.html")
363 def license_icon(license_url):
364 """Creates a license icon, if the license_url is known."""
365 known = LICENSES.get(license_url)
369 "license_url": license_url,
370 "icon": "img/licenses/%s.png" % known['icon'],
371 "license_description": known['description'],
377 return obj.__class__.__name__
381 def source_name(url):
383 netloc = urlparse(url).netloc
385 netloc = urlparse('http://' + url).netloc
388 source, created = Source.objects.get_or_create(netloc=netloc)
389 return source.name or netloc
392 @ssi_variable(register, patch_response=[add_never_cache_headers])
393 def catalogue_random_book(request, exclude_ids):
394 queryset = Book.objects.exclude(pk__in=exclude_ids)
395 count = queryset.count()
397 return queryset[randint(0, count - 1)].pk
402 @ssi_variable(register, patch_response=[add_never_cache_headers])
403 def choose_fragment(request, book_id=None, tag_ids=None, unless=False):
407 if book_id is not None:
408 fragment = Book.objects.get(pk=book_id).choose_fragment()
410 if tag_ids is not None:
411 tags = Tag.objects.filter(pk__in=tag_ids)
412 fragments = Fragment.tagged.with_all(tags).order_by().only('id')
414 fragments = Fragment.objects.all().order_by().only('id')
415 fragment_count = fragments.count()
416 fragment = fragments[randint(0, fragment_count - 1)] if fragment_count else None
417 return fragment.pk if fragment is not None else None