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 BookSerializer(LegacyMixin, serializers.ModelSerializer):
31 author = serializers.CharField(source='author_unicode')
32 kind = serializers.CharField(source='kind_unicode')
33 epoch = serializers.CharField(source='epoch_unicode')
34 genre = serializers.CharField(source='genre_unicode')
37 simple_thumb = serializers.FileField(source='cover_api_thumb')
38 href = AbsoluteURLField(view_name='catalogue_api_book', view_args=['slug'])
39 url = AbsoluteURLField()
40 cover_thumb = ThumbnailField('139x193', source='cover')
45 'kind', 'full_sort_key', 'title', 'url', 'cover_color', 'author',
46 'cover', 'epoch', 'href', 'has_audio', 'genre',
47 'simple_thumb', 'slug', 'cover_thumb', 'liked']
48 legacy_non_null_fields = [
49 'kind', 'author', 'epoch', 'genre',
50 'cover', 'simple_thumb', 'cover_thumb']
53 class BookListSerializer(BookSerializer):
54 cover = serializers.CharField()
55 cover_thumb = serializers.CharField()
57 Meta = BookSerializer.Meta
60 class FilterBookListSerializer(BookListSerializer):
61 key = serializers.CharField()
65 fields = BookListSerializer.Meta.fields + ['key']
66 legacy_non_null_fields = BookListSerializer.Meta.legacy_non_null_fields
69 class MediaSerializer(LegacyMixin, serializers.ModelSerializer):
70 url = EmbargoURLField(source='file_url')
74 fields = ['url', 'director', 'type', 'name', 'artist']
75 legacy_non_null_fields = ['director', 'artist']
78 class BookDetailSerializer(LegacyMixin, serializers.ModelSerializer):
79 url = AbsoluteURLField()
81 authors = TagSerializer(many=True)
82 epochs = TagSerializer(many=True)
83 genres = TagSerializer(many=True)
84 kinds = TagSerializer(many=True)
86 fragment_data = serializers.DictField()
87 parent = BookSerializer()
88 children = BookSerializer(many=True)
90 xml = EmbargoURLField(source='xml_url')
91 html = EmbargoURLField(source='html_url')
92 txt = EmbargoURLField(source='txt_url')
93 fb2 = EmbargoURLField(source='fb2_url')
94 epub = EmbargoURLField(source='epub_url')
95 mobi = EmbargoURLField(source='mobi_url')
96 pdf = EmbargoURLField(source='pdf_url')
97 media = MediaSerializer(many=True)
98 cover_thumb = ThumbnailField('139x193', source='cover')
99 simple_thumb = serializers.FileField(source='cover_api_thumb')
105 'epochs', 'genres', 'kinds', 'authors',
106 'fragment_data', 'children', 'parent', 'preview',
107 'epub', 'mobi', 'pdf', 'html', 'txt', 'fb2', 'xml', 'media', 'audio_length',
108 'cover_color', 'simple_cover', 'cover_thumb', 'cover', 'simple_thumb'
110 legacy_non_null_fields = ['html', 'txt', 'fb2', 'epub', 'mobi', 'pdf',
111 'cover', 'simple_cover', 'cover_thumb', 'simple_thumb']
114 class BookPreviewSerializer(BookDetailSerializer):
117 fields = BookDetailSerializer.Meta.fields + ['slug']
118 legacy_non_null_fields = BookDetailSerializer.Meta.legacy_non_null_fields
121 class EbookSerializer(BookListSerializer):
122 txt = EmbargoURLField(source='txt_url')
123 fb2 = EmbargoURLField(source='fb2_url')
124 epub = EmbargoURLField(source='epub_url')
125 mobi = EmbargoURLField(source='mobi_url')
126 pdf = EmbargoURLField(source='pdf_url')
130 fields = ['author', 'href', 'title', 'cover', 'slug'] + Book.ebook_formats
131 legacy_non_null_fields = ['author', 'cover'] + Book.ebook_formats
134 class CollectionListSerializer(serializers.ModelSerializer):
135 url = AbsoluteURLField()
136 href = AbsoluteURLField(view_name='collection-detail', view_args=['slug'])
140 fields = ['url', 'href', 'title']
143 class CollectionSerializer(serializers.ModelSerializer):
144 books = BookSerializer(many=True, source='get_books')
145 url = AbsoluteURLField()
149 fields = ['url', 'books', 'description', 'title']
152 class FragmentSerializer(serializers.ModelSerializer):
153 book = BookSerializer()
154 url = AbsoluteURLField()
155 href = AbsoluteURLField(source='get_api_url')
159 fields = ['book', 'url', 'anchor', 'href']
162 class FragmentDetailSerializer(serializers.ModelSerializer):
163 book = BookSerializer()
164 url = AbsoluteURLField()
165 themes = TagSerializer(many=True)
169 fields = ['book', 'anchor', 'text', 'url', 'themes']