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