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