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