More Py3 compatibility fixes.
[wolnelektury.git] / src / catalogue / api / views.py
index aba794d..7c4b888 100644 (file)
@@ -1,9 +1,25 @@
-from django.http import HttpResponse
+# -*- coding: utf-8 -*-
+# This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
+# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
+#
+import json
+from django.http import Http404, HttpResponse
 from rest_framework.generics import ListAPIView, RetrieveAPIView, get_object_or_404
 from rest_framework.generics import ListAPIView, RetrieveAPIView, get_object_or_404
+from rest_framework.permissions import DjangoModelPermissionsOrAnonReadOnly
+from rest_framework.response import Response
+from rest_framework import status
 from paypal.permissions import IsSubscribed
 from api.handlers import read_tags
 from paypal.permissions import IsSubscribed
 from api.handlers import read_tags
+from api.utils import vary_on_auth
+from .helpers import books_after, order_books
 from . import serializers
 from . import serializers
-from catalogue.models import Book, Collection, Tag, Fragment
+from catalogue.forms import BookImportForm
+from catalogue.models import Book, Collection, Tag, Fragment, BookMedia
+from catalogue.models.tag import prefetch_relations
+from wolnelektury.utils import re_escape
+
+
+book_tag_categories = ['author', 'epoch', 'kind', 'genre']
 
 
 class CollectionList(ListAPIView):
 
 
 class CollectionList(ListAPIView):
@@ -11,18 +27,202 @@ class CollectionList(ListAPIView):
     serializer_class = serializers.CollectionListSerializer
 
 
     serializer_class = serializers.CollectionListSerializer
 
 
+@vary_on_auth  # Because of 'liked'.
 class CollectionDetail(RetrieveAPIView):
     queryset = Collection.objects.all()
     lookup_field = 'slug'
     serializer_class = serializers.CollectionSerializer
 
 
 class CollectionDetail(RetrieveAPIView):
     queryset = Collection.objects.all()
     lookup_field = 'slug'
     serializer_class = serializers.CollectionSerializer
 
 
+@vary_on_auth  # Because of 'liked'.
+class BookList(ListAPIView):
+    permission_classes = [DjangoModelPermissionsOrAnonReadOnly]
+    queryset = Book.objects.none()  # Required for DjangoModelPermissions
+    serializer_class = serializers.BookListSerializer
+
+    def get_queryset(self):
+        try:
+            tags, ancestors = read_tags(
+                self.kwargs.get('tags', ''), self.request,
+                allowed=('author', 'epoch', 'kind', 'genre')
+            )
+        except ValueError:
+            raise Http404
+
+        new_api = self.request.query_params.get('new_api')
+        after = self.request.query_params.get('after', self.kwargs.get('after'))
+        count = self.request.query_params.get('count', self.kwargs.get('count'))
+        if count:
+            try:
+                count = int(count)
+            except TypeError:
+                raise Http404  # Fixme
+
+        if tags:
+            if self.kwargs.get('top_level'):
+                books = Book.tagged_top_level(tags)
+                if not books:
+                    raise Http404
+                return books
+            else:
+                books = Book.tagged.with_all(tags)
+        else:
+            books = Book.objects.all()
+        books = order_books(books, new_api)
+
+        if self.kwargs.get('top_level'):
+            books = books.filter(parent=None)
+        if self.kwargs.get('audiobooks'):
+            books = books.filter(media__type='mp3').distinct()
+        if self.kwargs.get('daisy'):
+            books = books.filter(media__type='daisy').distinct()
+        if self.kwargs.get('recommended'):
+            books = books.filter(recommended=True)
+        if self.kwargs.get('newest'):
+            books = books.order_by('-created_at')
+
+        if after:
+            books = books_after(books, after, new_api)
+
+        prefetch_relations(books, 'author')
+        prefetch_relations(books, 'genre')
+        prefetch_relations(books, 'kind')
+        prefetch_relations(books, 'epoch')
+
+        if count:
+            books = books[:count]
+
+        return books
+
+    def post(self, request, **kwargs):
+        if kwargs.get('audiobooks'):
+            return self.post_audiobook(request, **kwargs)
+        else:
+            return self.post_book(request, **kwargs)
+
+    def post_book(self, request, **kwargs):
+        data = json.loads(request.POST.get('data'))
+        form = BookImportForm(data)
+        if form.is_valid():
+            form.save()
+            return Response({}, status=status.HTTP_201_CREATED)
+        else:
+            raise Http404
+
+    def post_audiobook(self, request, **kwargs):
+        index = int(request.POST['part_index'])
+        parts_count = int(request.POST['parts_count'])
+        media_type = request.POST['type'].lower()
+        source_sha1 = request.POST.get('source_sha1')
+        name = request.POST.get('name', '')
+        part_name = request.POST.get('part_name', '')
+
+        _rest, slug = request.POST['book'].rstrip('/').rsplit('/', 1)
+        book = Book.objects.get(slug=slug)
+
+        try:
+            assert source_sha1
+            bm = book.media.get(type=media_type, source_sha1=source_sha1)
+        except (AssertionError, BookMedia.DoesNotExist):
+            bm = BookMedia(book=book, type=media_type)
+        bm.name = name
+        bm.part_name = part_name
+        bm.index = index
+        bm.file.save(None, request.data['file'], save=False)
+        bm.save(parts_count=parts_count)
+
+        return Response({}, status=status.HTTP_201_CREATED)
+
+
+@vary_on_auth  # Because of 'liked'.
 class BookDetail(RetrieveAPIView):
     queryset = Book.objects.all()
     lookup_field = 'slug'
     serializer_class = serializers.BookDetailSerializer
 
 
 class BookDetail(RetrieveAPIView):
     queryset = Book.objects.all()
     lookup_field = 'slug'
     serializer_class = serializers.BookDetailSerializer
 
 
