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