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