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.
5 from collections import OrderedDict
8 from django.conf import settings
9 from django.core.cache import get_cache
10 from django.template import RequestContext
11 from django.template.loader import render_to_string
12 from django.shortcuts import render_to_response, get_object_or_404
13 from django.http import HttpResponse, HttpResponseRedirect, Http404, HttpResponsePermanentRedirect, JsonResponse
14 from django.core.urlresolvers import reverse
15 from django.db.models import Q
16 from django.contrib.auth.decorators import login_required, user_passes_test
17 from django.utils.http import urlquote_plus
18 from django.utils import translation
19 from django.utils.translation import get_language, ugettext as _, ugettext_lazy
20 from django.views.decorators.vary import vary_on_headers
22 from ajaxable.utils import AjaxableFormView
23 from catalogue import models
24 from catalogue import forms
25 from .helpers import get_related_tags, get_fragment_related_tags, tags_usage_for_books, tags_usage_for_works, tags_usage_for_fragments
26 from catalogue.utils import split_tags, MultiQuerySet, SortedMultiQuerySet
27 from catalogue.templatetags.catalogue_tags import tag_list, collection_list
28 from pdcounter import models as pdcounter_models
29 from pdcounter import views as pdcounter_views
30 from suggest.forms import PublishingSuggestForm
31 from picture.models import Picture, PictureArea
32 from picture.views import picture_list_thumb
34 staff_required = user_passes_test(lambda user: user.is_staff)
35 permanent_cache = get_cache('permanent')
38 @vary_on_headers('X-Requested-With')
39 def catalogue(request):
40 #cache_key = 'catalogue.catalogue/' + get_language()
41 #output = permanent_cache.get(cache_key)
45 common_categories = ('author',)
46 split_categories = ('epoch', 'genre', 'kind')
48 categories = split_tags(tags_usage_for_works(common_categories))
49 book_categories = split_tags(tags_usage_for_books(split_categories))
50 picture_categories = split_tags(
51 models.Tag.objects.usage_for_model(Picture, counts=True).filter(
52 category__in=split_categories))
53 # we want global usage for themes
54 fragment_tags = list(tags_usage_for_fragments(('theme',)))
55 collections = models.Collection.objects.all()
57 render_tag_list = lambda x: render_to_string(
58 'catalogue/tag_list.html', tag_list(x))
60 def render_split(with_books, with_pictures):
63 ctx['books'] = render_tag_list(with_books)
65 ctx['pictures'] = render_tag_list(with_pictures)
66 return render_to_string('catalogue/tag_list_split.html', ctx)
69 output['theme'] = render_tag_list(fragment_tags)
70 for category in common_categories:
71 output[category] = render_tag_list(categories.get(category, []))
72 for category in split_categories:
73 output[category] = render_split(
74 book_categories.get(category, []),
75 picture_categories.get(category, []))
77 output['collections'] = render_to_string(
78 'catalogue/collection_list.html', collection_list(collections))
79 #permanent_cache.set(cache_key, output)
81 return JsonResponse(output)
83 return render_to_response('catalogue/catalogue.html', locals(),
84 context_instance=RequestContext(request))
87 def book_list(request, filter=None, get_filter=None,
88 template_name='catalogue/book_list.html',
89 nav_template_name='catalogue/snippets/book_list_nav.html',
90 list_template_name='catalogue/snippets/book_list.html',
91 cache_key='catalogue.book_list',
94 """ generates a listing of all books, optionally filtered with a test function """
95 cache_key = "%s/%s" % (cache_key, get_language())
96 cached = permanent_cache.get(cache_key)
97 if cached is not None:
98 rendered_nav, rendered_book_list = cached
101 filter = get_filter()
102 books_by_author, orphans, books_by_parent = models.Book.book_list(filter)
103 books_nav = OrderedDict()
104 for tag in books_by_author:
105 if books_by_author[tag]:
106 books_nav.setdefault(tag.sort_key[0], []).append(tag)
107 rendered_nav = render_to_string(nav_template_name, locals())
108 rendered_book_list = render_to_string(list_template_name, locals())
109 permanent_cache.set(cache_key, (rendered_nav, rendered_book_list))
110 return render_to_response(template_name, locals(),
111 context_instance=RequestContext(request))
114 def audiobook_list(request):
115 return book_list(request, Q(media__type='mp3') | Q(media__type='ogg'),
116 template_name='catalogue/audiobook_list.html',
117 list_template_name='catalogue/snippets/audiobook_list.html',
118 cache_key='catalogue.audiobook_list')
121 def daisy_list(request):
122 return book_list(request, Q(media__type='daisy'),
123 template_name='catalogue/daisy_list.html',
124 cache_key='catalogue.daisy_list')
127 def collection(request, slug):
128 coll = get_object_or_404(models.Collection, slug=slug)
129 if coll.kind == 'book':
131 tmpl = "catalogue/collection.html"
132 elif coll.kind == 'picture':
133 view = picture_list_thumb
134 tmpl = "picture/collection.html"
136 raise ValueError('How do I show this kind of collection? %s' % coll.kind)
137 return view(request, get_filter=coll.get_query,
139 cache_key='catalogue.collection:%s' % coll.slug,
140 context={'collection': coll})
143 def differentiate_tags(request, tags, ambiguous_slugs):
144 beginning = '/'.join(tag.url_chunk for tag in tags)
145 unparsed = '/'.join(ambiguous_slugs[1:])
147 for tag in models.Tag.objects.filter(slug=ambiguous_slugs[0]):
149 'url_args': '/'.join((beginning, tag.url_chunk, unparsed)).strip('/'),
152 return render_to_response('catalogue/differentiate_tags.html',
153 {'tags': tags, 'options': options, 'unparsed': ambiguous_slugs[1:]},
154 context_instance=RequestContext(request))
157 # TODO: Rewrite this hellish piece of code which tries to do everything
158 def tagged_object_list(request, tags=''):
159 # preliminary tests and conditions
161 tags = models.Tag.get_tag_list(tags)
162 except models.Tag.DoesNotExist:
163 # Perhaps the user is asking about an author in Public Domain
164 # counter (they are not represented in tags)
165 chunks = tags.split('/')
166 if len(chunks) == 2 and chunks[0] == 'autor':
167 return pdcounter_views.author_detail(request, chunks[1])
170 except models.Tag.MultipleObjectsReturned, e:
171 # Ask the user to disambiguate
172 return differentiate_tags(request, e.tags, e.ambiguous_slugs)
173 except models.Tag.UrlDeprecationWarning, e:
174 return HttpResponsePermanentRedirect(reverse('tagged_object_list', args=['/'.join(tag.url_chunk for tag in e.tags)]))
177 if len(tags) > settings.MAX_TAG_LIST:
179 except AttributeError:
182 if len([tag for tag in tags if tag.category == 'book']):
185 # beginning of digestion
186 theme_is_set = [tag for tag in tags if tag.category == 'theme']
187 shelf_is_set = [tag for tag in tags if tag.category == 'set']
188 only_shelf = shelf_is_set and len(tags) == 1
189 only_my_shelf = only_shelf and request.user.is_authenticated() and request.user == tags[0].user
197 shelf_tags = [tag for tag in tags if tag.category == 'set']
198 fragment_tags = [tag for tag in tags if tag.category != 'set']
199 fragments = models.Fragment.tagged.with_all(fragment_tags)
200 areas = PictureArea.tagged.with_all(fragment_tags)
203 # FIXME: book tags here
204 books = models.Book.tagged.with_all(shelf_tags).order_by()
205 l_tags = models.Tag.objects.filter(category='book',
206 slug__in=[book.book_tag_slug() for book in books.iterator()])
207 fragments = models.Fragment.tagged.with_any(l_tags, fragments)
209 related_tags = get_fragment_related_tags(tags)
210 categories = split_tags(related_tags, categories)
211 object_queries.insert(0, fragments)
213 area_keys = [area.pk for area in areas.iterator()]
215 related_tags = PictureArea.tags.usage(counts=True,
216 filters={'pk__in': area_keys})
217 related_tags = (tag for tag in related_tags if tag not in fragment_tags)
219 categories = split_tags(related_tags, categories)
221 # we want the Pictures to go first
222 object_queries.insert(0, areas)
223 objects = MultiQuerySet(*object_queries)
226 books = models.Book.tagged.with_all(tags).order_by(
227 'sort_key_author', 'title')
229 books = models.Book.tagged_top_level(tags).order_by(
230 'sort_key_author', 'title')
232 pictures = Picture.tagged.with_all(tags).order_by(
233 'sort_key_author', 'title')
235 categories = split_tags(get_related_tags(tags))
237 objects = SortedMultiQuerySet(pictures, books,
238 order_by=('sort_key_author', 'title'))
242 objects = models.Book.objects.none()
244 return render_to_response('catalogue/tagged_object_list.html',
246 'object_list': objects,
247 'categories': categories,
248 'only_shelf': only_shelf,
249 'only_my_shelf': only_my_shelf,
250 'formats_form': forms.DownloadFormatsForm(),
252 'theme_is_set': theme_is_set,
254 context_instance=RequestContext(request))
257 def book_fragments(request, slug, theme_slug):
258 book = get_object_or_404(models.Book, slug=slug)
259 theme = get_object_or_404(models.Tag, slug=theme_slug, category='theme')
260 fragments = models.Fragment.tagged.with_all([theme]).filter(
261 Q(book=book) | Q(book__ancestor=book))
263 return render_to_response('catalogue/book_fragments.html', locals(),
264 context_instance=RequestContext(request))
267 def book_detail(request, slug):
269 book = models.Book.objects.get(slug=slug)
270 except models.Book.DoesNotExist:
271 return pdcounter_views.book_stub_detail(request, slug)
273 book_children = book.children.all().order_by('parent_number', 'sort_key')
274 return render_to_response('catalogue/book_detail.html', locals(),
275 context_instance=RequestContext(request))
278 def player(request, slug):
279 book = get_object_or_404(models.Book, slug=slug)
280 if not book.has_media('mp3'):
284 for m in book.media.filter(type='ogg').order_by().iterator():
285 ogg_files[m.name] = m
290 for mp3 in book.media.filter(type='mp3').iterator():
291 # ogg files are always from the same project
292 meta = mp3.extra_info
293 project = meta.get('project')
296 project = u'CzytamySłuchając'
298 projects.add((project, meta.get('funded_by', '')))
302 ogg = ogg_files.get(mp3.name)
307 audiobooks.append(media)
309 projects = sorted(projects)
311 extra_info = book.extra_info
313 return render_to_response('catalogue/player.html', locals(),
314 context_instance=RequestContext(request))
317 def book_text(request, slug):
318 book = get_object_or_404(models.Book, slug=slug)
320 if not book.has_html_file():
322 return render_to_response('catalogue/book_text.html', locals(),
323 context_instance=RequestContext(request))
330 def _no_diacritics_regexp(query):
331 """ returns a regexp for searching for a query without diacritics
333 should be locale-aware """
335 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źżŹŻ',
336 u'ą':u'ąĄ', u'ć':u'ćĆ', u'ę':u'ęĘ', u'ł': u'łŁ', u'ń':u'ńŃ', u'ó':u'óÓ', u'ś':u'śŚ', u'ź':u'źŹ', u'ż':u'żŻ'
340 return u"(%s)" % '|'.join(names[l])
341 return re.sub(u'[%s]' % (u''.join(names.keys())), repl, query)
343 def unicode_re_escape(query):
344 """ Unicode-friendly version of re.escape """
345 return re.sub('(?u)(\W)', r'\\\1', query)
347 def _word_starts_with(name, prefix):
348 """returns a Q object getting models having `name` contain a word
349 starting with `prefix`
351 We define word characters as alphanumeric and underscore, like in JS.
353 Works for MySQL, PostgreSQL, Oracle.
354 For SQLite, _sqlite* version is substituted for this.
358 prefix = _no_diacritics_regexp(unicode_re_escape(prefix))
359 # can't use [[:<:]] (word start),
360 # but we want both `xy` and `(xy` to catch `(xyz)`
361 kwargs['%s__iregex' % name] = u"(^|[^[:alnum:]_])%s" % prefix
366 def _word_starts_with_regexp(prefix):
367 prefix = _no_diacritics_regexp(unicode_re_escape(prefix))
368 return ur"(^|(?<=[^\wąćęłńóśźżĄĆĘŁŃÓŚŹŻ]))%s" % prefix
371 def _sqlite_word_starts_with(name, prefix):
372 """ version of _word_starts_with for SQLite
374 SQLite in Django uses Python re module
377 kwargs['%s__iregex' % name] = _word_starts_with_regexp(prefix)
381 if hasattr(settings, 'DATABASES'):
382 if settings.DATABASES['default']['ENGINE'] == 'django.db.backends.sqlite3':
383 _word_starts_with = _sqlite_word_starts_with
384 elif settings.DATABASE_ENGINE == 'sqlite3':
385 _word_starts_with = _sqlite_word_starts_with
389 def __init__(self, name, view):
392 self.lower = name.lower()
393 self.category = 'application'
395 return reverse(*self._view)
398 App(u'Leśmianator', (u'lesmianator', )),
402 def _tags_starting_with(prefix, user=None):
403 prefix = prefix.lower()
405 book_stubs = pdcounter_models.BookStub.objects.filter(_word_starts_with('title', prefix))
406 authors = pdcounter_models.Author.objects.filter(_word_starts_with('name', prefix))
408 books = models.Book.objects.filter(_word_starts_with('title', prefix))
409 tags = models.Tag.objects.filter(_word_starts_with('name', prefix))
410 if user and user.is_authenticated():
411 tags = tags.filter(~Q(category='set') | Q(user=user))
413 tags = tags.exclude(category='set')
415 prefix_regexp = re.compile(_word_starts_with_regexp(prefix))
416 return list(books) + list(tags) + [app for app in _apps if prefix_regexp.search(app.lower)] + list(book_stubs) + list(authors)
419 def _get_result_link(match, tag_list):
420 if isinstance(match, models.Tag):
421 return reverse('catalogue.views.tagged_object_list',
422 kwargs={'tags': '/'.join(tag.url_chunk for tag in tag_list + [match])}
424 elif isinstance(match, App):
427 return match.get_absolute_url()
430 def _get_result_type(match):
431 if isinstance(match, models.Book) or isinstance(match, pdcounter_models.BookStub):
434 type = match.category
438 def books_starting_with(prefix):
439 prefix = prefix.lower()
440 return models.Book.objects.filter(_word_starts_with('title', prefix))
443 def find_best_matches(query, user=None):
444 """ Finds a models.Book, Tag, models.BookStub or Author best matching a query.
447 - zero elements when nothing is found,
448 - one element when a best result is found,
449 - more then one element on multiple exact matches
451 Raises a ValueError on too short a query.
454 query = query.lower()
456 raise ValueError("query must have at least two characters")
458 result = tuple(_tags_starting_with(query, user))
459 # remove pdcounter stuff
460 book_titles = set(match.pretty_title().lower() for match in result
461 if isinstance(match, models.Book))
462 authors = set(match.name.lower() for match in result
463 if isinstance(match, models.Tag) and match.category == 'author')
464 result = tuple(res for res in result if not (
465 (isinstance(res, pdcounter_models.BookStub) and res.pretty_title().lower() in book_titles)
466 or (isinstance(res, pdcounter_models.Author) and res.name.lower() in authors)
469 exact_matches = tuple(res for res in result if res.name.lower() == query)
473 return tuple(result)[:1]
477 tags = request.GET.get('tags', '')
478 prefix = request.GET.get('q', '')
481 tag_list = models.Tag.get_tag_list(tags)
486 result = find_best_matches(prefix, request.user)
488 return render_to_response('catalogue/search_too_short.html', {'tags':tag_list, 'prefix':prefix},
489 context_instance=RequestContext(request))
492 return HttpResponseRedirect(_get_result_link(result[0], tag_list))
493 elif len(result) > 1:
494 return render_to_response('catalogue/search_multiple_hits.html',
495 {'tags':tag_list, 'prefix':prefix, 'results':((x, _get_result_link(x, tag_list), _get_result_type(x)) for x in result)},
496 context_instance=RequestContext(request))
498 form = PublishingSuggestForm(initial={"books": prefix + ", "})
499 return render_to_response('catalogue/search_no_hits.html',
500 {'tags':tag_list, 'prefix':prefix, "pubsuggest_form": form},
501 context_instance=RequestContext(request))
504 def tags_starting_with(request):
505 prefix = request.GET.get('q', '')
506 # Prefix must have at least 2 characters
508 return HttpResponse('')
511 for tag in _tags_starting_with(prefix, request.user):
512 if not tag.name in tags_list:
513 result += "\n" + tag.name
514 tags_list.append(tag.name)
515 return HttpResponse(result)
517 def json_tags_starting_with(request, callback=None):
519 prefix = request.GET.get('q', '')
520 callback = request.GET.get('callback', '')
521 # Prefix must have at least 2 characters
523 return HttpResponse('')
525 for tag in _tags_starting_with(prefix, request.user):
526 if not tag.name in tags_list:
527 tags_list.append(tag.name)
528 if request.GET.get('mozhint', ''):
529 result = [prefix, tags_list]
531 result = {"matches": tags_list}
532 return JsonResponse(result, callback)
540 def import_book(request):
541 """docstring for import_book"""
542 book_import_form = forms.BookImportForm(request.POST, request.FILES)
543 if book_import_form.is_valid():
545 book_import_form.save()
550 info = sys.exc_info()
551 exception = pprint.pformat(info[1])
552 tb = '\n'.join(traceback.format_tb(info[2]))
553 return HttpResponse(_("An error occurred: %(exception)s\n\n%(tb)s") % {'exception':exception, 'tb':tb}, mimetype='text/plain')
554 return HttpResponse(_("Book imported successfully"))
556 return HttpResponse(_("Error importing file: %r") % book_import_form.errors)
561 def book_info(request, id, lang='pl'):
562 book = get_object_or_404(models.Book, id=id)
563 # set language by hand
564 translation.activate(lang)
565 return render_to_response('catalogue/book_info.html', locals(),
566 context_instance=RequestContext(request))
569 def tag_info(request, id):
570 tag = get_object_or_404(models.Tag, id=id)
571 return HttpResponse(tag.description)
574 def download_zip(request, format, slug=None):
576 if format in models.Book.ebook_formats:
577 url = models.Book.zip_format(format)
578 elif format in ('mp3', 'ogg') and slug is not None:
579 book = get_object_or_404(models.Book, slug=slug)
580 url = book.zip_audiobooks(format)
582 raise Http404('No format specified for zip package')
583 return HttpResponseRedirect(urlquote_plus(settings.MEDIA_URL + url, safe='/?='))
586 class CustomPDFFormView(AjaxableFormView):
587 form_class = forms.CustomPDFForm
588 title = ugettext_lazy('Download custom PDF')
589 submit = ugettext_lazy('Download')
592 def __call__(self, *args, **kwargs):
593 if settings.NO_CUSTOM_PDF:
594 raise Http404('Custom PDF is disabled')
595 return super(CustomPDFFormView, self).__call__(*args, **kwargs)
597 def form_args(self, request, obj):
598 """Override to parse view args and give additional args to the form."""
601 def get_object(self, request, slug, *args, **kwargs):
602 return get_object_or_404(models.Book, slug=slug)
604 def context_description(self, request, obj):
605 return obj.pretty_title()