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.
4 from rest_framework import serializers
5 from api.fields import AbsoluteURLField, LegacyMixin, ThumbnailField
6 from catalogue.models import Book, Collection, Tag, BookMedia, Fragment
7 from .fields import BookLiked, EmbargoURLField
10 class TagSerializer(serializers.ModelSerializer):
11 url = AbsoluteURLField()
12 href = AbsoluteURLField(
13 view_name='catalogue_api_tag',
14 view_args=('category', 'slug')
19 fields = ['url', 'href', 'name', 'slug']
22 class TagDetailSerializer(serializers.ModelSerializer):
23 url = AbsoluteURLField()
28 'name', 'url', 'sort_key',
31 'plural', 'genre_epoch_specific',
32 'adjective_feminine_singular', 'adjective_nonmasculine_plural',
33 'genitive', 'collective_noun',
37 'plural', 'genre_epoch_specific',
38 'adjective_feminine_singular', 'adjective_nonmasculine_plural',
39 'genitive', 'collective_noun',
43 class AuthorItemSerializer(serializers.ModelSerializer):
44 url = AbsoluteURLField()
45 href = AbsoluteURLField(
46 view_name='catalogue_api_author',
56 class AuthorSerializer(AuthorItemSerializer):
57 photo_thumb = ThumbnailField('139x193', source='photo')
62 'url', 'href', 'name', 'slug', 'sort_key', 'description',
63 'genitive', 'photo', 'photo_thumb', 'photo_attribution',
66 class EpochItemSerializer(serializers.ModelSerializer):
67 url = AbsoluteURLField()
68 href = AbsoluteURLField(
69 view_name='catalogue_api_epoch',
74 fields = ['url', 'href', 'name']
76 class EpochSerializer(EpochItemSerializer):
80 'url', 'href', 'name', 'slug', 'sort_key', 'description',
81 'adjective_feminine_singular', 'adjective_nonmasculine_plural',
84 class GenreItemSerializer(serializers.ModelSerializer):
85 url = AbsoluteURLField()
86 href = AbsoluteURLField(
87 view_name='catalogue_api_genre',
92 fields = ['url', 'href', 'name']
94 class GenreSerializer(GenreItemSerializer):
98 'url', 'href', 'name', 'slug', 'sort_key', 'description',
99 'plural', 'genre_epoch_specific',
102 class KindItemSerializer(serializers.ModelSerializer):
103 url = AbsoluteURLField()
104 href = AbsoluteURLField(
105 view_name='catalogue_api_kind',
110 fields = ['url', 'href', 'name']
112 class KindSerializer(KindItemSerializer):
116 'url', 'href', 'name', 'slug', 'sort_key', 'description',
121 class TranslatorSerializer(serializers.Serializer):
122 name = serializers.CharField(source='*')
125 class BookSerializer2(serializers.ModelSerializer):
126 url = AbsoluteURLField()
127 href = AbsoluteURLField(view_name='catalogue_api_book', view_args=['slug'])
128 xml = EmbargoURLField(source='xml_url')
129 html = EmbargoURLField(source='html_url')
130 txt = EmbargoURLField(source='txt_url')
131 fb2 = EmbargoURLField(source='fb2_url')
132 epub = EmbargoURLField(source='epub_url')
133 mobi = EmbargoURLField(source='mobi_url')
134 pdf = EmbargoURLField(source='pdf_url')
136 authors = AuthorItemSerializer(many=True)
137 translators = AuthorItemSerializer(many=True)
138 epochs = EpochItemSerializer(many=True)
139 genres = GenreItemSerializer(many=True)
140 kinds = KindItemSerializer(many=True)
141 parent = serializers.HyperlinkedRelatedField(
143 view_name='catalogue_api_book',
150 'slug', 'title', 'full_sort_key',
151 'href', 'url', 'language',
152 'authors', 'translators',
153 'epochs', 'genres', 'kinds',
156 'epub', 'mobi', 'pdf', 'html', 'txt', 'fb2', 'xml',
157 'cover_thumb', 'cover',
158 'isbn_pdf', 'isbn_epub', 'isbn_mobi',
161 class BookSerializer(LegacyMixin, serializers.ModelSerializer):
162 author = serializers.CharField(source='author_unicode')
163 kind = serializers.CharField(source='kind_unicode')
164 epoch = serializers.CharField(source='epoch_unicode')
165 genre = serializers.CharField(source='genre_unicode')
168 simple_thumb = serializers.FileField(source='cover_api_thumb')
169 href = AbsoluteURLField(view_name='catalogue_api_book', view_args=['slug'])
170 url = AbsoluteURLField()
171 cover_thumb = ThumbnailField('139x193', source='cover')
176 'kind', 'full_sort_key', 'title', 'url', 'cover_color', 'author',
177 'cover', 'epoch', 'href', 'has_audio', 'genre',
178 'simple_thumb', 'slug', 'cover_thumb', 'liked']
179 legacy_non_null_fields = [
180 'kind', 'author', 'epoch', 'genre',
181 'cover', 'simple_thumb', 'cover_thumb']
184 class BookListSerializer(BookSerializer):
185 cover = serializers.CharField()
186 cover_thumb = serializers.CharField()
188 Meta = BookSerializer.Meta
191 class FilterBookListSerializer(BookListSerializer):
192 key = serializers.CharField()
196 fields = BookListSerializer.Meta.fields + ['key']
197 legacy_non_null_fields = BookListSerializer.Meta.legacy_non_null_fields
200 class MediaSerializer(LegacyMixin, serializers.ModelSerializer):
201 url = EmbargoURLField(source='file_url')
205 fields = ['url', 'director', 'type', 'name', 'artist']
206 legacy_non_null_fields = ['director', 'artist']
209 class BookDetailSerializer(LegacyMixin, serializers.ModelSerializer):
210 url = AbsoluteURLField()
212 authors = TagSerializer(many=True)
213 translators = TranslatorSerializer(many=True)
214 epochs = TagSerializer(many=True)
215 genres = TagSerializer(many=True)
216 kinds = TagSerializer(many=True)
218 fragment_data = serializers.DictField()
219 parent = BookSerializer()
220 children = BookSerializer(many=True)
222 xml = EmbargoURLField(source='xml_url')
223 html = EmbargoURLField(source='html_url')
224 txt = EmbargoURLField(source='txt_url')
225 fb2 = EmbargoURLField(source='fb2_url')
226 epub = EmbargoURLField(source='epub_url')
227 mobi = EmbargoURLField(source='mobi_url')
228 pdf = EmbargoURLField(source='pdf_url')
229 media = MediaSerializer(many=True)
230 cover_thumb = ThumbnailField('139x193', source='cover')
231 simple_thumb = serializers.FileField(source='cover_api_thumb')
236 'title', 'url', 'language',
237 'epochs', 'genres', 'kinds', 'authors', 'translators',
238 'fragment_data', 'children', 'parent', 'preview',
239 'epub', 'mobi', 'pdf', 'html', 'txt', 'fb2', 'xml', 'media', 'audio_length',
240 'cover_color', 'simple_cover', 'cover_thumb', 'cover', 'simple_thumb',
241 'isbn_pdf', 'isbn_epub', 'isbn_mobi',
243 legacy_non_null_fields = ['html', 'txt', 'fb2', 'epub', 'mobi', 'pdf',
244 'cover', 'simple_cover', 'cover_thumb', 'simple_thumb']
247 class BookPreviewSerializer(BookDetailSerializer):
250 fields = BookDetailSerializer.Meta.fields + ['slug']
251 legacy_non_null_fields = BookDetailSerializer.Meta.legacy_non_null_fields
254 class EbookSerializer(BookListSerializer):
255 txt = EmbargoURLField(source='txt_url')
256 fb2 = EmbargoURLField(source='fb2_url')
257 epub = EmbargoURLField(source='epub_url')
258 mobi = EmbargoURLField(source='mobi_url')
259 pdf = EmbargoURLField(source='pdf_url')
263 fields = ['author', 'href', 'title', 'cover', 'slug'] + Book.ebook_formats
264 legacy_non_null_fields = ['author', 'cover'] + Book.ebook_formats
267 class CollectionListSerializer(serializers.ModelSerializer):
268 url = AbsoluteURLField()
269 href = AbsoluteURLField(view_name='collection-detail', view_args=['slug'])
273 fields = ['url', 'href', 'title']
276 class CollectionSerializer(serializers.ModelSerializer):
277 books = BookSerializer(many=True, source='get_books', read_only=True)
278 authors = TagSerializer(many=True, read_only=True)
279 book_slugs = serializers.CharField(write_only=True, required=False)
280 author_slugs = serializers.CharField(write_only=True, required=False)
281 url = AbsoluteURLField()
286 'url', 'books', 'description', 'title',
287 'book_slugs', 'authors', 'author_slugs'
290 def update(self, instance, validated_data):
291 instance = super().update(instance, validated_data)
292 author_slugs = validated_data.get('author_slugs', '').strip().split()
294 authors = Tag.objects.filter(
296 slug__in=author_slugs
300 instance.authors.set(authors)
304 class FragmentSerializer(serializers.ModelSerializer):
305 book = BookSerializer()
306 url = AbsoluteURLField()
307 href = AbsoluteURLField(source='get_api_url')
311 fields = ['book', 'url', 'anchor', 'href']
314 class FragmentDetailSerializer(serializers.ModelSerializer):
315 book = BookSerializer()
316 url = AbsoluteURLField()
317 themes = TagSerializer(many=True)
321 fields = ['book', 'anchor', 'text', 'url', 'themes']