Fixes #309: breadcrumbs to parent books
[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 tempfile
6 import zipfile
7 import sys
8 import pprint
9 import traceback
10 import re
11 import itertools
12 from operator import itemgetter
13
14 from django.conf import settings
15 from django.template import RequestContext
16 from django.shortcuts import render_to_response, get_object_or_404
17 from django.http import HttpResponse, HttpResponseRedirect, Http404
18 from django.core.urlresolvers import reverse
19 from django.db.models import Q
20 from django.contrib.auth.decorators import login_required, user_passes_test
21 from django.utils.datastructures import SortedDict
22 from django.views.decorators.http import require_POST
23 from django.contrib import auth
24 from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
25 from django.utils import simplejson
26 from django.utils.functional import Promise
27 from django.utils.encoding import force_unicode
28 from django.utils.http import urlquote_plus
29 from django.views.decorators import cache
30 from django.utils.translation import ugettext as _
31 from django.views.generic.list_detail import object_list
32
33 from catalogue import models
34 from catalogue import forms
35 from catalogue.utils import split_tags
36 from newtagging import views as newtagging_views
37
38
39 staff_required = user_passes_test(lambda user: user.is_staff)
40
41
42 class LazyEncoder(simplejson.JSONEncoder):
43     def default(self, obj):
44         if isinstance(obj, Promise):
45             return force_unicode(obj)
46         return obj
47
48 # shortcut for JSON reponses
49 class JSONResponse(HttpResponse):
50     def __init__(self, data={}, callback=None, **kwargs):
51         # get rid of mimetype
52         kwargs.pop('mimetype', None)
53         data = simplejson.dumps(data)
54         if callback:
55             data = callback + "(" + data + ");" 
56         super(JSONResponse, self).__init__(data, mimetype="application/json", **kwargs)
57
58
59 def main_page(request):
60     if request.user.is_authenticated():
61         shelves = models.Tag.objects.filter(category='set', user=request.user)
62         new_set_form = forms.NewSetForm()
63
64     tags = models.Tag.objects.exclude(category__in=('set', 'book'))
65     for tag in tags:
66         tag.count = tag.get_count()
67     categories = split_tags(tags)
68     fragment_tags = categories.get('theme', [])
69
70     form = forms.SearchForm()
71     return render_to_response('catalogue/main_page.html', locals(),
72         context_instance=RequestContext(request))
73
74
75 def book_list(request):
76     books = models.Book.objects.all()
77     form = forms.SearchForm()
78
79     books_by_first_letter = SortedDict()
80     for book in books:
81         books_by_first_letter.setdefault(book.title[0], []).append(book)
82
83     return render_to_response('catalogue/book_list.html', locals(),
84         context_instance=RequestContext(request))
85
86
87 def differentiate_tags(request, tags, ambiguous_slugs):
88     beginning = '/'.join(tag.url_chunk for tag in tags)
89     unparsed = '/'.join(ambiguous_slugs[1:])
90     options = []
91     for tag in models.Tag.objects.exclude(category='book').filter(slug=ambiguous_slugs[0]):
92         options.append({
93             'url_args': '/'.join((beginning, tag.url_chunk, unparsed)).strip('/'),
94             'tags': [tag]
95         })
96     return render_to_response('catalogue/differentiate_tags.html',
97                 {'tags': tags, 'options': options, 'unparsed': ambiguous_slugs[1:]},
98                 context_instance=RequestContext(request))
99
100
101 def tagged_object_list(request, tags=''):
102     try:
103         tags = models.Tag.get_tag_list(tags)
104     except models.Tag.DoesNotExist:
105         raise Http404
106     except models.Tag.MultipleObjectsReturned, e:
107         return differentiate_tags(request, e.tags, e.ambiguous_slugs)
108
109     try:
110         if len(tags) > settings.MAX_TAG_LIST:
111             raise Http404
112     except AttributeError:
113         pass
114
115     if len([tag for tag in tags if tag.category == 'book']):
116         raise Http404
117
118     theme_is_set = [tag for tag in tags if tag.category == 'theme']
119     shelf_is_set = [tag for tag in tags if tag.category == 'set']
120     only_shelf = shelf_is_set and len(tags) == 1
121     only_my_shelf = only_shelf and request.user.is_authenticated() and request.user == tags[0].user
122
123     objects = only_author = pd_counter = None
124     categories = {}
125
126     if theme_is_set:
127         shelf_tags = [tag for tag in tags if tag.category == 'set']
128         fragment_tags = [tag for tag in tags if tag.category != 'set']
129         fragments = models.Fragment.tagged.with_all(fragment_tags)
130
131         if shelf_tags:
132             books = models.Book.tagged.with_all(shelf_tags).order_by()
133             l_tags = models.Tag.objects.filter(category='book', slug__in=[book.book_tag_slug() for book in books])
134             fragments = models.Fragment.tagged.with_any(l_tags, fragments)
135
136         # newtagging goes crazy if we just try:
137         #related_tags = models.Tag.objects.usage_for_queryset(fragments, counts=True,
138         #                    extra={'where': ["catalogue_tag.category != 'book'"]})
139         fragment_keys = [fragment.pk for fragment in fragments]
140         if fragment_keys:
141             related_tags = models.Fragment.tags.usage(counts=True,
142                                 filters={'pk__in': fragment_keys},
143                                 extra={'where': ["catalogue_tag.category != 'book'"]})
144             related_tags = (tag for tag in related_tags if tag not in fragment_tags)
145             categories = split_tags(related_tags)
146
147             objects = fragments
148     else:
149         # get relevant books and their tags
150         objects = models.Book.tagged.with_all(tags).order_by()
151         if not shelf_is_set:
152             # eliminate descendants
153             l_tags = models.Tag.objects.filter(category='book', slug__in=[book.book_tag_slug() for book in objects])
154             descendants_keys = [book.pk for book in models.Book.tagged.with_any(l_tags)]
155             if descendants_keys:
156                 objects = objects.exclude(pk__in=descendants_keys)
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         pd_counter = only_author and tags[0].goes_to_pd()
177         objects = models.Book.objects.none()
178
179     return object_list(
180         request,
181         objects,
182         template_name='catalogue/tagged_object_list.html',
183         extra_context={
184             'categories': categories,
185             'only_shelf': only_shelf,
186             'only_author': only_author,
187             'pd_counter': pd_counter,
188             'only_my_shelf': only_my_shelf,
189             'formats_form': forms.DownloadFormatsForm(),
190
191             'tags': tags,
192         }
193     )
194
195
196 def book_fragments(request, book_slug, theme_slug):
197     book = get_object_or_404(models.Book, slug=book_slug)
198     book_tag = get_object_or_404(models.Tag, slug='l-' + book_slug, category='book')
199     theme = get_object_or_404(models.Tag, slug=theme_slug, category='theme')
200     fragments = models.Fragment.tagged.with_all([book_tag, theme])
201
202     form = forms.SearchForm()
203     return render_to_response('catalogue/book_fragments.html', locals(),
204         context_instance=RequestContext(request))
205
206
207 def book_detail(request, slug):
208     try:
209         book = models.Book.objects.get(slug=slug)
210     except models.Book.DoesNotExist:
211         return book_stub_detail(request, slug)
212
213     book_tag = book.book_tag()
214     tags = list(book.tags.filter(~Q(category='set')))
215     categories = split_tags(tags)
216     book_children = book.children.all().order_by('parent_number')
217     
218     _book = book
219     parents = []
220     while _book.parent:
221         parents.append(_book.parent)
222         _book = _book.parent
223     parents = reversed(parents)
224
225     theme_counter = book.theme_counter
226     book_themes = models.Tag.objects.filter(pk__in=theme_counter.keys())
227     for tag in book_themes:
228         tag.count = theme_counter[tag.pk]
229
230     extra_info = book.get_extra_info_value()
231
232     form = forms.SearchForm()
233     return render_to_response('catalogue/book_detail.html', locals(),
234         context_instance=RequestContext(request))
235
236
237 def book_stub_detail(request, slug):
238     book = get_object_or_404(models.BookStub, slug=slug)
239     pd_counter = book.pd
240     form = forms.SearchForm()
241
242     return render_to_response('catalogue/book_stub_detail.html', locals(),
243         context_instance=RequestContext(request))
244
245
246 def book_text(request, slug):
247     book = get_object_or_404(models.Book, slug=slug)
248     book_themes = {}
249     for fragment in book.fragments.all():
250         for theme in fragment.tags.filter(category='theme'):
251             book_themes.setdefault(theme, []).append(fragment)
252
253     book_themes = book_themes.items()
254     book_themes.sort(key=lambda s: s[0].sort_key)
255     return render_to_response('catalogue/book_text.html', locals(),
256         context_instance=RequestContext(request))
257
258
259 # ==========
260 # = Search =
261 # ==========
262
263 def _no_diacritics_regexp(query):
264     """ returns a regexp for searching for a query without diacritics
265
266     should be locale-aware """
267     names = {
268         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źżŹŻ',
269         u'ą':u'ąĄ', u'ć':u'ćĆ', u'ę':u'ęĘ', u'ł': u'łŁ', u'ń':u'ńŃ', u'ó':u'óÓ', u'ś':u'śŚ', u'ź':u'źŹ', u'ż':u'żŻ'
270         }
271     def repl(m):
272         l = m.group()
273         return u"(%s)" % '|'.join(names[l])
274     return re.sub(u'[%s]' % (u''.join(names.keys())), repl, query)
275
276 def unicode_re_escape(query):
277     """ Unicode-friendly version of re.escape """
278     return re.sub('(?u)(\W)', r'\\\1', query)
279
280 def _word_starts_with(name, prefix):
281     """returns a Q object getting models having `name` contain a word
282     starting with `prefix`
283
284     We define word characters as alphanumeric and underscore, like in JS.
285
286     Works for MySQL, PostgreSQL, Oracle.
287     For SQLite, _sqlite* version is substituted for this.
288     """
289     kwargs = {}
290
291     prefix = _no_diacritics_regexp(unicode_re_escape(prefix))
292     # can't use [[:<:]] (word start),
293     # but we want both `xy` and `(xy` to catch `(xyz)`
294     kwargs['%s__iregex' % name] = u"(^|[^[:alnum:]_])%s" % prefix
295
296     return Q(**kwargs)
297
298
299 def _sqlite_word_starts_with(name, prefix):
300     """ version of _word_starts_with for SQLite
301
302     SQLite in Django uses Python re module
303     """
304     kwargs = {}
305     prefix = _no_diacritics_regexp(unicode_re_escape(prefix))
306     kwargs['%s__iregex' % name] = ur"(^|(?<=[^\wąćęłńóśźżĄĆĘŁŃÓŚŹŻ]))%s" % prefix
307     return Q(**kwargs)
308
309
310 if settings.DATABASE_ENGINE == 'sqlite3':
311     _word_starts_with = _sqlite_word_starts_with
312
313
314 def _tags_starting_with(prefix, user=None):
315     prefix = prefix.lower()
316     book_stubs = models.BookStub.objects.filter(_word_starts_with('title', prefix))
317     books = models.Book.objects.filter(_word_starts_with('title', prefix))
318     book_stubs = filter(lambda x: x not in books, book_stubs)
319     tags = models.Tag.objects.filter(_word_starts_with('name', prefix))
320     if user and user.is_authenticated():
321         tags = tags.filter(~Q(category='book') & (~Q(category='set') | Q(user=user)))
322     else:
323         tags = tags.filter(~Q(category='book') & ~Q(category='set'))
324     return list(books) + list(tags) + list(book_stubs)
325
326
327 def _get_result_link(match, tag_list):
328     if isinstance(match, models.Book) or isinstance(match, models.BookStub):
329         return match.get_absolute_url()
330     else:
331         return reverse('catalogue.views.tagged_object_list',
332             kwargs={'tags': '/'.join(tag.url_chunk for tag in tag_list + [match])}
333         )
334
335 def _get_result_type(match):
336     if isinstance(match, models.Book) or isinstance(match, models.BookStub):
337         type = 'book'
338     else:
339         type = match.category
340     return type
341
342
343
344 def find_best_matches(query, user=None):
345     """ Finds a Book, Tag or Bookstub best matching a query.
346
347     Returns a with:
348       - zero elements when nothing is found,
349       - one element when a best result is found,
350       - more then one element on multiple exact matches
351
352     Raises a ValueError on too short a query.
353     """
354
355     query = query.lower()
356     if len(query) < 2:
357         raise ValueError("query must have at least two characters")
358
359     result = tuple(_tags_starting_with(query, user))
360     exact_matches = tuple(res for res in result if res.name.lower() == query)
361     if exact_matches:
362         return exact_matches
363     else:
364         return result[:1]
365
366
367 def search(request):
368     tags = request.GET.get('tags', '')
369     prefix = request.GET.get('q', '')
370
371     try:
372         tag_list = models.Tag.get_tag_list(tags)
373     except:
374         tag_list = []
375
376     try:
377         result = find_best_matches(prefix, request.user)
378     except ValueError:
379         return render_to_response('catalogue/search_too_short.html', {'tags':tag_list, 'prefix':prefix},
380             context_instance=RequestContext(request))
381
382     if len(result) == 1:
383         return HttpResponseRedirect(_get_result_link(result[0], tag_list))
384     elif len(result) > 1:
385         return render_to_response('catalogue/search_multiple_hits.html',
386             {'tags':tag_list, 'prefix':prefix, 'results':((x, _get_result_link(x, tag_list), _get_result_type(x)) for x in result)},
387             context_instance=RequestContext(request))
388     else:
389         return render_to_response('catalogue/search_no_hits.html', {'tags':tag_list, 'prefix':prefix},
390             context_instance=RequestContext(request))
391
392
393 def tags_starting_with(request):
394     prefix = request.GET.get('q', '')
395     # Prefix must have at least 2 characters
396     if len(prefix) < 2:
397         return HttpResponse('')
398     tags_list = []
399     result = ""   
400     for tag in _tags_starting_with(prefix, request.user):
401         if not tag.name in tags_list:
402             result += "\n" + tag.name
403             tags_list.append(tag.name)
404     return HttpResponse(result)
405
406 def json_tags_starting_with(request, callback=None):
407     # Callback for JSONP
408     prefix = request.GET.get('q', '')
409     callback = request.GET.get('callback', '')
410     # Prefix must have at least 2 characters
411     if len(prefix) < 2:
412         return HttpResponse('')
413     tags_list = []
414     result = ""   
415     for tag in _tags_starting_with(prefix, request.user):
416         if not tag.name in tags_list:
417             result += "\n" + tag.name
418             tags_list.append(tag.name)
419     dict_result = {"matches": tags_list}
420     return JSONResponse(dict_result, callback)
421
422 # ====================
423 # = Shelf management =
424 # ====================
425 @login_required
426 @cache.never_cache
427 def user_shelves(request):
428     shelves = models.Tag.objects.filter(category='set', user=request.user)
429     new_set_form = forms.NewSetForm()
430     return render_to_response('catalogue/user_shelves.html', locals(),
431             context_instance=RequestContext(request))
432
433 @cache.never_cache
434 def book_sets(request, slug):
435     book = get_object_or_404(models.Book, slug=slug)
436     user_sets = models.Tag.objects.filter(category='set', user=request.user)
437     book_sets = book.tags.filter(category='set', user=request.user)
438
439     if not request.user.is_authenticated():
440         return HttpResponse(_('<p>To maintain your shelves you need to be logged in.</p>'))
441
442     if request.method == 'POST':
443         form = forms.ObjectSetsForm(book, request.user, request.POST)
444         if form.is_valid():
445             old_shelves = list(book.tags.filter(category='set'))
446             new_shelves = [models.Tag.objects.get(pk=id) for id in form.cleaned_data['set_ids']]
447
448             for shelf in [shelf for shelf in old_shelves if shelf not in new_shelves]:
449                 shelf.book_count = None
450                 shelf.save()
451
452             for shelf in [shelf for shelf in new_shelves if shelf not in old_shelves]:
453                 shelf.book_count = None
454                 shelf.save()
455
456             book.tags = new_shelves + list(book.tags.filter(~Q(category='set') | ~Q(user=request.user)))
457             if request.is_ajax():
458                 return HttpResponse(_('<p>Shelves were sucessfully saved.</p>'))
459             else:
460                 return HttpResponseRedirect('/')
461     else:
462         form = forms.ObjectSetsForm(book, request.user)
463         new_set_form = forms.NewSetForm()
464
465     return render_to_response('catalogue/book_sets.html', locals(),
466         context_instance=RequestContext(request))
467
468
469 @login_required
470 @require_POST
471 @cache.never_cache
472 def remove_from_shelf(request, shelf, book):
473     book = get_object_or_404(models.Book, slug=book)
474     shelf = get_object_or_404(models.Tag, slug=shelf, category='set', user=request.user)
475
476     if shelf in book.tags:
477         models.Tag.objects.remove_tag(book, shelf)
478
479         shelf.book_count = None
480         shelf.save()
481
482         return HttpResponse(_('Book was successfully removed from the shelf'))
483     else:
484         return HttpResponse(_('This book is not on the shelf'))
485
486
487 def collect_books(books):
488     """
489     Returns all real books in collection.
490     """
491     result = []
492     for book in books:
493         if len(book.children.all()) == 0:
494             result.append(book)
495         else:
496             result += collect_books(book.children.all())
497     return result
498
499
500 @cache.never_cache
501 def download_shelf(request, slug):
502     """"
503     Create a ZIP archive on disk and transmit it in chunks of 8KB,
504     without loading the whole file into memory. A similar approach can
505     be used for large dynamic PDF files.
506     """
507     shelf = get_object_or_404(models.Tag, slug=slug, category='set')
508
509     formats = []
510     form = forms.DownloadFormatsForm(request.GET)
511     if form.is_valid():
512         formats = form.cleaned_data['formats']
513     if len(formats) == 0:
514         formats = ['pdf', 'epub', 'odt', 'txt', 'mp3', 'ogg']
515
516     # Create a ZIP archive
517     temp = tempfile.TemporaryFile()
518     archive = zipfile.ZipFile(temp, 'w')
519
520     for book in collect_books(models.Book.tagged.with_all(shelf)):
521         if 'pdf' in formats and book.pdf_file:
522             filename = book.pdf_file.path
523             archive.write(filename, str('%s.pdf' % book.slug))
524         if 'epub' in formats and book.epub_file:
525             filename = book.epub_file.path
526             archive.write(filename, str('%s.epub' % book.slug))
527         if 'odt' in formats and book.odt_file:
528             filename = book.odt_file.path
529             archive.write(filename, str('%s.odt' % book.slug))
530         if 'txt' in formats and book.txt_file:
531             filename = book.txt_file.path
532             archive.write(filename, str('%s.txt' % book.slug))
533         if 'mp3' in formats and book.mp3_file:
534             filename = book.mp3_file.path
535             archive.write(filename, str('%s.mp3' % book.slug))
536         if 'ogg' in formats and book.ogg_file:
537             filename = book.ogg_file.path
538             archive.write(filename, str('%s.ogg' % book.slug))
539     archive.close()
540
541     response = HttpResponse(content_type='application/zip', mimetype='application/x-zip-compressed')
542     response['Content-Disposition'] = 'attachment; filename=%s.zip' % shelf.sort_key
543     response['Content-Length'] = temp.tell()
544
545     temp.seek(0)
546     response.write(temp.read())
547     return response
548
549
550 @cache.never_cache
551 def shelf_book_formats(request, shelf):
552     """"
553     Returns a list of formats of books in shelf.
554     """
555     shelf = get_object_or_404(models.Tag, slug=shelf, category='set')
556
557     formats = {'pdf': False, 'epub': False, 'odt': False, 'txt': False, 'mp3': False, 'ogg': False}
558
559     for book in collect_books(models.Book.tagged.with_all(shelf)):
560         if book.pdf_file:
561             formats['pdf'] = True
562         if book.epub_file:
563             formats['epub'] = True
564         if book.odt_file:
565             formats['odt'] = True
566         if book.txt_file:
567             formats['txt'] = True
568         if book.mp3_file:
569             formats['mp3'] = True
570         if book.ogg_file:
571             formats['ogg'] = True
572
573     return HttpResponse(LazyEncoder().encode(formats))
574
575
576 @login_required
577 @require_POST
578 @cache.never_cache
579 def new_set(request):
580     new_set_form = forms.NewSetForm(request.POST)
581     if new_set_form.is_valid():
582         new_set = new_set_form.save(request.user)
583
584         if request.is_ajax():
585             return HttpResponse(_('<p>Shelf <strong>%s</strong> was successfully created</p>') % new_set)
586         else:
587             return HttpResponseRedirect('/')
588
589     return HttpResponseRedirect('/')
590
591
592 @login_required
593 @require_POST
594 @cache.never_cache
595 def delete_shelf(request, slug):
596     user_set = get_object_or_404(models.Tag, slug=slug, category='set', user=request.user)
597     user_set.delete()
598
599     if request.is_ajax():
600         return HttpResponse(_('<p>Shelf <strong>%s</strong> was successfully removed</p>') % user_set.name)
601     else:
602         return HttpResponseRedirect('/')
603
604
605 # ==================
606 # = Authentication =
607 # ==================
608 @require_POST
609 @cache.never_cache
610 def login(request):
611     form = AuthenticationForm(data=request.POST, prefix='login')
612     if form.is_valid():
613         auth.login(request, form.get_user())
614         response_data = {'success': True, 'errors': {}}
615     else:
616         response_data = {'success': False, 'errors': form.errors}
617     return HttpResponse(LazyEncoder(ensure_ascii=False).encode(response_data))
618
619
620 @require_POST
621 @cache.never_cache
622 def register(request):
623     registration_form = UserCreationForm(request.POST, prefix='registration')
624     if registration_form.is_valid():
625         user = registration_form.save()
626         user = auth.authenticate(
627             username=registration_form.cleaned_data['username'],
628             password=registration_form.cleaned_data['password1']
629         )
630         auth.login(request, user)
631         response_data = {'success': True, 'errors': {}}
632     else:
633         response_data = {'success': False, 'errors': registration_form.errors}
634     return HttpResponse(LazyEncoder(ensure_ascii=False).encode(response_data))
635
636
637 @cache.never_cache
638 def logout_then_redirect(request):
639     auth.logout(request)
640     return HttpResponseRedirect(urlquote_plus(request.GET.get('next', '/'), safe='/?='))
641
642
643
644 # =========
645 # = Admin =
646 # =========
647 @login_required
648 @staff_required
649 def import_book(request):
650     """docstring for import_book"""
651     book_import_form = forms.BookImportForm(request.POST, request.FILES)
652     if book_import_form.is_valid():
653         try:
654             book_import_form.save()
655         except:
656             info = sys.exc_info()
657             exception = pprint.pformat(info[1])
658             tb = '\n'.join(traceback.format_tb(info[2]))
659             return HttpResponse(_("An error occurred: %(exception)s\n\n%(tb)s") % {'exception':exception, 'tb':tb}, mimetype='text/plain')
660         return HttpResponse(_("Book imported successfully"))
661     else:
662         return HttpResponse(_("Error importing file: %r") % book_import_form.errors)
663
664
665
666 def clock(request):
667     """ Provides server time for jquery.countdown,
668     in a format suitable for Date.parse()
669     """
670     from datetime import datetime
671     return HttpResponse(datetime.now().strftime('%Y/%m/%d %H:%M:%S'))