1 from rest_framework import serializers
2 from api.fields import AbsoluteURLField, LegacyMixin
3 from catalogue.models import Book, Collection, Tag, BookMedia, Fragment
4 from .fields import BookLiked, ThumbnailField
7 class TagSerializer(serializers.ModelSerializer):
8 url = AbsoluteURLField()
9 href = AbsoluteURLField(
10 view_name='catalogue_api_tag',
11 view_args=('category', 'slug')
16 fields = ['url', 'href', 'name', 'slug']
19 class TagDetailSerializer(serializers.ModelSerializer):
20 url = AbsoluteURLField()
24 fields = ['name', 'url', 'sort_key', 'description']
27 class BaseBookSerializer(LegacyMixin, serializers.ModelSerializer):
28 author = serializers.CharField(source='author_unicode')
29 kind = serializers.CharField(source='kind_unicode')
30 epoch = serializers.CharField(source='epoch_unicode')
31 genre = serializers.CharField(source='genre_unicode')
33 simple_thumb = serializers.FileField(source='cover_api_thumb')
34 href = AbsoluteURLField(view_name='catalogue_api_book', view_args=['slug'])
35 url = AbsoluteURLField()
36 cover = serializers.CharField()
37 cover_thumb = ThumbnailField('139x193', source='cover')
42 'kind', 'full_sort_key', 'title', 'url', 'cover_color', 'author',
43 'cover', 'epoch', 'href', 'has_audio', 'genre',
44 'simple_thumb', 'slug', 'cover_thumb']
45 legacy_non_null_fields = [
46 'kind', 'author', 'epoch', 'genre',
47 'cover', 'simple_thumb', 'cover_thumb']
50 class BookSerializer(BaseBookSerializer):
52 cover = serializers.FileField()
56 fields = BaseBookSerializer.Meta.fields + ['liked']
57 legacy_non_null_fields = BaseBookSerializer.Meta.legacy_non_null_fields
60 class MediaSerializer(LegacyMixin, serializers.ModelSerializer):
61 url = serializers.FileField(source='file')
65 fields = ['url', 'director', 'type', 'name', 'artist']
66 legacy_non_null_fields = ['director', 'artist']
69 class BookDetailSerializer(LegacyMixin, serializers.ModelSerializer):
70 url = AbsoluteURLField()
72 authors = TagSerializer(many=True)
73 epochs = TagSerializer(many=True)
74 genres = TagSerializer(many=True)
75 kinds = TagSerializer(many=True)
77 fragment_data = serializers.DictField()
78 parent = BookSerializer()
79 children = BookSerializer(many=True)
81 xml = AbsoluteURLField(source='xml_url')
82 html = AbsoluteURLField(source='html_url')
83 txt = AbsoluteURLField(source='txt_url')
84 fb2 = AbsoluteURLField(source='fb2_url')
85 epub = AbsoluteURLField(source='epub_url')
86 mobi = AbsoluteURLField(source='mobi_url')
87 pdf = AbsoluteURLField(source='pdf_url')
88 media = MediaSerializer(many=True)
89 cover_thumb = ThumbnailField('139x193', source='cover')
90 simple_thumb = serializers.FileField(source='cover_api_thumb')
96 'epochs', 'genres', 'kinds', 'authors',
97 'fragment_data', 'children', 'parent', 'preview',
98 'epub', 'mobi', 'pdf', 'html', 'txt', 'fb2', 'xml', 'media', 'audio_length',
99 'cover_color', 'simple_cover', 'cover_thumb', 'cover', 'simple_thumb'
101 legacy_non_null_fields = ['html', 'txt', 'fb2', 'epub', 'mobi', 'pdf',
102 'cover', 'simple_cover', 'cover_thumb', 'simple_thumb']
105 class BookPreviewSerializer(BookDetailSerializer):
108 fields = BookDetailSerializer.Meta.fields + ['slug']
109 legacy_non_null_fields = BookDetailSerializer.Meta.legacy_non_null_fields
112 class EbookSerializer(BookSerializer):
115 fields = ['author', 'href', 'title', 'cover', 'slug'] + Book.ebook_formats
116 legacy_non_null_fields = ['author', 'cover'] + Book.ebook_formats
119 class CollectionListSerializer(serializers.ModelSerializer):
120 url = AbsoluteURLField()
121 href = AbsoluteURLField(view_name='collection-detail', view_args=['slug'])
125 fields = ['url', 'href', 'title']
128 class CollectionSerializer(serializers.ModelSerializer):
129 books = BookSerializer(many=True, source='get_books')
130 url = AbsoluteURLField()
134 fields = ['url', 'books', 'description', 'title']
137 class FragmentSerializer(serializers.ModelSerializer):
138 book = BookSerializer()
139 url = AbsoluteURLField()
140 href = AbsoluteURLField(source='get_api_url')
144 fields = ['book', 'url', 'anchor', 'href']
147 class FragmentDetailSerializer(serializers.ModelSerializer):
148 book = BookSerializer()
149 url = AbsoluteURLField()
150 themes = TagSerializer(many=True)
154 fields = ['book', 'anchor', 'text', 'url', 'themes']