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