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.
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
30 book_tag_categories = ['author', 'epoch', 'kind', 'genre']
33 class LegacyListAPIView(ListAPIView):
34 pagination_class = None
37 class CreateOnPutMixin:
39 Creates a new model instance when PUTting a nonexistent resource.
43 return super().get_object()
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]
54 class CollectionList(LegacyListAPIView):
55 queryset = Collection.objects.filter(listed=True)
56 serializer_class = serializers.CollectionListSerializer
59 @vary_on_auth # Because of 'liked'.
60 class CollectionDetail(CreateOnPutMixin, RetrieveUpdateAPIView):
61 permission_classes = [DjangoModelPermissionsOrAnonReadOnly]
62 queryset = Collection.objects.all()
64 serializer_class = serializers.CollectionSerializer
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
73 def get(self, request, filename=None, **kwargs):
74 if filename and not kwargs.get('tags') and 'count' not in request.query_params:
76 with open(os.path.join(settings.MEDIA_ROOT, 'api', '%s.%s' % (filename, request.accepted_renderer.format)), 'rb') as f:
78 return HttpResponse(content, content_type=request.accepted_media_type)
81 return super().get(request, filename=filename, **kwargs)
83 def get_queryset(self):
85 tags, ancestors = read_tags(
86 self.kwargs.get('tags', ''), self.request,
87 allowed=('author', 'epoch', 'kind', 'genre')
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'))
102 if self.kwargs.get('top_level'):
103 books = Book.tagged_top_level(tags)
108 books = Book.tagged.with_all(tags)
110 books = Book.objects.all()
111 books = books.filter(findable=True)
112 books = order_books(books, new_api)
114 if not Membership.is_active_for(self.request.user):
115 books = books.exclude(preview=True)
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')
129 books = books_after(books, after, new_api)
131 prefetch_relations(books, 'author')
132 prefetch_relations(books, 'genre')
133 prefetch_relations(books, 'kind')
134 prefetch_relations(books, 'epoch')
137 books = books[:count]
141 def post(self, request, **kwargs):
142 if kwargs.get('audiobooks'):
143 return self.post_audiobook(request, **kwargs)
145 return self.post_book(request, **kwargs)
147 def post_book(self, request, **kwargs):
148 data = json.loads(request.POST.get('data'))
149 form = BookImportForm(data)
152 return Response({}, status=status.HTTP_201_CREATED)
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', '')
164 project_description = request.POST.get('project_description', '')
165 project_icon = request.POST.get('project_icon', '')
167 _rest, slug = request.POST['book'].rstrip('/').rsplit('/', 1)
168 book = Book.objects.get(slug=slug)
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)
176 bm.part_name = part_name
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)
183 return Response({}, status=status.HTTP_201_CREATED)
186 class BookList2(ListAPIView):
187 permission_classes = [DjangoModelPermissionsOrAnonReadOnly]
188 queryset = Book.objects.none() # Required for DjangoModelPermissions
189 serializer_class = serializers.BookSerializer2
191 def get_queryset(self):
192 books = Book.objects.all()
193 books = books.filter(findable=True)
194 books = order_books(books, True)
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
206 class BookDetail2(RetrieveAPIView):
207 queryset = Book.objects.all()
208 lookup_field = 'slug'
209 serializer_class = serializers.BookSerializer2
212 @vary_on_auth # Because of embargo links.
213 class EbookList(BookList):
214 serializer_class = serializers.EbookSerializer
217 @method_decorator(never_cache, name='dispatch')
218 class Preview(LegacyListAPIView):
219 #queryset = Book.objects.filter(preview=True)
220 serializer_class = serializers.BookPreviewSerializer
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:
230 @vary_on_auth # Because of 'liked'.
231 class FilterBookList(LegacyListAPIView):
232 serializer_class = serializers.FilterBookListSerializer
234 def parse_bool(self, s):
235 if s in ('true', 'false'):
240 def get_queryset(self):
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):
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:
258 books = books.filter(media__type='mp3')
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:
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)
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')]
283 book_lists = [(books_author, 'author'), (books_title, 'title')]
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')]
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:
302 return filtered_books
305 class EpubView(RetrieveAPIView):
306 queryset = Book.objects.all()
307 lookup_field = 'slug'
308 permission_classes = [IsClubMember]
310 @method_decorator(never_cache)
311 def get(self, *args, **kwargs):
312 return HttpResponse(self.get_object().get_media('epub'))
315 class TagCategoryView(LegacyListAPIView):
316 serializer_class = serializers.TagSerializer
318 def get_queryset(self):
319 category = self.kwargs['category']
320 tags = Tag.objects.filter(category=category).exclude(items=None).order_by('slug')
322 after = self.request.query_params.get('after')
323 count = self.request.query_params.get('count')
325 tags = tags.filter(slug__gt=after)
332 class TagView(RetrieveAPIView):
333 permission_classes = [DjangoModelPermissionsOrAnonReadOnly]
334 serializer_class = serializers.TagDetailSerializer
335 queryset = Tag.objects.all()
337 def get_object(self):
339 return get_object_or_404(
341 category=self.kwargs['category'],
342 slug=self.kwargs['slug']
345 if self.request.method == 'POST':
347 category=self.kwargs['category'],
348 slug=self.kwargs['slug']
353 def post(self, request, **kwargs):
354 data = json.loads(request.POST.get('data'))
356 "name_pl": "name_pl",
357 "description_pl": "description_pl",
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",
370 obj = self.get_object()
372 for data_field, model_field in fields.items():
373 value = data.get(data_field)
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]
385 "{}.{}".format(self.kwargs['slug'], ext),
386 ContentFile(response.read()),
392 obj.save(update_fields=updated, quick=True)
398 @vary_on_auth # Because of 'liked'.
399 class FragmentList(LegacyListAPIView):
400 serializer_class = serializers.FragmentSerializer
402 def get_queryset(self):
404 tags, ancestors = read_tags(
407 allowed={'author', 'epoch', 'kind', 'genre', 'book', 'theme'}
411 return Fragment.tagged.with_all(tags).filter(book__findable=True).select_related('book')
414 @vary_on_auth # Because of 'liked'.
415 class FragmentView(RetrieveAPIView):
416 serializer_class = serializers.FragmentDetailSerializer
418 def get_object(self):
419 return get_object_or_404(
421 book__slug=self.kwargs['book'],
422 anchor=self.kwargs['anchor']