X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/bf99d8859b9b576381cde347bd8abd4544f09d38..f546d1dd07dd4d86e1dcde26a1b02f0ffe783f7c:/src/catalogue/api/serializers.py?ds=inline diff --git a/src/catalogue/api/serializers.py b/src/catalogue/api/serializers.py index e8f04ca70..9baba3461 100644 --- a/src/catalogue/api/serializers.py +++ b/src/catalogue/api/serializers.py @@ -1,7 +1,10 @@ +# This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. +# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. +# from rest_framework import serializers -from api.fields import AbsoluteURLField, LegacyMixin +from api.fields import AbsoluteURLField, LegacyMixin, ThumbnailField from catalogue.models import Book, Collection, Tag, BookMedia, Fragment -from .fields import BookLiked, ThumbnailField +from .fields import BookLiked, EmbargoURLField class TagSerializer(serializers.ModelSerializer): @@ -21,19 +24,36 @@ class TagDetailSerializer(serializers.ModelSerializer): class Meta: model = Tag - fields = ['name', 'url', 'sort_key', 'description'] + fields = [ + 'name', 'url', 'sort_key', + 'description', + 'description_pl', + 'plural', 'genre_epoch_specific', + 'adjective_feminine_singular', 'adjective_nonmasculine_plural', + 'genitive', 'collective_noun', + ] + write_only_fields = [ + 'description_pl', + 'plural', 'genre_epoch_specific', + 'adjective_feminine_singular', 'adjective_nonmasculine_plural', + 'genitive', 'collective_noun', + ] + +class TranslatorSerializer(serializers.Serializer): + name = serializers.CharField(source='*') -class BaseBookSerializer(LegacyMixin, serializers.ModelSerializer): + +class BookSerializer(LegacyMixin, serializers.ModelSerializer): author = serializers.CharField(source='author_unicode') kind = serializers.CharField(source='kind_unicode') epoch = serializers.CharField(source='epoch_unicode') genre = serializers.CharField(source='genre_unicode') + liked = BookLiked() simple_thumb = serializers.FileField(source='cover_api_thumb') href = AbsoluteURLField(view_name='catalogue_api_book', view_args=['slug']) url = AbsoluteURLField() - cover = serializers.CharField() cover_thumb = ThumbnailField('139x193', source='cover') class Meta: @@ -41,24 +61,30 @@ class BaseBookSerializer(LegacyMixin, serializers.ModelSerializer): fields = [ 'kind', 'full_sort_key', 'title', 'url', 'cover_color', 'author', 'cover', 'epoch', 'href', 'has_audio', 'genre', - 'simple_thumb', 'slug', 'cover_thumb'] + 'simple_thumb', 'slug', 'cover_thumb', 'liked'] legacy_non_null_fields = [ 'kind', 'author', 'epoch', 'genre', 'cover', 'simple_thumb', 'cover_thumb'] -class BookSerializer(BaseBookSerializer): - liked = BookLiked() - cover = serializers.FileField() +class BookListSerializer(BookSerializer): + cover = serializers.CharField() + cover_thumb = serializers.CharField() + + Meta = BookSerializer.Meta + + +class FilterBookListSerializer(BookListSerializer): + key = serializers.CharField() class Meta: model = Book - fields = BaseBookSerializer.Meta.fields + ['liked'] - legacy_non_null_fields = BaseBookSerializer.Meta.legacy_non_null_fields + fields = BookListSerializer.Meta.fields + ['key'] + legacy_non_null_fields = BookListSerializer.Meta.legacy_non_null_fields class MediaSerializer(LegacyMixin, serializers.ModelSerializer): - url = serializers.FileField(source='file') + url = EmbargoURLField(source='file_url') class Meta: model = BookMedia @@ -70,6 +96,7 @@ class BookDetailSerializer(LegacyMixin, serializers.ModelSerializer): url = AbsoluteURLField() authors = TagSerializer(many=True) + translators = TranslatorSerializer(many=True) epochs = TagSerializer(many=True) genres = TagSerializer(many=True) kinds = TagSerializer(many=True) @@ -78,13 +105,13 @@ class BookDetailSerializer(LegacyMixin, serializers.ModelSerializer): parent = BookSerializer() children = BookSerializer(many=True) - xml = AbsoluteURLField(source='xml_url') - html = AbsoluteURLField(source='html_url') - txt = AbsoluteURLField(source='txt_url') - fb2 = AbsoluteURLField(source='fb2_url') - epub = AbsoluteURLField(source='epub_url') - mobi = AbsoluteURLField(source='mobi_url') - pdf = AbsoluteURLField(source='pdf_url') + xml = EmbargoURLField(source='xml_url') + html = EmbargoURLField(source='html_url') + txt = EmbargoURLField(source='txt_url') + fb2 = EmbargoURLField(source='fb2_url') + epub = EmbargoURLField(source='epub_url') + mobi = EmbargoURLField(source='mobi_url') + pdf = EmbargoURLField(source='pdf_url') media = MediaSerializer(many=True) cover_thumb = ThumbnailField('139x193', source='cover') simple_thumb = serializers.FileField(source='cover_api_thumb') @@ -92,11 +119,12 @@ class BookDetailSerializer(LegacyMixin, serializers.ModelSerializer): class Meta: model = Book fields = [ - 'title', 'url', - 'epochs', 'genres', 'kinds', 'authors', + 'title', 'url', 'language', + 'epochs', 'genres', 'kinds', 'authors', 'translators', 'fragment_data', 'children', 'parent', 'preview', 'epub', 'mobi', 'pdf', 'html', 'txt', 'fb2', 'xml', 'media', 'audio_length', - 'cover_color', 'simple_cover', 'cover_thumb', 'cover', 'simple_thumb' + 'cover_color', 'simple_cover', 'cover_thumb', 'cover', 'simple_thumb', + 'isbn_pdf', 'isbn_epub', 'isbn_mobi', ] legacy_non_null_fields = ['html', 'txt', 'fb2', 'epub', 'mobi', 'pdf', 'cover', 'simple_cover', 'cover_thumb', 'simple_thumb'] @@ -109,7 +137,13 @@ class BookPreviewSerializer(BookDetailSerializer): legacy_non_null_fields = BookDetailSerializer.Meta.legacy_non_null_fields -class EbookSerializer(BookSerializer): +class EbookSerializer(BookListSerializer): + txt = EmbargoURLField(source='txt_url') + fb2 = EmbargoURLField(source='fb2_url') + epub = EmbargoURLField(source='epub_url') + mobi = EmbargoURLField(source='mobi_url') + pdf = EmbargoURLField(source='pdf_url') + class Meta: model = Book fields = ['author', 'href', 'title', 'cover', 'slug'] + Book.ebook_formats @@ -126,12 +160,31 @@ class CollectionListSerializer(serializers.ModelSerializer): class CollectionSerializer(serializers.ModelSerializer): - books = BookSerializer(many=True, source='get_books') + books = BookSerializer(many=True, source='get_books', read_only=True) + authors = TagSerializer(many=True, read_only=True) + book_slugs = serializers.CharField(write_only=True, required=False) + author_slugs = serializers.CharField(write_only=True, required=False) url = AbsoluteURLField() class Meta: model = Collection - fields = ['url', 'books', 'description', 'title'] + fields = [ + 'url', 'books', 'description', 'title', + 'book_slugs', 'authors', 'author_slugs' + ] + + def update(self, instance, validated_data): + instance = super().update(instance, validated_data) + author_slugs = validated_data.get('author_slugs', '').strip().split() + if author_slugs: + authors = Tag.objects.filter( + category='author', + slug__in=author_slugs + ) + else: + authors = [] + instance.authors.set(authors) + return instance class FragmentSerializer(serializers.ModelSerializer):