Minor upgrades and test fixes.
[wolnelektury.git] / src / catalogue / api / views.py
1 # This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
3 #
4 import json
5 import os.path
6 from urllib.request import urlopen
7 from django.conf import settings
8 from django.core.files.base import ContentFile
9 from django.http import Http404, HttpResponse
10 from django.utils.decorators import method_decorator
11 from django.views.decorators.cache import never_cache
12 from rest_framework.generics import (ListAPIView, RetrieveAPIView,
13                                      RetrieveUpdateAPIView, get_object_or_404)
14 from rest_framework.permissions import DjangoModelPermissionsOrAnonReadOnly
15 from rest_framework.response import Response
16 from rest_framework import status
17 from api.handlers import read_tags
18 from api.utils import vary_on_auth
19 from catalogue.forms import BookImportForm
20 from catalogue.models import Book, Collection, Tag, Fragment, BookMedia
21 from catalogue.models.tag import prefetch_relations
22 from club.models import Membership
23 from club.permissions import IsClubMember
24 from sortify import sortify
25 from wolnelektury.utils import re_escape
26 from .helpers import books_after, order_books
27 from . import serializers
28
29
30 book_tag_categories = ['author', 'epoch', 'kind', 'genre']
31
32
33 class LegacyListAPIView(ListAPIView):
34     pagination_class = None
35
36
37 class CreateOnPutMixin:
38     '''
39     Creates a new model instance when PUTting a nonexistent resource.
40     '''
41     def get_object(self):
42         try:
43             return super().get_object()
44         except Http404:
45             if self.request.method == 'PUT':
46                 lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field
47                 return self.get_queryset().model(**{
48                     self.lookup_field: self.kwargs[lookup_url_kwarg]
49                 })
50             else:
51                 raise
52
53
54 class CollectionList(LegacyListAPIView):
55     queryset = Collection.objects.filter(listed=True)
56     serializer_class = serializers.CollectionListSerializer
57
58
59 @vary_on_auth  # Because of 'liked'.
60 class CollectionDetail(CreateOnPutMixin, RetrieveUpdateAPIView):
61     permission_classes = [DjangoModelPermissionsOrAnonReadOnly]
62     queryset = Collection.objects.all()
63     lookup_field = 'slug'
64     serializer_class = serializers.CollectionSerializer
65
66
67 @vary_on_auth  # Because of 'liked'.
68 class BookList(LegacyListAPIView):
69     permission_classes = [DjangoModelPermissionsOrAnonReadOnly]
70     queryset = Book.objects.none()  # Required for DjangoModelPermissions
71     serializer_class = serializers.BookListSerializer
72
73     def get(self, request, filename=None, **kwargs):
74         if filename and not kwargs.get('tags') and 'count' not in request.query_params:
75             try:
76                 with open(os.path.join(settings.MEDIA_ROOT, 'api', '%s.%s' % (filename, request.accepted_renderer.format)), 'rb') as f:
77                     content = f.read()
78                 return HttpResponse(content, content_type=request.accepted_media_type)
79             except:
80                 pass
81         return super().get(request, filename=filename, **kwargs)
82
83     def get_queryset(self):
84         try:
85             tags, ancestors = read_tags(
86                 self.kwargs.get('tags', ''), self.request,
87                 allowed=('author', 'epoch', 'kind', 'genre')
88             )
89         except ValueError:
90             raise Http404
91
92         new_api = self.request.query_params.get('new_api')
93         after = self.request.query_params.get('after', self.kwargs.get('after'))
94         count = self.request.query_params.get('count', self.kwargs.get('count'))
95         if count:
96             try:
97                 count = int(count)
98             except TypeError:
99                 raise Http404  # Fixme
100
101         if tags:
102             if self.kwargs.get('top_level'):
103                 books = Book.tagged_top_level(tags)
104                 if not books:
105                     raise Http404
106                 return books
107             else:
108                 books = Book.tagged.with_all(tags)
109         else:
110             books = Book.objects.all()
111         books = books.filter(findable=True)
112         books = order_books(books, new_api)
113
114         if not Membership.is_active_for(self.request.user):
115             books = books.exclude(preview=True)
116
117         if self.kwargs.get('top_level'):
118             books = books.filter(parent=None)
119         if self.kwargs.get('audiobooks'):
120             books = books.filter(media__type='mp3').distinct()
121         if self.kwargs.get('daisy'):
122             books = books.filter(media__type='daisy').distinct()
123         if self.kwargs.get('recommended'):
124             books = books.filter(recommended=True)
125         if self.kwargs.get('newest'):
126             books = books.order_by('-created_at')
127
128         if after:
129             books = books_after(books, after, new_api)
130
131         prefetch_relations(books, 'author')
132         prefetch_relations(books, 'genre')
133         prefetch_relations(books, 'kind')
134         prefetch_relations(books, 'epoch')
135
136         if count:
137             books = books[:count]
138
139         return books
140
141     def post(self, request, **kwargs):
142         if kwargs.get('audiobooks'):
143             return self.post_audiobook(request, **kwargs)
144         else:
145             return self.post_book(request, **kwargs)
146
147     def post_book(self, request, **kwargs):
148         data = json.loads(request.POST.get('data'))
149         form = BookImportForm(data)
150         if form.is_valid():
151             form.save()
152             return Response({}, status=status.HTTP_201_CREATED)
153         else:
154             raise Http404
155
156     def post_audiobook(self, request, **kwargs):
157         index = int(request.POST['part_index'])
158         parts_count = int(request.POST['parts_count'])
159         media_type = request.POST['type'].lower()
160         source_sha1 = request.POST.get('source_sha1')
161         name = request.POST.get('name', '')
162         part_name = request.POST.get('part_name', '')
163
164         project_description = request.POST.get('project_description', '')
165         project_icon = request.POST.get('project_icon', '')
166
167         _rest, slug = request.POST['book'].rstrip('/').rsplit('/', 1)
168         book = Book.objects.get(slug=slug)
169
170         try:
171             assert source_sha1
172             bm = book.media.get(type=media_type, source_sha1=source_sha1)
173         except (AssertionError, BookMedia.DoesNotExist):
174             bm = BookMedia(book=book, type=media_type)
175         bm.name = name
176         bm.part_name = part_name
177         bm.index = index
178         bm.project_description = project_description
179         bm.project_icon = project_icon
180         bm.file.save(None, request.data['file'], save=False)
181         bm.save(parts_count=parts_count)
182
183         return Response({}, status=status.HTTP_201_CREATED)
184
185
186 class BookList2(ListAPIView):
187     permission_classes = [DjangoModelPermissionsOrAnonReadOnly]
188     queryset = Book.objects.none()  # Required for DjangoModelPermissions
189     serializer_class = serializers.BookSerializer2
190
191     def get_queryset(self):
192         books = Book.objects.all()
193         books = books.filter(findable=True)
194         books = order_books(books, True)
195
196         return books
197
198
199 @vary_on_auth  # Because of 'liked'.
200 class BookDetail(RetrieveAPIView):
201     queryset = Book.objects.all()
202     lookup_field = 'slug'
203     serializer_class = serializers.BookDetailSerializer
204
205
206 class BookDetail2(RetrieveAPIView):
207     queryset = Book.objects.all()
208     lookup_field = 'slug'
209     serializer_class = serializers.BookSerializer2
210
211
212 @vary_on_auth  # Because of embargo links.
213 class EbookList(BookList):
214     serializer_class = serializers.EbookSerializer
215
216
217 @method_decorator(never_cache, name='dispatch')
218 class Preview(LegacyListAPIView):
219     #queryset = Book.objects.filter(preview=True)
220     serializer_class = serializers.BookPreviewSerializer
221
222     def get_queryset(self):
223         qs = Book.objects.filter(preview=True)
224         # FIXME: temporary workaround for a problem with iOS app; see #3954.
225         if 'Darwin' in self.request.META.get('HTTP_USER_AGENT', '') and 'debug' not in self.request.GET:
226             qs = qs.none()
227         return qs
228
229
230 @vary_on_auth  # Because of 'liked'.
231 class FilterBookList(LegacyListAPIView):
232     serializer_class = serializers.FilterBookListSerializer
233
234     def parse_bool(self, s):
235         if s in ('true', 'false'):
236             return s == 'true'
237         else:
238             return None
239
240     def get_queryset(self):
241         key_sep = '$'
242         search_string = self.request.query_params.get('search')
243         is_lektura = self.parse_bool(self.request.query_params.get('lektura'))
244         is_audiobook = self.parse_bool(self.request.query_params.get('audiobook'))
245         preview = self.parse_bool(self.request.query_params.get('preview'))
246         if not Membership.is_active_for(self.request.user):
247             preview = False
248
249         new_api = self.request.query_params.get('new_api')
250         after = self.request.query_params.get('after')
251         count = int(self.request.query_params.get('count', 50))
252         books = order_books(Book.objects.distinct(), new_api)
253         books = books.filter(findable=True)
254         if is_lektura is not None:
255             books = books.filter(has_audience=is_lektura)
256         if is_audiobook is not None:
257             if is_audiobook:
258                 books = books.filter(media__type='mp3')
259             else:
260                 books = books.exclude(media__type='mp3')
261         if preview is not None:
262             books = books.filter(preview=preview)
263         for category in book_tag_categories:
264             category_plural = category + 's'
265             if category_plural in self.request.query_params:
266                 slugs = self.request.query_params[category_plural].split(',')
267                 tags = Tag.objects.filter(category=category, slug__in=slugs)
268                 books = Book.tagged.with_any(tags, books)
269         if (search_string is not None) and len(search_string) < 3:
270             search_string = None
271         if search_string:
272             search_string = re_escape(search_string)
273             books_author = books.filter(cached_author__iregex=r'\m' + search_string)
274             books_title = books.filter(title__iregex=r'\m' + search_string)
275             books_title = books_title.exclude(id__in=list(books_author.values_list('id', flat=True)))
276             if after and (key_sep in after):
277                 which, key = after.split(key_sep, 1)
278                 if which == 'title':
279                     book_lists = [(books_after(books_title, key, new_api), 'title')]
280                 else:  # which == 'author'
281                     book_lists = [(books_after(books_author, key, new_api), 'author'), (books_title, 'title')]
282             else:
283                 book_lists = [(books_author, 'author'), (books_title, 'title')]
284         else:
285             if after and key_sep in after:
286                 which, key = after.split(key_sep, 1)
287                 books = books_after(books, key, new_api)
288             book_lists = [(books, 'book')]
289
290         filtered_books = []
291         for book_list, label in book_lists:
292             for category in book_tag_categories:
293                 book_list = prefetch_relations(book_list, category)
294             remaining_count = count - len(filtered_books)
295             for book in book_list[:remaining_count]:
296                 book.key = '%s%s%s' % (
297                     label, key_sep, book.slug if not new_api else book.full_sort_key())
298                 filtered_books.append(book)
299             if len(filtered_books) == count:
300                 break
301
302         return filtered_books
303
304
305 class EpubView(RetrieveAPIView):
306     queryset = Book.objects.all()
307     lookup_field = 'slug'
308     permission_classes = [IsClubMember]
309
310     @method_decorator(never_cache)
311     def get(self, *args, **kwargs):
312         return HttpResponse(self.get_object().get_media('epub'))
313
314
315 class TagCategoryView(LegacyListAPIView):
316     serializer_class = serializers.TagSerializer
317
318     def get_queryset(self):
319         category = self.kwargs['category']
320         tags = Tag.objects.filter(category=category).exclude(items=None).order_by('slug')
321
322         after = self.request.query_params.get('after')
323         count = self.request.query_params.get('count')
324         if after:
325             tags = tags.filter(slug__gt=after)
326         if count:
327             tags = tags[:count]
328
329         return tags
330
331
332 class TagView(RetrieveAPIView):
333     permission_classes = [DjangoModelPermissionsOrAnonReadOnly]
334     serializer_class = serializers.TagDetailSerializer
335     queryset = Tag.objects.all()
336     
337     def get_object(self):
338         try:
339             return get_object_or_404(
340                 Tag,
341                 category=self.kwargs['category'],
342                 slug=self.kwargs['slug']
343             )
344         except Http404:
345             if self.request.method == 'POST':
346                 return Tag(
347                     category=self.kwargs['category'],
348                     slug=self.kwargs['slug']
349                 )
350             else:
351                 raise
352
353     def post(self, request, **kwargs):
354         data = json.loads(request.POST.get('data'))
355         fields = {
356             "name_pl": "name_pl",
357             "description_pl": "description_pl",
358             "plural": "plural",
359             "is_epoch_specific": "genre_epoch_specific",
360             "collective_noun": "collective_noun",
361             "adjective_feminine_singular": "adjective_feminine_singular",
362             "adjective_nonmasculine_plural": "adjective_nonmasculine_plural",
363             "genitive": "genitive",
364             "collective_noun": "collective_noun",
365             "gazeta_link": "gazeta_link",
366             "culturepl_link": "culturepl_link",
367             "wiki_link_pl": "wiki_link_pl",
368             "photo_attribution": "photo_attribution",
369         }
370         obj = self.get_object()
371         updated = set()
372         for data_field, model_field in fields.items():
373             value = data.get(data_field)
374             if value:
375                 if obj.category == 'author' and model_field == 'name_pl':
376                     obj.sort_key = sortify(value.lower())
377                     updated.add('sort_key')
378                     value = ' '.join(reversed([t.strip() for t in value.split(',', 1)]))
379                 setattr(obj, model_field, value)
380                 updated.add(model_field)
381         if data.get('photo'):
382             response = urlopen(data['photo'])
383             ext = response.headers.get('Content-Type', '').rsplit('/', 1)[-1]
384             obj.photo.save(
385                 "{}.{}".format(self.kwargs['slug'], ext),
386                 ContentFile(response.read()),
387                 save=False,
388             )
389             updated.add('photo')
390
391         if obj.pk:
392             obj.save(update_fields=updated, quick=True)
393         else:
394             obj.save()
395         return Response({})
396
397
398 @vary_on_auth  # Because of 'liked'.
399 class FragmentList(LegacyListAPIView):
400     serializer_class = serializers.FragmentSerializer
401
402     def get_queryset(self):
403         try:
404             tags, ancestors = read_tags(
405                 self.kwargs['tags'],
406                 self.request,
407                 allowed={'author', 'epoch', 'kind', 'genre', 'book', 'theme'}
408             )
409         except ValueError:
410             raise Http404
411         return Fragment.tagged.with_all(tags).filter(book__findable=True).select_related('book')
412
413
414 @vary_on_auth  # Because of 'liked'.
415 class FragmentView(RetrieveAPIView):
416     serializer_class = serializers.FragmentDetailSerializer
417
418     def get_object(self):
419         return get_object_or_404(
420             Fragment,
421             book__slug=self.kwargs['book'],
422             anchor=self.kwargs['anchor']
423         )