X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/ab72083a61fc7a6257101b31ec92d12ea61c7a03..4ddb93217b0476206908c0f2b4a66989631bdef3:/src/api/handlers.py diff --git a/src/api/handlers.py b/src/api/handlers.py index 01430cbcb..27477b3b2 100644 --- a/src/api/handlers.py +++ b/src/api/handlers.py @@ -6,7 +6,6 @@ import json from django.contrib.sites.models import Site from django.core.urlresolvers import reverse -from django.db.models import Prefetch from django.utils.functional import lazy from piston.handler import AnonymousBaseHandler, BaseHandler from piston.utils import rc @@ -14,7 +13,7 @@ from sorl.thumbnail import default from catalogue.forms import BookImportForm from catalogue.models import Book, Tag, BookMedia, Fragment, Collection -from catalogue.models.tag import TagRelation +from catalogue.models.tag import prefetch_relations from picture.models import Picture from picture.forms import PictureImportForm @@ -164,7 +163,7 @@ class AnonymousBooksHandler(AnonymousBaseHandler, BookDetails): """ allowed_methods = ('GET',) model = Book - fields = book_tag_categories + ['href', 'title', 'url', 'cover', 'cover_thumb'] + fields = book_tag_categories + ['href', 'title', 'url', 'cover', 'cover_thumb', 'slug'] @classmethod def genres(cls, book): @@ -172,7 +171,9 @@ class AnonymousBooksHandler(AnonymousBaseHandler, BookDetails): return book.tags.filter(category='genre') @piwik_track - def read(self, request, tags=None, top_level=False, audiobooks=False, daisy=False, pk=None): + def read(self, request, tags=None, top_level=False, audiobooks=False, daisy=False, pk=None, + recommended=False, newest=False, books=None, + after=None, before=None, count=None): """ Lists all books with given tags. :param tags: filtering tags; should be a path of categories @@ -199,7 +200,8 @@ class AnonymousBooksHandler(AnonymousBaseHandler, BookDetails): else: books = Book.tagged.with_all(tags) else: - books = Book.objects.all() + books = books if books is not None else Book.objects.all() + books = books.order_by('slug') if top_level: books = books.filter(parent=None) @@ -207,15 +209,25 @@ class AnonymousBooksHandler(AnonymousBaseHandler, BookDetails): books = books.filter(media__type='mp3').distinct() if daisy: books = books.filter(media__type='daisy').distinct() + if recommended: + books = books.filter(recommended=True) + if newest: + books = books.order_by('-created_at') + + if after: + books = books.filter(slug__gt=after) + if before: + books = books.filter(slug__lt=before) books = books.only('slug', 'title', 'cover', 'cover_thumb') for category in book_tag_categories: - books = books.prefetch_related( - Prefetch( - 'tag_relations', - queryset=TagRelation.objects.filter(tag__category=category) - .select_related('tag').only('tag__name_pl', 'object_id'), - to_attr='%s_relations' % category)) + books = prefetch_relations(books, category) + + if count: + if before: + books = list(reversed(books.order_by('-slug')[:count])) + else: + books = books[:count] if books: return books @@ -229,7 +241,7 @@ class AnonymousBooksHandler(AnonymousBaseHandler, BookDetails): class BooksHandler(BookDetailHandler): allowed_methods = ('GET', 'POST') model = Book - fields = book_tag_categories + ['href', 'title', 'url', 'cover', 'cover_thumb'] + fields = book_tag_categories + ['href', 'title', 'url', 'cover', 'cover_thumb', 'slug'] anonymous = AnonymousBooksHandler def create(self, request, *args, **kwargs): @@ -246,7 +258,35 @@ class BooksHandler(BookDetailHandler): class EBooksHandler(AnonymousBooksHandler): - fields = ('author', 'href', 'title', 'cover') + tuple(Book.ebook_formats) + fields = ('author', 'href', 'title', 'cover') + tuple(Book.ebook_formats) + ('slug',) + + +class FilterBooksHandler(AnonymousBooksHandler): + fields = book_tag_categories + ['href', 'title', 'url', 'cover', 'cover_thumb', 'slug'] + + def read(self, request, title_part=None, author_part=None, is_lektura=None, is_audiobook=None, + after=None, before=None, count=None): + if count is None: + count = 50 + if is_lektura in ('true', 'false'): + is_lektura = is_lektura == 'true' + else: + is_lektura = None + if is_audiobook in ('true', 'false'): + is_audiobook = is_audiobook == 'true' + books = Book.objects.distinct() + if title_part: + books = books.filter(title__iregex='\m' + title_part) + if author_part is not None: + books = books.filter(cached_author__iregex='\m' + author_part) + if is_lektura is not None: + books = books.filter(has_audience=is_lektura) + if is_audiobook is not None: + if is_audiobook: + books = books.filter(media__type='mp3') + else: + books = books.exclude(media__type='mp3') + return super(FilterBooksHandler, self).read(request, books=books, after=after, before=before, count=count) # add categorized tags fields for Book @@ -260,9 +300,7 @@ def _tags_getter(category): def _tag_getter(category): @classmethod def get_tag(cls, book): - if hasattr(book, '%s_relations' % category): - return ', '.join(rel.tag.name for rel in getattr(book, '%s_relations' % category)) - return ', '.join(book.tags.filter(category=category).values_list('name', flat=True)) + return book.tag_unicode(category) return get_tag