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