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