1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from rest_framework import serializers
6 from api.fields import AbsoluteURLField, LegacyMixin, ThumbnailField
7 from catalogue.models import Book, Collection, Tag, BookMedia, Fragment
8 from .fields import BookLiked
11 class TagSerializer(serializers.ModelSerializer):
12 url = AbsoluteURLField()
13 href = AbsoluteURLField(
14 view_name='catalogue_api_tag',
15 view_args=('category', 'slug')
20 fields = ['url', 'href', 'name', 'slug']
23 class TagDetailSerializer(serializers.ModelSerializer):
24 url = AbsoluteURLField()
28 fields = ['name', 'url', 'sort_key', 'description']
31 class BookSerializer(LegacyMixin, serializers.ModelSerializer):
32 author = serializers.CharField(source='author_unicode')
33 kind = serializers.CharField(source='kind_unicode')
34 epoch = serializers.CharField(source='epoch_unicode')
35 genre = serializers.CharField(source='genre_unicode')
38 simple_thumb = serializers.FileField(source='cover_api_thumb')
39 href = AbsoluteURLField(view_name='catalogue_api_book', view_args=['slug'])
40 url = AbsoluteURLField()
41 cover_thumb = ThumbnailField('139x193', source='cover')
46 'kind', 'full_sort_key', 'title', 'url', 'cover_color', 'author',
47 'cover', 'epoch', 'href', 'has_audio', 'genre',
48 'simple_thumb', 'slug', 'cover_thumb', 'liked']
49 legacy_non_null_fields = [
50 'kind', 'author', 'epoch', 'genre',
51 'cover', 'simple_thumb', 'cover_thumb']
54 class BookListSerializer(BookSerializer):
55 cover = serializers.CharField()
56 cover_thumb = serializers.CharField()
58 Meta = BookSerializer.Meta
61 class FilterBookListSerializer(BookListSerializer):
62 key = serializers.CharField()
66 fields = BookListSerializer.Meta.fields + ['key']
67 legacy_non_null_fields = BookListSerializer.Meta.legacy_non_null_fields
70 class MediaSerializer(LegacyMixin, serializers.ModelSerializer):
71 url = serializers.FileField(source='file')
75 fields = ['url', 'director', 'type', 'name', 'artist']
76 legacy_non_null_fields = ['director', 'artist']
79 class BookDetailSerializer(LegacyMixin, serializers.ModelSerializer):
80 url = AbsoluteURLField()
82 authors = TagSerializer(many=True)
83 epochs = TagSerializer(many=True)
84 genres = TagSerializer(many=True)
85 kinds = TagSerializer(many=True)
87 fragment_data = serializers.DictField()
88 parent = BookSerializer()
89 children = BookSerializer(many=True)
91 xml = AbsoluteURLField(source='xml_url')
92 html = AbsoluteURLField(source='html_url')
93 txt = AbsoluteURLField(source='txt_url')
94 fb2 = AbsoluteURLField(source='fb2_url')
95 epub = AbsoluteURLField(source='epub_url')
96 mobi = AbsoluteURLField(source='mobi_url')
97 pdf = AbsoluteURLField(source='pdf_url')
98 media = MediaSerializer(many=True)
99 cover_thumb = ThumbnailField('139x193', source='cover')
100 simple_thumb = serializers.FileField(source='cover_api_thumb')
106 'epochs', 'genres', 'kinds', 'authors',
107 'fragment_data', 'children', 'parent', 'preview',
108 'epub', 'mobi', 'pdf', 'html', 'txt', 'fb2', 'xml', 'media', 'audio_length',
109 'cover_color', 'simple_cover', 'cover_thumb', 'cover', 'simple_thumb'
111 legacy_non_null_fields = ['html', 'txt', 'fb2', 'epub', 'mobi', 'pdf',
112 'cover', 'simple_cover', 'cover_thumb', 'simple_thumb']
115 class BookPreviewSerializer(BookDetailSerializer):
118 fields = BookDetailSerializer.Meta.fields + ['slug']
119 legacy_non_null_fields = BookDetailSerializer.Meta.legacy_non_null_fields
122 class EbookSerializer(BookListSerializer):
123 txt = AbsoluteURLField(source='txt_url')
124 fb2 = AbsoluteURLField(source='fb2_url')
125 epub = AbsoluteURLField(source='epub_url')
126 mobi = AbsoluteURLField(source='mobi_url')
127 pdf = AbsoluteURLField(source='pdf_url')
131 fields = ['author', 'href', 'title', 'cover', 'slug'] + Book.ebook_formats
132 legacy_non_null_fields = ['author', 'cover'] + Book.ebook_formats
135 class CollectionListSerializer(serializers.ModelSerializer):
136 url = AbsoluteURLField()
137 href = AbsoluteURLField(view_name='collection-detail', view_args=['slug'])
141 fields = ['url', 'href', 'title']
144 class CollectionSerializer(serializers.ModelSerializer):
145 books = BookSerializer(many=True, source='get_books')
146 url = AbsoluteURLField()
150 fields = ['url', 'books', 'description', 'title']
153 class FragmentSerializer(serializers.ModelSerializer):
154 book = BookSerializer()
155 url = AbsoluteURLField()
156 href = AbsoluteURLField(source='get_api_url')
160 fields = ['book', 'url', 'anchor', 'href']
163 class FragmentDetailSerializer(serializers.ModelSerializer):
164 book = BookSerializer()
165 url = AbsoluteURLField()
166 themes = TagSerializer(many=True)
170 fields = ['book', 'anchor', 'text', 'url', 'themes']