1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. 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()
27 fields = ['name', 'url', 'sort_key', 'description']
30 class TranslatorSerializer(serializers.Serializer):
31 name = serializers.CharField(source='*')
34 class BookSerializer(LegacyMixin, serializers.ModelSerializer):
35 author = serializers.CharField(source='author_unicode')
36 kind = serializers.CharField(source='kind_unicode')
37 epoch = serializers.CharField(source='epoch_unicode')
38 genre = serializers.CharField(source='genre_unicode')
41 simple_thumb = serializers.FileField(source='cover_api_thumb')
42 href = AbsoluteURLField(view_name='catalogue_api_book', view_args=['slug'])
43 url = AbsoluteURLField()
44 cover_thumb = ThumbnailField('139x193', source='cover')
49 'kind', 'full_sort_key', 'title', 'url', 'cover_color', 'author',
50 'cover', 'epoch', 'href', 'has_audio', 'genre',
51 'simple_thumb', 'slug', 'cover_thumb', 'liked']
52 legacy_non_null_fields = [
53 'kind', 'author', 'epoch', 'genre',
54 'cover', 'simple_thumb', 'cover_thumb']
57 class BookListSerializer(BookSerializer):
58 cover = serializers.CharField()
59 cover_thumb = serializers.CharField()
61 Meta = BookSerializer.Meta
64 class FilterBookListSerializer(BookListSerializer):
65 key = serializers.CharField()
69 fields = BookListSerializer.Meta.fields + ['key']
70 legacy_non_null_fields = BookListSerializer.Meta.legacy_non_null_fields
73 class MediaSerializer(LegacyMixin, serializers.ModelSerializer):
74 url = EmbargoURLField(source='file_url')
78 fields = ['url', 'director', 'type', 'name', 'artist']
79 legacy_non_null_fields = ['director', 'artist']
82 class BookDetailSerializer(LegacyMixin, serializers.ModelSerializer):
83 url = AbsoluteURLField()
85 authors = TagSerializer(many=True)
86 translators = TranslatorSerializer(many=True)
87 epochs = TagSerializer(many=True)
88 genres = TagSerializer(many=True)
89 kinds = TagSerializer(many=True)
91 fragment_data = serializers.DictField()
92 parent = BookSerializer()
93 children = BookSerializer(many=True)
95 xml = EmbargoURLField(source='xml_url')
96 html = EmbargoURLField(source='html_url')
97 txt = EmbargoURLField(source='txt_url')
98 fb2 = EmbargoURLField(source='fb2_url')
99 epub = EmbargoURLField(source='epub_url')
100 mobi = EmbargoURLField(source='mobi_url')
101 pdf = EmbargoURLField(source='pdf_url')
102 media = MediaSerializer(many=True)
103 cover_thumb = ThumbnailField('139x193', source='cover')
104 simple_thumb = serializers.FileField(source='cover_api_thumb')
110 'epochs', 'genres', 'kinds', 'authors', 'translators',
111 'fragment_data', 'children', 'parent', 'preview',
112 'epub', 'mobi', 'pdf', 'html', 'txt', 'fb2', 'xml', 'media', 'audio_length',
113 'cover_color', 'simple_cover', 'cover_thumb', 'cover', 'simple_thumb'
115 legacy_non_null_fields = ['html', 'txt', 'fb2', 'epub', 'mobi', 'pdf',
116 'cover', 'simple_cover', 'cover_thumb', 'simple_thumb']
119 class BookPreviewSerializer(BookDetailSerializer):
122 fields = BookDetailSerializer.Meta.fields + ['slug']
123 legacy_non_null_fields = BookDetailSerializer.Meta.legacy_non_null_fields
126 class EbookSerializer(BookListSerializer):
127 txt = EmbargoURLField(source='txt_url')
128 fb2 = EmbargoURLField(source='fb2_url')
129 epub = EmbargoURLField(source='epub_url')
130 mobi = EmbargoURLField(source='mobi_url')
131 pdf = EmbargoURLField(source='pdf_url')
135 fields = ['author', 'href', 'title', 'cover', 'slug'] + Book.ebook_formats
136 legacy_non_null_fields = ['author', 'cover'] + Book.ebook_formats
139 class CollectionListSerializer(serializers.ModelSerializer):
140 url = AbsoluteURLField()
141 href = AbsoluteURLField(view_name='collection-detail', view_args=['slug'])
145 fields = ['url', 'href', 'title']
148 class CollectionSerializer(serializers.ModelSerializer):
149 books = BookSerializer(many=True, source='get_books')
150 url = AbsoluteURLField()
154 fields = ['url', 'books', 'description', 'title']
157 class FragmentSerializer(serializers.ModelSerializer):
158 book = BookSerializer()
159 url = AbsoluteURLField()
160 href = AbsoluteURLField(source='get_api_url')
164 fields = ['book', 'url', 'anchor', 'href']
167 class FragmentDetailSerializer(serializers.ModelSerializer):
168 book = BookSerializer()
169 url = AbsoluteURLField()
170 themes = TagSerializer(many=True)
174 fields = ['book', 'anchor', 'text', 'url', 'themes']