Minor book funding page changes.
[wolnelektury.git] / src / catalogue / api / views.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
4 import json
5 import os.path
6 from django.conf import settings
7 from django.http import Http404, HttpResponse
8 from django.utils.decorators import method_decorator
9 from django.views.decorators.cache import never_cache
10 from rest_framework.generics import ListAPIView, RetrieveAPIView, get_object_or_404
11 from rest_framework.permissions import DjangoModelPermissionsOrAnonReadOnly
12 from rest_framework.response import Response
13 from rest_framework import status
14 from api.handlers import read_tags
15 from api.utils import vary_on_auth
16 from catalogue.forms import BookImportForm
17 from catalogue.models import Book, Collection, Tag, Fragment, BookMedia
18 from catalogue.models.tag import prefetch_relations
19 from club.models import Membership
20 from club.permissions import IsClubMember
21 from wolnelektury.utils import re_escape
22 from .helpers import books_after, order_books
23 from . import serializers
24
25
26 book_tag_categories = ['author', 'epoch', 'kind', 'genre']
27
28
29 class CollectionList(ListAPIView):
30     queryset = Collection.objects.all()
31     serializer_class = serializers.CollectionListSerializer
32
33
34 @vary_on_auth  # Because of 'liked'.
35 class CollectionDetail(RetrieveAPIView):
36     queryset = Collection.objects.all()
37     lookup_field = 'slug'
38     serializer_class = serializers.CollectionSerializer
39
40
41 @vary_on_auth  # Because of 'liked'.
42 class BookList(ListAPIView):
43     permission_classes = [DjangoModelPermissionsOrAnonReadOnly]
44     queryset = Book.objects.none()  # Required for DjangoModelPermissions
45     serializer_class = serializers.BookListSerializer
46
47     def get(self, request, filename=None, **kwargs):
48         if filename and not kwargs.get('tags') and 'count' not in request.query_params:
49             try:
50                 with open(os.path.join(settings.MEDIA_ROOT, 'api', '%s.%s' % (filename, request.accepted_renderer.format)), 'rb') as f:
51                     content = f.read()
52                 return HttpResponse(content, content_type=request.accepted_media_type)
53             except:
54                 pass
55         return super().get(request, filename=filename, **kwargs)
56
57     def get_queryset(self):
58         try:
59             tags, ancestors = read_tags(
60                 self.kwargs.get('tags', ''), self.request,
61                 allowed=('author', 'epoch', 'kind', 'genre')
62             )
63         except ValueError:
64             raise Http404
65
66         new_api = self.request.query_params.get('new_api')
67         after = self.request.query_params.get('after', self.kwargs.get('after'))
68         count = self.request.query_params.get('count', self.kwargs.get('count'))
69         if count:
70             try:
71                 count = int(count)
72             except TypeError:
73                 raise Http404  # Fixme
74
75         if tags:
76             if self.kwargs.get('top_level'):
77                 books = Book.tagged_top_level(tags)
78                 if not books:
79                     raise Http404
80                 return books
81             else:
82                 books = Book.tagged.with_all(tags)
83         else:
84             books = Book.objects.all()
85         books = books.filter(findable=True)
86         books = order_books(books, new_api)
87
88         if not Membership.is_active_for(self.request.user):
89             books = books.exclude(preview=True)
90
91         if self.kwargs.get('top_level'):
92             books = books.filter(parent=None)
93         if self.kwargs.get('audiobooks'):
94             books = books.filter(media__type='mp3').distinct()
95         if self.kwargs.get('daisy'):
96             books = books.filter(media__type='daisy').distinct()
97         if self.kwargs.get('recommended'):
98             books = books.filter(recommended=True)
99         if self.kwargs.get('newest'):
100             books = books.order_by('-created_at')
101
102         if after:
103             books = books_after(books, after, new_api)
104
105         prefetch_relations(books, 'author')
106         prefetch_relations(books, 'genre')
107         prefetch_relations(books, 'kind')
108         prefetch_relations(books, 'epoch')
109
110         if count:
111             books = books[:count]
112
113         return books
114
115     def post(self, request, **kwargs):
116         if kwargs.get('audiobooks'):
117             return self.post_audiobook(request, **kwargs)
118         else:
119             return self.post_book(request, **kwargs)
120
121     def post_book(self, request, **kwargs):
122         data = json.loads(request.POST.get('data'))
123         form = BookImportForm(data)
124         if form.is_valid():
125             form.save()
126             return Response({}, status=status.HTTP_201_CREATED)
127         else:
128             raise Http404
129
130     def post_audiobook(self, request, **kwargs):
131         index = int(request.POST['part_index'])
132         parts_count = int(request.POST['parts_count'])
133         media_type = request.POST['type'].lower()
134         source_sha1 = request.POST.get('source_sha1')
135         name = request.POST.get('name', '')
136         part_name = request.POST.get('part_name', '')
137
138         _rest, slug = request.POST['book'].rstrip('/').rsplit('/', 1)
139         book = Book.objects.get(slug=slug)
140
141         try:
142             assert source_sha1
143             bm = book.media.get(type=media_type, source_sha1=source_sha1)
144         except (AssertionError, BookMedia.DoesNotExist):
145             bm = BookMedia(book=book, type=media_type)
146         bm.name = name
147         bm.part_name = part_name
148         bm.index = index
149         bm.file.save(None, request.data['file'], save=False)
150         bm.save(parts_count=parts_count)
151
152         return Response({}, status=status.HTTP_201_CREATED)
153
154
155 @vary_on_auth  # Because of 'liked'.
156 class BookDetail(RetrieveAPIView):
157     queryset = Book.objects.all()
158     lookup_field = 'slug'
159     serializer_class = serializers.BookDetailSerializer
160
161
162 @vary_on_auth  # Because of embargo links.
163 class EbookList(BookList):
164     serializer_class = serializers.EbookSerializer
165
166
167 @method_decorator(never_cache, name='dispatch')
168 class Preview(ListAPIView):
169     #queryset = Book.objects.filter(preview=True)
170     serializer_class = serializers.BookPreviewSerializer
171
172     def get_queryset(self):
173         qs = Book.objects.filter(preview=True)
174         # FIXME: temporary workaround for a problem with iOS app; see #3954.
175         if 'Darwin' in self.request.META.get('HTTP_USER_AGENT', '') and 'debug' not in self.request.GET:
176             qs = qs.none()
177         return qs
178
179
180 @vary_on_auth  # Because of 'liked'.
181 class FilterBookList(ListAPIView):
182     serializer_class = serializers.FilterBookListSerializer
183
184     def parse_bool(self, s):
185         if s in ('true', 'false'):
186             return s == 'true'
187         else:
188             return None
189
190     def get_queryset(self):
191         key_sep = '$'
192         search_string = self.request.query_params.get('search')
193         is_lektura = self.parse_bool(self.request.query_params.get('lektura'))
194         is_audiobook = self.parse_bool(self.request.query_params.get('audiobook'))
195         preview = self.parse_bool(self.request.query_params.get('preview'))
196         if not Membership.is_active_for(self.request.user):
197             preview = False
198
199         new_api = self.request.query_params.get('new_api')
200         after = self.request.query_params.get('after')
201         count = int(self.request.query_params.get('count', 50))
202         books = order_books(Book.objects.distinct(), new_api)
203         books = books.filter(findable=True)
204         if is_lektura is not None:
205             books = books.filter(has_audience=is_lektura)
206         if is_audiobook is not None:
207             if is_audiobook:
208                 books = books.filter(media__type='mp3')
209             else:
210                 books = books.exclude(media__type='mp3')
211         if preview is not None:
212             books = books.filter(preview=preview)
213         for category in book_tag_categories:
214             category_plural = category + 's'
215             if category_plural in self.request.query_params:
216                 slugs = self.request.query_params[category_plural].split(',')
217                 tags = Tag.objects.filter(category=category, slug__in=slugs)
218                 books = Book.tagged.with_any(tags, books)
219         if (search_string is not None) and len(search_string) < 3:
220             search_string = None
221         if search_string:
222             search_string = re_escape(search_string)
223             books_author = books.filter(cached_author__iregex=r'\m' + search_string)
224             books_title = books.filter(title__iregex=r'\m' + search_string)
225             books_title = books_title.exclude(id__in=list(books_author.values_list('id', flat=True)))
226             if after and (key_sep in after):
227                 which, key = after.split(key_sep, 1)
228                 if which == 'title':
229                     book_lists = [(books_after(books_title, key, new_api), 'title')]
230                 else:  # which == 'author'
231                     book_lists = [(books_after(books_author, key, new_api), 'author'), (books_title, 'title')]
232             else:
233                 book_lists = [(books_author, 'author'), (books_title, 'title')]
234         else:
235             if after and key_sep in after:
236                 which, key = after.split(key_sep, 1)
237                 books = books_after(books, key, new_api)
238             book_lists = [(books, 'book')]
239
240         filtered_books = []
241         for book_list, label in book_lists:
242             for category in book_tag_categories:
243                 book_list = prefetch_relations(book_list, category)
244             remaining_count = count - len(filtered_books)
245             for book in book_list[:remaining_count]:
246                 book.key = '%s%s%s' % (
247                     label, key_sep, book.slug if not new_api else book.full_sort_key())
248                 filtered_books.append(book)
249             if len(filtered_books) == count:
250                 break
251
252         return filtered_books
253
254
255 class EpubView(RetrieveAPIView):
256     queryset = Book.objects.all()
257     lookup_field = 'slug'
258     permission_classes = [IsClubMember]
259
260     @method_decorator(never_cache)
261     def get(self, *args, **kwargs):
262         return HttpResponse(self.get_object().get_media('epub'))
263
264
265 class TagCategoryView(ListAPIView):
266     serializer_class = serializers.TagSerializer
267
268     def get_queryset(self):
269         category = self.kwargs['category']
270         tags = Tag.objects.filter(category=category).exclude(items=None).order_by('slug')
271         if self.request.query_params.get('book_only') == 'true':
272             tags = tags.filter(for_books=True)
273         if self.request.GET.get('picture_only') == 'true':
274             tags = filter(for_pictures=True)
275
276         after = self.request.query_params.get('after')
277         count = self.request.query_params.get('count')
278         if after:
279             tags = tags.filter(slug__gt=after)
280         if count:
281             tags = tags[:count]
282
283         return tags
284
285
286 class TagView(RetrieveAPIView):
287     serializer_class = serializers.TagDetailSerializer
288
289     def get_object(self):
290         return get_object_or_404(
291             Tag,
292             category=self.kwargs['category'],
293             slug=self.kwargs['slug']
294         )
295
296
297 @vary_on_auth  # Because of 'liked'.
298 class FragmentList(ListAPIView):
299     serializer_class = serializers.FragmentSerializer
300
301     def get_queryset(self):
302         try:
303             tags, ancestors = read_tags(
304                 self.kwargs['tags'],
305                 self.request,
306                 allowed={'author', 'epoch', 'kind', 'genre', 'book', 'theme'}
307             )
308         except ValueError:
309             raise Http404
310         return Fragment.tagged.with_all(tags).filter(book__findable=True).select_related('book')
311
312
313 @vary_on_auth  # Because of 'liked'.
314 class FragmentView(RetrieveAPIView):
315     serializer_class = serializers.FragmentDetailSerializer
316
317     def get_object(self):
318         return get_object_or_404(
319             Fragment,
320             book__slug=self.kwargs['book'],
321             anchor=self.kwargs['anchor']
322         )