+class EbookList(BookList):
+    serializer_class = serializers.EbookSerializer
+
+
+@vary_on_auth  # Because of 'liked'.
+class Preview(ListAPIView):
+    queryset = Book.objects.filter(preview=True)
+    serializer_class = serializers.BookPreviewSerializer
+
+
+@vary_on_auth  # Because of 'liked'.
+class FilterBookList(ListAPIView):
+    serializer_class = serializers.FilterBookListSerializer
+
+    def parse_bool(self, s):
+        if s in ('true', 'false'):
+            return s == 'true'
+        else:
+            return None
+
+    def get_queryset(self):
+        key_sep = '$'
+        search_string = self.request.query_params.get('search')
+        is_lektura = self.parse_bool(self.request.query_params.get('lektura'))
+        is_audiobook = self.parse_bool(self.request.query_params.get('audiobook'))
+        preview = self.parse_bool(self.request.query_params.get('preview'))
+
+        new_api = self.request.query_params.get('new_api')
+        after = self.request.query_params.get('after')
+        count = int(self.request.query_params.get('count', 50))
+        books = order_books(Book.objects.distinct(), new_api)
+        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')
+        if preview is not None:
+            books = books.filter(preview=preview)
+        for category in book_tag_categories:
+            category_plural = category + 's'
+            if category_plural in self.request.query_params:
+                slugs = self.request.query_params[category_plural].split(',')
+                tags = Tag.objects.filter(category=category, slug__in=slugs)
+                books = Book.tagged.with_any(tags, books)
+        if (search_string is not None) and len(search_string) < 3:
+            search_string = None
+        if search_string:
+            search_string = re_escape(search_string)
+            books_author = books.filter(cached_author__iregex=r'\m' + search_string)
+            books_title = books.filter(title__iregex=r'\m' + search_string)
+            books_title = books_title.exclude(id__in=list(books_author.values_list('id', flat=True)))
+            if after and (key_sep in after):
+                which, key = after.split(key_sep, 1)
+                if which == 'title':
+                    book_lists = [(books_after(books_title, key, new_api), 'title')]
+                else:  # which == 'author'
+                    book_lists = [(books_after(books_author, key, new_api), 'author'), (books_title, 'title')]
+            else:
+                book_lists = [(books_author, 'author'), (books_title, 'title')]
+        else:
+            if after and key_sep in after:
+                which, key = after.split(key_sep, 1)
+                books = books_after(books, key, new_api)
+            book_lists = [(books, 'book')]
+
+        filtered_books = []
+        for book_list, label in book_lists:
+            for category in book_tag_categories:
+                book_list = prefetch_relations(book_list, category)
+            remaining_count = count - len(filtered_books)
+            for book in book_list[:remaining_count]:
+                book.key = '%s%s%s' % (
+                    label, key_sep, book.slug if not new_api else book.full_sort_key())
+                filtered_books.append(book)
+            if len(filtered_books) == count:
+                break
+
+        return filtered_books
+
+
 class EpubView(RetrieveAPIView):
     queryset = Book.objects.all()
     lookup_field = 'slug'
 class EpubView(RetrieveAPIView):
     queryset = Book.objects.all()
     lookup_field = 'slug'
@@ -64,6 +264,7 @@ class TagView(RetrieveAPIView):
         )
 
 
         )
 
 
+@vary_on_auth  # Because of 'liked'.
 class FragmentList(ListAPIView):
     serializer_class = serializers.FragmentSerializer
 
 class FragmentList(ListAPIView):
     serializer_class = serializers.FragmentSerializer
 
@@ -79,6 +280,7 @@ class FragmentList(ListAPIView):
         return Fragment.tagged.with_all(tags).select_related('book')
 
 
         return Fragment.tagged.with_all(tags).select_related('book')
 
 
+@vary_on_auth  # Because of 'liked'.
 class FragmentView(RetrieveAPIView):
     serializer_class = serializers.FragmentDetailSerializer
 
 class FragmentView(RetrieveAPIView):
     serializer_class = serializers.FragmentDetailSerializer