From 86f05630842437fb68b7dfa1fe50607a49826c86 Mon Sep 17 00:00:00 2001 From: Jan Szejko Date: Tue, 5 Jun 2018 10:29:06 +0200 Subject: [PATCH 1/1] api: filter books by preview --- src/api/handlers.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/api/handlers.py b/src/api/handlers.py index b37a6d9dc..77728208e 100644 --- a/src/api/handlers.py +++ b/src/api/handlers.py @@ -323,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) @@ -345,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] -- 2.20.1