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