X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/6e3e08e5354baaf82f7d46cbd88883e4c7426dce..0bb67240e8bc535ec703e34262487ba902c91079:/src/api/handlers.py?ds=inline diff --git a/src/api/handlers.py b/src/api/handlers.py index 199f030ce..77728208e 100644 --- a/src/api/handlers.py +++ b/src/api/handlers.py @@ -6,6 +6,7 @@ import json from django.contrib.sites.models import Site from django.core.urlresolvers import reverse +from django.http.response import HttpResponse from django.utils.functional import lazy from django.db import models from piston.handler import AnonymousBaseHandler, BaseHandler @@ -15,6 +16,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 prefetch_relations +from catalogue.utils import is_subscribed from picture.models import Picture from picture.forms import PictureImportForm @@ -278,6 +280,18 @@ class BooksHandler(BookDetailHandler): return rc.NOT_FOUND +class EpubHandler(BookDetailHandler): + def read(self, request, slug): + if not is_subscribed(request.user): + return rc.FORBIDDEN + try: + book = Book.objects.get(slug=slug) + except Book.DoesNotExist: + return rc.NOT_FOUND + response = HttpResponse(book.get_media('epub')) + return response + + class EBooksHandler(AnonymousBooksHandler): fields = ('author', 'href', 'title', 'cover') + tuple(Book.ebook_formats) + ('slug',) @@ -309,20 +323,21 @@ class FilterBooksHandler(AnonymousBooksHandler): fields = book_tag_categories + [ 'href', 'title', 'url', 'cover', 'cover_thumb', 'simple_thumb', 'slug', 'key'] + def parse_bool(self, s): + if s in ('true', 'false'): + return s == 'true' + else: + return None + def read(self, request): key_sep = '$' search_string = request.GET.get('search') - is_lektura = request.GET.get('lektura') - is_audiobook = request.GET.get('audiobook') + is_lektura = self.parse_bool(request.GET.get('lektura')) + is_audiobook = self.parse_bool(request.GET.get('audiobook')) + preview = self.parse_bool(request.GET.get('preview')) after = request.GET.get('after') count = int(request.GET.get('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().order_by('slug') if is_lektura is not None: books = books.filter(has_audience=is_lektura) @@ -331,6 +346,8 @@ class FilterBooksHandler(AnonymousBooksHandler): books = books.filter(media__type='mp3') else: books = books.exclude(media__type='mp3') + if preview is not None: + books = books.filter(preview=preview) for key in request.GET: if key in category_singular: category = category_singular[key]