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.
13 from datetime import datetime
15 from django.conf import settings
16 from django.template import RequestContext
17 from django.shortcuts import render_to_response, get_object_or_404
18 from django.http import HttpResponse, HttpResponseRedirect, Http404
19 from django.core.urlresolvers import reverse
20 from django.db.models import Q
21 from django.contrib.auth.decorators import login_required, user_passes_test
22 from django.utils.datastructures import SortedDict
23 from django.views.decorators.http import require_POST
24 from django.contrib import auth
25 from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
26 from django.utils import simplejson
27 from django.utils.functional import Promise
28 from django.utils.encoding import force_unicode
29 from django.utils.http import urlquote_plus
30 from django.views.decorators import cache
31 from django.utils.translation import ugettext as _
32 from django.views.generic.list_detail import object_list
34 from catalogue import models
35 from catalogue import forms
36 from catalogue.utils import split_tags
37 from newtagging import views as newtagging_views
38 from pdcounter import models as pdcounter_models
39 from pdcounter import views as pdcounter_views
40 from slughifi import slughifi
43 staff_required = user_passes_test(lambda user: user.is_staff)
46 class LazyEncoder(simplejson.JSONEncoder):
47 def default(self, obj):
48 if isinstance(obj, Promise):
49 return force_unicode(obj)
52 # shortcut for JSON reponses
53 class JSONResponse(HttpResponse):
54 def __init__(self, data={}, callback=None, **kwargs):
56 kwargs.pop('mimetype', None)
57 data = simplejson.dumps(data)
59 data = callback + "(" + data + ");"
60 super(JSONResponse, self).__init__(data, mimetype="application/json", **kwargs)
63 def main_page(request):
64 if request.user.is_authenticated():
65 shelves = models.Tag.objects.filter(category='set', user=request.user)
66 new_set_form = forms.NewSetForm()
68 tags = models.Tag.objects.exclude(category__in=('set', 'book'))
70 tag.count = tag.get_count()
71 categories = split_tags(tags)
72 fragment_tags = categories.get('theme', [])
74 form = forms.SearchForm()
75 return render_to_response('catalogue/main_page.html', locals(),
76 context_instance=RequestContext(request))
79 def book_list(request, filter=None, template_name='catalogue/book_list.html'):
80 """ generates a listing of all books, optionally filtered with a test function """
82 form = forms.SearchForm()
85 books = models.Book.objects.all().order_by('parent_number', 'title').only('title', 'parent', 'slug')
87 books = books.filter(filter).distinct()
88 book_ids = set((book.pk for book in books))
90 parent = book.parent_id
91 if parent not in book_ids:
93 books_by_parent.setdefault(parent, []).append(book)
96 books_by_parent.setdefault(book.parent_id, []).append(book)
99 books_by_author = SortedDict()
100 books_nav = SortedDict()
101 for tag in models.Tag.objects.filter(category='author'):
102 books_by_author[tag] = []
104 for book in books_by_parent.get(None,()):
105 authors = list(book.tags.filter(category='author'))
107 for author in authors:
108 books_by_author[author].append(book)
112 for tag in books_by_author:
113 if books_by_author[tag]:
114 books_nav.setdefault(tag.sort_key[0], []).append(tag)
116 return render_to_response(template_name, locals(),
117 context_instance=RequestContext(request))
120 def audiobook_list(request):
121 return book_list(request, Q(medias__type='mp3') | Q(medias__type='ogg'),
122 template_name='catalogue/audiobook_list.html')
125 def daisy_list(request):
126 return book_list(request, Q(medias__type='daisy'),
127 template_name='catalogue/daisy_list.html')
130 def differentiate_tags(request, tags, ambiguous_slugs):
131 beginning = '/'.join(tag.url_chunk for tag in tags)
132 unparsed = '/'.join(ambiguous_slugs[1:])
134 for tag in models.Tag.objects.exclude(category='book').filter(slug=ambiguous_slugs[0]):
136 'url_args': '/'.join((beginning, tag.url_chunk, unparsed)).strip('/'),
139 return render_to_response('catalogue/differentiate_tags.html',
140 {'tags': tags, 'options': options, 'unparsed': ambiguous_slugs[1:]},
141 context_instance=RequestContext(request))
144 def tagged_object_list(request, tags=''):
146 tags = models.Tag.get_tag_list(tags)
147 except models.Tag.DoesNotExist:
148 chunks = tags.split('/')
149 if len(chunks) == 2 and chunks[0] == 'autor':
150 return pdcounter_views.author_detail(request, chunks[1])
153 except models.Tag.MultipleObjectsReturned, e:
154 return differentiate_tags(request, e.tags, e.ambiguous_slugs)
157 if len(tags) > settings.MAX_TAG_LIST:
159 except AttributeError:
162 if len([tag for tag in tags if tag.category == 'book']):
165 theme_is_set = [tag for tag in tags if tag.category == 'theme']
166 shelf_is_set = [tag for tag in tags if tag.category == 'set']
167 only_shelf = shelf_is_set and len(tags) == 1
168 only_my_shelf = only_shelf and request.user.is_authenticated() and request.user == tags[0].user
170 objects = only_author = None
174 shelf_tags = [tag for tag in tags if tag.category == 'set']
175 fragment_tags = [tag for tag in tags if tag.category != 'set']
176 fragments = models.Fragment.tagged.with_all(fragment_tags)
179 books = models.Book.tagged.with_all(shelf_tags).order_by()
180 l_tags = models.Tag.objects.filter(category='book', slug__in=[book.book_tag_slug() for book in books])
181 fragments = models.Fragment.tagged.with_any(l_tags, fragments)
183 # newtagging goes crazy if we just try:
184 #related_tags = models.Tag.objects.usage_for_queryset(fragments, counts=True,
185 # extra={'where': ["catalogue_tag.category != 'book'"]})
186 fragment_keys = [fragment.pk for fragment in fragments]
188 related_tags = models.Fragment.tags.usage(counts=True,
189 filters={'pk__in': fragment_keys},
190 extra={'where': ["catalogue_tag.category != 'book'"]})
191 related_tags = (tag for tag in related_tags if tag not in fragment_tags)
192 categories = split_tags(related_tags)
196 # get relevant books and their tags
197 objects = models.Book.tagged.with_all(tags)
199 # eliminate descendants
200 l_tags = models.Tag.objects.filter(category='book', slug__in=[book.book_tag_slug() for book in objects])
201 descendants_keys = [book.pk for book in models.Book.tagged.with_any(l_tags)]
203 objects = objects.exclude(pk__in=descendants_keys)
205 # get related tags from `tag_counter` and `theme_counter`
207 tags_pks = [tag.pk for tag in tags]
209 for tag_pk, value in itertools.chain(book.tag_counter.iteritems(), book.theme_counter.iteritems()):
210 if tag_pk in tags_pks:
212 related_counts[tag_pk] = related_counts.get(tag_pk, 0) + value
213 related_tags = models.Tag.objects.filter(pk__in=related_counts.keys())
214 related_tags = [tag for tag in related_tags if tag not in tags]
215 for tag in related_tags:
216 tag.count = related_counts[tag.pk]
218 categories = split_tags(related_tags)
222 only_author = len(tags) == 1 and tags[0].category == 'author'
223 objects = models.Book.objects.none()
228 template_name='catalogue/tagged_object_list.html',
230 'categories': categories,
231 'only_shelf': only_shelf,
232 'only_author': only_author,
233 'only_my_shelf': only_my_shelf,
234 'formats_form': forms.DownloadFormatsForm(),
241 def book_fragments(request, book_slug, theme_slug):
242 book = get_object_or_404(models.Book, slug=book_slug)
243 book_tag = get_object_or_404(models.Tag, slug='l-' + book_slug, category='book')
244 theme = get_object_or_404(models.Tag, slug=theme_slug, category='theme')
245 fragments = models.Fragment.tagged.with_all([book_tag, theme])
247 form = forms.SearchForm()
248 return render_to_response('catalogue/book_fragments.html', locals(),
249 context_instance=RequestContext(request))
252 def book_detail(request, slug):
254 book = models.Book.objects.get(slug=slug)
255 except models.Book.DoesNotExist:
256 return pdcounter_views.book_stub_detail(request, slug)
258 book_tag = book.book_tag()
259 tags = list(book.tags.filter(~Q(category='set')))
260 categories = split_tags(tags)
261 book_children = book.children.all().order_by('parent_number', 'title')
266 parents.append(_book.parent)
268 parents = reversed(parents)
270 theme_counter = book.theme_counter
271 book_themes = models.Tag.objects.filter(pk__in=theme_counter.keys())
272 for tag in book_themes:
273 tag.count = theme_counter[tag.pk]
275 extra_info = book.get_extra_info_value()
277 form = forms.SearchForm()
278 return render_to_response('catalogue/book_detail.html', locals(),
279 context_instance=RequestContext(request))
282 def book_text(request, slug):
283 book = get_object_or_404(models.Book, slug=slug)
284 if not book.has_html_file():
287 for fragment in book.fragments.all():
288 for theme in fragment.tags.filter(category='theme'):
289 book_themes.setdefault(theme, []).append(fragment)
291 book_themes = book_themes.items()
292 book_themes.sort(key=lambda s: s[0].sort_key)
293 return render_to_response('catalogue/book_text.html', locals(),
294 context_instance=RequestContext(request))
301 def _no_diacritics_regexp(query):
302 """ returns a regexp for searching for a query without diacritics
304 should be locale-aware """
306 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źżŹŻ',
307 u'ą':u'ąĄ', u'ć':u'ćĆ', u'ę':u'ęĘ', u'ł': u'łŁ', u'ń':u'ńŃ', u'ó':u'óÓ', u'ś':u'śŚ', u'ź':u'źŹ', u'ż':u'żŻ'
311 return u"(%s)" % '|'.join(names[l])
312 return re.sub(u'[%s]' % (u''.join(names.keys())), repl, query)
314 def unicode_re_escape(query):
315 """ Unicode-friendly version of re.escape """
316 return re.sub('(?u)(\W)', r'\\\1', query)
318 def _word_starts_with(name, prefix):
319 """returns a Q object getting models having `name` contain a word
320 starting with `prefix`
322 We define word characters as alphanumeric and underscore, like in JS.
324 Works for MySQL, PostgreSQL, Oracle.
325 For SQLite, _sqlite* version is substituted for this.
329 prefix = _no_diacritics_regexp(unicode_re_escape(prefix))
330 # can't use [[:<:]] (word start),
331 # but we want both `xy` and `(xy` to catch `(xyz)`
332 kwargs['%s__iregex' % name] = u"(^|[^[:alnum:]_])%s" % prefix
337 def _word_starts_with_regexp(prefix):
338 prefix = _no_diacritics_regexp(unicode_re_escape(prefix))
339 return ur"(^|(?<=[^\wąćęłńóśźżĄĆĘŁŃÓŚŹŻ]))%s" % prefix
342 def _sqlite_word_starts_with(name, prefix):
343 """ version of _word_starts_with for SQLite
345 SQLite in Django uses Python re module
348 kwargs['%s__iregex' % name] = _word_starts_with_regexp(prefix)
352 if hasattr(settings, 'DATABASES'):
353 if settings.DATABASES['default']['ENGINE'] == 'django.db.backends.sqlite3':
354 _word_starts_with = _sqlite_word_starts_with
355 elif settings.DATABASE_ENGINE == 'sqlite3':
356 _word_starts_with = _sqlite_word_starts_with
360 def __init__(self, name, view):
363 self.lower = name.lower()
364 self.category = 'application'
366 return reverse(*self._view)
369 App(u'Leśmianator', (u'lesmianator', )),
373 def _tags_starting_with(prefix, user=None):
374 prefix = prefix.lower()
376 book_stubs = pdcounter_models.BookStub.objects.filter(_word_starts_with('title', prefix))
377 authors = pdcounter_models.Author.objects.filter(_word_starts_with('name', prefix))
379 books = models.Book.objects.filter(_word_starts_with('title', prefix))
380 tags = models.Tag.objects.filter(_word_starts_with('name', prefix))
381 if user and user.is_authenticated():
382 tags = tags.filter(~Q(category='book') & (~Q(category='set') | Q(user=user)))
384 tags = tags.filter(~Q(category='book') & ~Q(category='set'))
386 prefix_regexp = re.compile(_word_starts_with_regexp(prefix))
387 return list(books) + list(tags) + [app for app in _apps if prefix_regexp.search(app.lower)] + list(book_stubs) + list(authors)
390 def _get_result_link(match, tag_list):
391 if isinstance(match, models.Tag):
392 return reverse('catalogue.views.tagged_object_list',
393 kwargs={'tags': '/'.join(tag.url_chunk for tag in tag_list + [match])}
395 elif isinstance(match, App):
398 return match.get_absolute_url()
401 def _get_result_type(match):
402 if isinstance(match, models.Book) or isinstance(match, pdcounter_models.BookStub):
405 type = match.category
409 def books_starting_with(prefix):
410 prefix = prefix.lower()
411 return models.Book.objects.filter(_word_starts_with('title', prefix))
414 def find_best_matches(query, user=None):
415 """ Finds a Book, Tag, BookStub or Author best matching a query.
418 - zero elements when nothing is found,
419 - one element when a best result is found,
420 - more then one element on multiple exact matches
422 Raises a ValueError on too short a query.
425 query = query.lower()
427 raise ValueError("query must have at least two characters")
429 result = tuple(_tags_starting_with(query, user))
430 # remove pdcounter stuff
431 book_titles = set(match.pretty_title().lower() for match in result
432 if isinstance(match, models.Book))
433 authors = set(match.name.lower() for match in result
434 if isinstance(match, models.Tag) and match.category=='author')
435 result = tuple(res for res in result if not (
436 (isinstance(res, pdcounter_models.BookStub) and res.pretty_title().lower() in book_titles)
437 or (isinstance(res, pdcounter_models.Author) and res.name.lower() in authors)
440 exact_matches = tuple(res for res in result if res.name.lower() == query)
444 return tuple(result)[:1]
448 tags = request.GET.get('tags', '')
449 prefix = request.GET.get('q', '')
452 tag_list = models.Tag.get_tag_list(tags)
457 result = find_best_matches(prefix, request.user)
459 return render_to_response('catalogue/search_too_short.html', {'tags':tag_list, 'prefix':prefix},
460 context_instance=RequestContext(request))
463 return HttpResponseRedirect(_get_result_link(result[0], tag_list))
464 elif len(result) > 1:
465 return render_to_response('catalogue/search_multiple_hits.html',
466 {'tags':tag_list, 'prefix':prefix, 'results':((x, _get_result_link(x, tag_list), _get_result_type(x)) for x in result)},
467 context_instance=RequestContext(request))
469 return render_to_response('catalogue/search_no_hits.html', {'tags':tag_list, 'prefix':prefix},
470 context_instance=RequestContext(request))
473 def tags_starting_with(request):
474 prefix = request.GET.get('q', '')
475 # Prefix must have at least 2 characters
477 return HttpResponse('')
480 for tag in _tags_starting_with(prefix, request.user):
481 if not tag.name in tags_list:
482 result += "\n" + tag.name
483 tags_list.append(tag.name)
484 return HttpResponse(result)
486 def json_tags_starting_with(request, callback=None):
488 prefix = request.GET.get('q', '')
489 callback = request.GET.get('callback', '')
490 # Prefix must have at least 2 characters
492 return HttpResponse('')
494 for tag in _tags_starting_with(prefix, request.user):
495 if not tag.name in tags_list:
496 tags_list.append(tag.name)
497 if request.GET.get('mozhint', ''):
498 result = [prefix, tags_list]
500 result = {"matches": tags_list}
501 return JSONResponse(result, callback)
503 # ====================
504 # = Shelf management =
505 # ====================
508 def user_shelves(request):
509 shelves = models.Tag.objects.filter(category='set', user=request.user)
510 new_set_form = forms.NewSetForm()
511 return render_to_response('catalogue/user_shelves.html', locals(),
512 context_instance=RequestContext(request))
515 def book_sets(request, slug):
516 if not request.user.is_authenticated():
517 return HttpResponse(_('<p>To maintain your shelves you need to be logged in.</p>'))
519 book = get_object_or_404(models.Book, slug=slug)
520 user_sets = models.Tag.objects.filter(category='set', user=request.user)
521 book_sets = book.tags.filter(category='set', user=request.user)
523 if request.method == 'POST':
524 form = forms.ObjectSetsForm(book, request.user, request.POST)
526 old_shelves = list(book.tags.filter(category='set'))
527 new_shelves = [models.Tag.objects.get(pk=id) for id in form.cleaned_data['set_ids']]
529 for shelf in [shelf for shelf in old_shelves if shelf not in new_shelves]:
530 shelf.book_count = None
533 for shelf in [shelf for shelf in new_shelves if shelf not in old_shelves]:
534 shelf.book_count = None
537 book.tags = new_shelves + list(book.tags.filter(~Q(category='set') | ~Q(user=request.user)))
538 if request.is_ajax():
539 return JSONResponse('{"msg":"'+_("<p>Shelves were sucessfully saved.</p>")+'", "after":"close"}')
541 return HttpResponseRedirect('/')
543 form = forms.ObjectSetsForm(book, request.user)
544 new_set_form = forms.NewSetForm()
546 return render_to_response('catalogue/book_sets.html', locals(),
547 context_instance=RequestContext(request))
553 def remove_from_shelf(request, shelf, book):
554 book = get_object_or_404(models.Book, slug=book)
555 shelf = get_object_or_404(models.Tag, slug=shelf, category='set', user=request.user)
557 if shelf in book.tags:
558 models.Tag.objects.remove_tag(book, shelf)
560 shelf.book_count = None
563 return HttpResponse(_('Book was successfully removed from the shelf'))
565 return HttpResponse(_('This book is not on the shelf'))
568 def collect_books(books):
570 Returns all real books in collection.
574 if len(book.children.all()) == 0:
577 result += collect_books(book.children.all())
582 def download_shelf(request, slug):
584 Create a ZIP archive on disk and transmit it in chunks of 8KB,
585 without loading the whole file into memory. A similar approach can
586 be used for large dynamic PDF files.
588 shelf = get_object_or_404(models.Tag, slug=slug, category='set')
591 form = forms.DownloadFormatsForm(request.GET)
593 formats = form.cleaned_data['formats']
594 if len(formats) == 0:
595 formats = ['pdf', 'epub', 'odt', 'txt']
597 # Create a ZIP archive
598 temp = tempfile.TemporaryFile()
599 archive = zipfile.ZipFile(temp, 'w')
602 for book in collect_books(models.Book.tagged.with_all(shelf)):
603 if 'pdf' in formats and book.pdf_file:
604 filename = book.pdf_file.path
605 archive.write(filename, str('%s.pdf' % book.slug))
606 if book.root_ancestor not in already and 'epub' in formats and book.root_ancestor.epub_file:
607 filename = book.root_ancestor.epub_file.path
608 archive.write(filename, str('%s.epub' % book.root_ancestor.slug))
609 already.add(book.root_ancestor)
610 if 'odt' in formats and book.has_media("odt"):
611 for file in book.get_media("odt"):
612 filename = file.file.path
613 archive.write(filename, str('%s.odt' % slughifi(file.name)))
614 if 'txt' in formats and book.txt_file:
615 filename = book.txt_file.path
616 archive.write(filename, str('%s.txt' % book.slug))
619 response = HttpResponse(content_type='application/zip', mimetype='application/x-zip-compressed')
620 response['Content-Disposition'] = 'attachment; filename=%s.zip' % slughifi(shelf.name)
621 response['Content-Length'] = temp.tell()
624 response.write(temp.read())
629 def shelf_book_formats(request, shelf):
631 Returns a list of formats of books in shelf.
633 shelf = get_object_or_404(models.Tag, slug=shelf, category='set')
635 formats = {'pdf': False, 'epub': False, 'odt': False, 'txt': False}
637 for book in collect_books(models.Book.tagged.with_all(shelf)):
639 formats['pdf'] = True
640 if book.root_ancestor.epub_file:
641 formats['epub'] = True
643 formats['txt'] = True
644 for format in ('odt',):
645 if book.has_media(format):
646 formats[format] = True
648 return HttpResponse(LazyEncoder().encode(formats))
654 def new_set(request):
655 new_set_form = forms.NewSetForm(request.POST)
656 if new_set_form.is_valid():
657 new_set = new_set_form.save(request.user)
659 if request.is_ajax():
660 return JSONResponse('{"id":"%d", "name":"%s", "msg":"<p>Shelf <strong>%s</strong> was successfully created</p>"}' % (new_set.id, new_set.name, new_set))
662 return HttpResponseRedirect('/')
664 return HttpResponseRedirect('/')
670 def delete_shelf(request, slug):
671 user_set = get_object_or_404(models.Tag, slug=slug, category='set', user=request.user)
674 if request.is_ajax():
675 return HttpResponse(_('<p>Shelf <strong>%s</strong> was successfully removed</p>') % user_set.name)
677 return HttpResponseRedirect('/')
686 form = AuthenticationForm(data=request.POST, prefix='login')
688 auth.login(request, form.get_user())
689 response_data = {'success': True, 'errors': {}}
691 response_data = {'success': False, 'errors': form.errors}
692 return HttpResponse(LazyEncoder(ensure_ascii=False).encode(response_data))
697 def register(request):
698 registration_form = UserCreationForm(request.POST, prefix='registration')
699 if registration_form.is_valid():
700 user = registration_form.save()
701 user = auth.authenticate(
702 username=registration_form.cleaned_data['username'],
703 password=registration_form.cleaned_data['password1']
705 auth.login(request, user)
706 response_data = {'success': True, 'errors': {}}
708 response_data = {'success': False, 'errors': registration_form.errors}
709 return HttpResponse(LazyEncoder(ensure_ascii=False).encode(response_data))
713 def logout_then_redirect(request):
715 return HttpResponseRedirect(urlquote_plus(request.GET.get('next', '/'), safe='/?='))
724 def import_book(request):
725 """docstring for import_book"""
726 book_import_form = forms.BookImportForm(request.POST, request.FILES)
727 if book_import_form.is_valid():
729 book_import_form.save()
731 info = sys.exc_info()
732 exception = pprint.pformat(info[1])
733 tb = '\n'.join(traceback.format_tb(info[2]))
734 return HttpResponse(_("An error occurred: %(exception)s\n\n%(tb)s") % {'exception':exception, 'tb':tb}, mimetype='text/plain')
735 return HttpResponse(_("Book imported successfully"))
737 return HttpResponse(_("Error importing file: %r") % book_import_form.errors)
742 """ Provides server time for jquery.countdown,
743 in a format suitable for Date.parse()
745 return HttpResponse(datetime.now().strftime('%Y/%m/%d %H:%M:%S'))
751 Create a zip archive with all XML files.
752 This should be removed when we have real API.
754 temp = tempfile.TemporaryFile()
755 archive = zipfile.ZipFile(temp, 'w')
757 for book in models.Book.objects.all():
758 archive.write(book.xml_file.path, str('%s.xml' % book.slug))
761 response = HttpResponse(content_type='application/zip', mimetype='application/x-zip-compressed')
762 response['Content-Disposition'] = 'attachment; filename=xmls.zip'
763 response['Content-Length'] = temp.tell()
766 response.write(temp.read())