cover colors
[wolnelektury.git] / apps / catalogue / views.py
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.
4 #
5 import re
6 import itertools
7
8 from django.conf import settings
9 from django.template import RequestContext
10 from django.shortcuts import render_to_response, get_object_or_404
11 from django.http import HttpResponse, HttpResponseRedirect, Http404, HttpResponsePermanentRedirect
12 from django.core.urlresolvers import reverse
13 from django.db.models import Q
14 from django.contrib.auth.decorators import login_required, user_passes_test
15 from django.utils.datastructures import SortedDict
16 from django.utils.http import urlquote_plus
17 from django.utils import translation
18 from django.utils.translation import ugettext as _
19 from django.views.decorators.cache import never_cache
20
21 from ajaxable.utils import JSONResponse, AjaxableFormView
22
23 from catalogue import models
24 from catalogue import forms
25 from catalogue.utils import (split_tags, AttachmentHttpResponse,
26     async_build_pdf, MultiQuerySet)
27 from pdcounter import models as pdcounter_models
28 from pdcounter import views as pdcounter_views
29 from suggest.forms import PublishingSuggestForm
30 from picture.models import Picture
31
32 from os import path
33
34 staff_required = user_passes_test(lambda user: user.is_staff)
35
36
37 def catalogue(request):
38     tags = models.Tag.objects.exclude(
39         category__in=('set', 'book')).exclude(book_count=0)
40     tags = list(tags)
41     for tag in tags:
42         tag.count = tag.book_count
43     categories = split_tags(tags)
44     fragment_tags = categories.get('theme', [])
45
46     return render_to_response('catalogue/catalogue.html', locals(),
47         context_instance=RequestContext(request))
48
49
50 def book_list(request, filter=None, template_name='catalogue/book_list.html',
51         context=None):
52     """ generates a listing of all books, optionally filtered with a test function """
53
54     books_by_author, orphans, books_by_parent = models.Book.book_list(filter)
55     books_nav = SortedDict()
56     for tag in books_by_author:
57         if books_by_author[tag]:
58             books_nav.setdefault(tag.sort_key[0], []).append(tag)
59
60     return render_to_response(template_name, locals(),
61         context_instance=RequestContext(request))
62
63
64 def audiobook_list(request):
65     return book_list(request, Q(media__type='mp3') | Q(media__type='ogg'),
66                      template_name='catalogue/audiobook_list.html')
67
68
69 def daisy_list(request):
70     return book_list(request, Q(media__type='daisy'),
71                      template_name='catalogue/daisy_list.html')
72
73
74 def collection(request, slug):
75     coll = get_object_or_404(models.Collection, slug=slug)
76     slugs = coll.book_slugs.split()
77     # allow URIs
78     slugs = [slug.rstrip('/').rsplit('/', 1)[-1] if '/' in slug else slug
79                 for slug in slugs]
80     return book_list(request, Q(slug__in=slugs),
81                      template_name='catalogue/collection.html',
82                      context={'collection': coll})
83
84
85 def differentiate_tags(request, tags, ambiguous_slugs):
86     beginning = '/'.join(tag.url_chunk for tag in tags)
87     unparsed = '/'.join(ambiguous_slugs[1:])
88     options = []
89     for tag in models.Tag.objects.exclude(category='book').filter(slug=ambiguous_slugs[0]):
90         options.append({
91             'url_args': '/'.join((beginning, tag.url_chunk, unparsed)).strip('/'),
92             'tags': [tag]
93         })
94     return render_to_response('catalogue/differentiate_tags.html',
95                 {'tags': tags, 'options': options, 'unparsed': ambiguous_slugs[1:]},
96                 context_instance=RequestContext(request))
97
98
99 @never_cache
100 def tagged_object_list(request, tags=''):
101     try:
102         tags = models.Tag.get_tag_list(tags)
103     except models.Tag.DoesNotExist:
104         chunks = tags.split('/')
105         if len(chunks) == 2 and chunks[0] == 'autor':
106             return pdcounter_views.author_detail(request, chunks[1])
107         else:
108             raise Http404
109     except models.Tag.MultipleObjectsReturned, e:
110         return differentiate_tags(request, e.tags, e.ambiguous_slugs)
111     except models.Tag.UrlDeprecationWarning, e:
112         return HttpResponsePermanentRedirect(reverse('tagged_object_list', args=['/'.join(tag.url_chunk for tag in e.tags)]))
113
114     try:
115         if len(tags) > settings.MAX_TAG_LIST:
116             raise Http404
117     except AttributeError:
118         pass
119
120     if len([tag for tag in tags if tag.category == 'book']):
121         raise Http404
122
123     theme_is_set = [tag for tag in tags if tag.category == 'theme']
124     shelf_is_set = [tag for tag in tags if tag.category == 'set']
125     only_shelf = shelf_is_set and len(tags) == 1
126     only_my_shelf = only_shelf and request.user.is_authenticated() and request.user == tags[0].user
127
128     objects = only_author = None
129     categories = {}
130
131     if theme_is_set:
132         shelf_tags = [tag for tag in tags if tag.category == 'set']
133         fragment_tags = [tag for tag in tags if tag.category != 'set']
134         fragments = models.Fragment.tagged.with_all(fragment_tags)
135
136         if shelf_tags:
137             books = models.Book.tagged.with_all(shelf_tags).order_by()
138             l_tags = models.Tag.objects.filter(category='book', slug__in=[book.book_tag_slug() for book in books])
139             fragments = models.Fragment.tagged.with_any(l_tags, fragments)
140
141         # newtagging goes crazy if we just try:
142         #related_tags = models.Tag.objects.usage_for_queryset(fragments, counts=True,
143         #                    extra={'where': ["catalogue_tag.category != 'book'"]})
144         fragment_keys = [fragment.pk for fragment in fragments]
145         if fragment_keys:
146             related_tags = models.Fragment.tags.usage(counts=True,
147                                 filters={'pk__in': fragment_keys},
148                                 extra={'where': ["catalogue_tag.category != 'book'"]})
149             related_tags = (tag for tag in related_tags if tag not in fragment_tags)
150             categories = split_tags(related_tags)
151
152             objects = fragments
153     else:
154         if shelf_is_set:
155             objects = models.Book.tagged.with_all(tags)
156         else:
157             objects = models.Book.tagged_top_level(tags)
158
159         # get related tags from `tag_counter` and `theme_counter`
160         related_counts = {}
161         tags_pks = [tag.pk for tag in tags]
162         for book in objects:
163             for tag_pk, value in itertools.chain(book.tag_counter.iteritems(), book.theme_counter.iteritems()):
164                 if tag_pk in tags_pks:
165                     continue
166                 related_counts[tag_pk] = related_counts.get(tag_pk, 0) + value
167         related_tags = models.Tag.objects.filter(pk__in=related_counts.keys())
168         related_tags = [tag for tag in related_tags if tag not in tags]
169         for tag in related_tags:
170             tag.count = related_counts[tag.pk]
171
172         categories = split_tags(related_tags)
173         del related_tags
174
175     if not objects:
176         only_author = len(tags) == 1 and tags[0].category == 'author'
177         objects = models.Book.objects.none()
178
179     # Add pictures
180     objects = MultiQuerySet(Picture.tagged.with_all(tags), objects)
181
182     return render_to_response('catalogue/tagged_object_list.html',
183         {
184             'object_list': objects,
185             'categories': categories,
186             'only_shelf': only_shelf,
187             'only_author': only_author,
188             'only_my_shelf': only_my_shelf,
189             'formats_form': forms.DownloadFormatsForm(),
190             'tags': tags,
191             'theme_is_set': theme_is_set,
192         },
193         context_instance=RequestContext(request))
194
195
196 def book_fragments(request, slug, theme_slug):
197     book = get_object_or_404(models.Book, slug=slug)
198
199     book_tag = book.book_tag()
200     theme = get_object_or_404(models.Tag, slug=theme_slug, category='theme')
201     fragments = models.Fragment.tagged.with_all([book_tag, theme])
202
203     return render_to_response('catalogue/book_fragments.html', locals(),
204         context_instance=RequestContext(request))
205
206
207 @never_cache
208 def book_detail(request, slug):
209     try:
210         book = models.Book.objects.get(slug=slug)
211     except models.Book.DoesNotExist:
212         return pdcounter_views.book_stub_detail(request, slug)
213
214     book_children = book.children.all().order_by('parent_number', 'sort_key')
215     return render_to_response('catalogue/book_detail.html', locals(),
216         context_instance=RequestContext(request))
217
218
219 def player(request, slug):
220     book = get_object_or_404(models.Book, slug=slug)
221     if not book.has_media('mp3'):
222         raise Http404
223
224     ogg_files = {}
225     for m in book.media.filter(type='ogg').order_by():
226         ogg_files[m.name] = m
227
228     audiobooks = []
229     have_oggs = True
230     projects = set()
231     for mp3 in book.media.filter(type='mp3'):
232         # ogg files are always from the same project
233         meta = mp3.get_extra_info_value()
234         project = meta.get('project')
235         if not project:
236             # temporary fallback
237             project = u'CzytamySłuchając'
238
239         projects.add((project, meta.get('funded_by', '')))
240
241         media = {'mp3': mp3}
242
243         ogg = ogg_files.get(mp3.name)
244         if ogg:
245             media['ogg'] = ogg
246         else:
247             have_oggs = False
248         audiobooks.append(media)
249
250     projects = sorted(projects)
251
252     extra_info = book.get_extra_info_value()
253
254     return render_to_response('catalogue/player.html', locals(),
255         context_instance=RequestContext(request))
256
257
258 def book_text(request, slug):
259     book = get_object_or_404(models.Book, slug=slug)
260
261     if not book.has_html_file():
262         raise Http404
263     book_themes = {}
264     for fragment in book.fragments.all():
265         for theme in fragment.tags.filter(category='theme'):
266             book_themes.setdefault(theme, []).append(fragment)
267
268     book_themes = book_themes.items()
269     book_themes.sort(key=lambda s: s[0].sort_key)
270     return render_to_response('catalogue/book_text.html', locals(),
271         context_instance=RequestContext(request))
272
273
274 # ==========
275 # = Search =
276 # ==========
277
278 def _no_diacritics_regexp(query):
279     """ returns a regexp for searching for a query without diacritics
280
281     should be locale-aware """
282     names = {
283         u'a':u'aąĄ', u'c':u'cćĆ', u'e':u'eęĘ', u'l': u'lłŁ', u'n':u'nńŃ', u'o':u'oóÓ', u's':u'sśŚ', u'z':u'zźżŹŻ',
284         u'ą':u'ąĄ', u'ć':u'ćĆ', u'ę':u'ęĘ', u'ł': u'łŁ', u'ń':u'ńŃ', u'ó':u'óÓ', u'ś':u'śŚ', u'ź':u'źŹ', u'ż':u'żŻ'
285         }
286     def repl(m):
287         l = m.group()
288         return u"(%s)" % '|'.join(names[l])
289     return re.sub(u'[%s]' % (u''.join(names.keys())), repl, query)
290
291 def unicode_re_escape(query):
292     """ Unicode-friendly version of re.escape """
293     return re.sub('(?u)(\W)', r'\\\1', query)
294
295 def _word_starts_with(name, prefix):
296     """returns a Q object getting models having `name` contain a word
297     starting with `prefix`
298
299     We define word characters as alphanumeric and underscore, like in JS.
300
301     Works for MySQL, PostgreSQL, Oracle.
302     For SQLite, _sqlite* version is substituted for this.
303     """
304     kwargs = {}
305
306     prefix = _no_diacritics_regexp(unicode_re_escape(prefix))
307     # can't use [[:<:]] (word start),
308     # but we want both `xy` and `(xy` to catch `(xyz)`
309     kwargs['%s__iregex' % name] = u"(^|[^[:alnum:]_])%s" % prefix
310
311     return Q(**kwargs)
312
313
314 def _word_starts_with_regexp(prefix):
315     prefix = _no_diacritics_regexp(unicode_re_escape(prefix))
316     return ur"(^|(?<=[^\wąćęłńóśźżĄĆĘŁŃÓŚŹŻ]))%s" % prefix
317
318
319 def _sqlite_word_starts_with(name, prefix):
320     """ version of _word_starts_with for SQLite
321
322     SQLite in Django uses Python re module
323     """
324     kwargs = {}
325     kwargs['%s__iregex' % name] = _word_starts_with_regexp(prefix)
326     return Q(**kwargs)
327
328
329 if hasattr(settings, 'DATABASES'):
330     if settings.DATABASES['default']['ENGINE'] == 'django.db.backends.sqlite3':
331         _word_starts_with = _sqlite_word_starts_with
332 elif settings.DATABASE_ENGINE == 'sqlite3':
333     _word_starts_with = _sqlite_word_starts_with
334
335
336 class App():
337     def __init__(self, name, view):
338         self.name = name
339         self._view = view
340         self.lower = name.lower()
341         self.category = 'application'
342     def view(self):
343         return reverse(*self._view)
344
345 _apps = (
346     App(u'Leśmianator', (u'lesmianator', )),
347     )
348
349
350 def _tags_starting_with(prefix, user=None):
351     prefix = prefix.lower()
352     # PD counter
353     book_stubs = pdcounter_models.BookStub.objects.filter(_word_starts_with('title', prefix))
354     authors = pdcounter_models.Author.objects.filter(_word_starts_with('name', prefix))
355
356     books = models.Book.objects.filter(_word_starts_with('title', prefix))
357     tags = models.Tag.objects.filter(_word_starts_with('name', prefix))
358     if user and user.is_authenticated():
359         tags = tags.filter(~Q(category='book') & (~Q(category='set') | Q(user=user)))
360     else:
361         tags = tags.filter(~Q(category='book') & ~Q(category='set'))
362
363     prefix_regexp = re.compile(_word_starts_with_regexp(prefix))
364     return list(books) + list(tags) + [app for app in _apps if prefix_regexp.search(app.lower)] + list(book_stubs) + list(authors)
365
366
367 def _get_result_link(match, tag_list):
368     if isinstance(match, models.Tag):
369         return reverse('catalogue.views.tagged_object_list',
370             kwargs={'tags': '/'.join(tag.url_chunk for tag in tag_list + [match])}
371         )
372     elif isinstance(match, App):
373         return match.view()
374     else:
375         return match.get_absolute_url()
376
377
378 def _get_result_type(match):
379     if isinstance(match, models.Book) or isinstance(match, pdcounter_models.BookStub):
380         type = 'book'
381     else:
382         type = match.category
383     return type
384
385
386 def books_starting_with(prefix):
387     prefix = prefix.lower()
388     return models.Book.objects.filter(_word_starts_with('title', prefix))
389
390
391 def find_best_matches(query, user=None):
392     """ Finds a models.Book, Tag, models.BookStub or Author best matching a query.
393
394     Returns a with:
395       - zero elements when nothing is found,
396       - one element when a best result is found,
397       - more then one element on multiple exact matches
398
399     Raises a ValueError on too short a query.
400     """
401
402     query = query.lower()
403     if len(query) < 2:
404         raise ValueError("query must have at least two characters")
405
406     result = tuple(_tags_starting_with(query, user))
407     # remove pdcounter stuff
408     book_titles = set(match.pretty_title().lower() for match in result
409                       if isinstance(match, models.Book))
410     authors = set(match.name.lower() for match in result
411                   if isinstance(match, models.Tag) and match.category=='author')
412     result = tuple(res for res in result if not (
413                  (isinstance(res, pdcounter_models.BookStub) and res.pretty_title().lower() in book_titles)
414                  or (isinstance(res, pdcounter_models.Author) and res.name.lower() in authors)
415              ))
416
417     exact_matches = tuple(res for res in result if res.name.lower() == query)
418     if exact_matches:
419         return exact_matches
420     else:
421         return tuple(result)[:1]
422
423
424 def search(request):
425     tags = request.GET.get('tags', '')
426     prefix = request.GET.get('q', '')
427
428     try:
429         tag_list = models.Tag.get_tag_list(tags)
430     except:
431         tag_list = []
432
433     try:
434         result = find_best_matches(prefix, request.user)
435     except ValueError:
436         return render_to_response('catalogue/search_too_short.html', {'tags':tag_list, 'prefix':prefix},
437             context_instance=RequestContext(request))
438
439     if len(result) == 1:
440         return HttpResponseRedirect(_get_result_link(result[0], tag_list))
441     elif len(result) > 1:
442         return render_to_response('catalogue/search_multiple_hits.html',
443             {'tags':tag_list, 'prefix':prefix, 'results':((x, _get_result_link(x, tag_list), _get_result_type(x)) for x in result)},
444             context_instance=RequestContext(request))
445     else:
446         form = PublishingSuggestForm(initial={"books": prefix + ", "})
447         return render_to_response('catalogue/search_no_hits.html',
448             {'tags':tag_list, 'prefix':prefix, "pubsuggest_form": form},
449             context_instance=RequestContext(request))
450
451
452 def tags_starting_with(request):
453     prefix = request.GET.get('q', '')
454     # Prefix must have at least 2 characters
455     if len(prefix) < 2:
456         return HttpResponse('')
457     tags_list = []
458     result = ""
459     for tag in _tags_starting_with(prefix, request.user):
460         if not tag.name in tags_list:
461             result += "\n" + tag.name
462             tags_list.append(tag.name)
463     return HttpResponse(result)
464
465 def json_tags_starting_with(request, callback=None):
466     # Callback for JSONP
467     prefix = request.GET.get('q', '')
468     callback = request.GET.get('callback', '')
469     # Prefix must have at least 2 characters
470     if len(prefix) < 2:
471         return HttpResponse('')
472     tags_list = []
473     for tag in _tags_starting_with(prefix, request.user):
474         if not tag.name in tags_list:
475             tags_list.append(tag.name)
476     if request.GET.get('mozhint', ''):
477         result = [prefix, tags_list]
478     else:
479         result = {"matches": tags_list}
480     return JSONResponse(result, callback)
481
482
483 # =========
484 # = Admin =
485 # =========
486 @login_required
487 @staff_required
488 def import_book(request):
489     """docstring for import_book"""
490     book_import_form = forms.BookImportForm(request.POST, request.FILES)
491     if book_import_form.is_valid():
492         try:
493             book_import_form.save()
494         except:
495             import sys
496             import pprint
497             import traceback
498             info = sys.exc_info()
499             exception = pprint.pformat(info[1])
500             tb = '\n'.join(traceback.format_tb(info[2]))
501             return HttpResponse(_("An error occurred: %(exception)s\n\n%(tb)s") % {'exception':exception, 'tb':tb}, mimetype='text/plain')
502         return HttpResponse(_("Book imported successfully"))
503     else:
504         return HttpResponse(_("Error importing file: %r") % book_import_form.errors)
505
506
507 # info views for API
508
509 def book_info(request, id, lang='pl'):
510     book = get_object_or_404(models.Book, id=id)
511     # set language by hand
512     translation.activate(lang)
513     return render_to_response('catalogue/book_info.html', locals(),
514         context_instance=RequestContext(request))
515
516
517 def tag_info(request, id):
518     tag = get_object_or_404(models.Tag, id=id)
519     return HttpResponse(tag.description)
520
521
522 def download_zip(request, format, slug=None):
523     url = None
524     if format in models.Book.ebook_formats:
525         url = models.Book.zip_format(format)
526     elif format in ('mp3', 'ogg') and slug is not None:
527         book = get_object_or_404(models.Book, slug=slug)
528         url = book.zip_audiobooks(format)
529     else:
530         raise Http404('No format specified for zip package')
531     return HttpResponseRedirect(urlquote_plus(settings.MEDIA_URL + url, safe='/?='))
532
533
534 def download_custom_pdf(request, slug, method='GET'):
535     book = get_object_or_404(models.Book, slug=slug)
536
537     if request.method == method:
538         form = forms.CustomPDFForm(method == 'GET' and request.GET or request.POST)
539         if form.is_valid():
540             cust = form.customizations
541             pdf_file = models.get_customized_pdf_path(book, cust)
542
543             if not path.exists(pdf_file):
544                 result = async_build_pdf.delay(book.id, cust, pdf_file)
545                 result.wait()
546             return AttachmentHttpResponse(file_name=("%s.pdf" % book.slug), file_path=pdf_file, mimetype="application/pdf")
547         else:
548             raise Http404(_('Incorrect customization options for PDF'))
549     else:
550         raise Http404(_('Bad method'))
551
552
553 class CustomPDFFormView(AjaxableFormView):
554     form_class = forms.CustomPDFForm
555     title = _('Download custom PDF')
556     submit = _('Download')
557
558     def __call__(self, request):
559         from copy import copy
560         if request.method == 'POST':
561             request.GET = copy(request.GET)
562             request.GET['next'] = "%s?%s" % (reverse('catalogue.views.download_custom_pdf', args=[request.GET.get('slug')]),
563                                              request.POST.urlencode())
564         return super(CustomPDFFormView, self).__call__(request)
565
566     def get_object(self, request):
567         return get_object_or_404(models.Book, slug=request.GET.get('slug'))
568
569     def context_description(self, request, obj):
570         return obj.pretty_title()
571
572     def success(self, *args):
573         pass