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