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 BookSerializer(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')
34 simple_thumb = serializers.FileField(source='cover_api_thumb')
35 href = AbsoluteURLField(view_name='catalogue_api_book', view_args=['slug'])
36 url = AbsoluteURLField()
37 cover = serializers.FileField()
38 cover_thumb = ThumbnailField('139x193', source='cover')
43 'kind', 'full_sort_key', 'title', 'url', 'cover_color', 'author',
44 'cover', 'epoch', 'href', 'has_audio', 'genre',
45 'simple_thumb', 'slug', 'cover_thumb', 'liked']
46 legacy_non_null_fields = [
47 'kind', 'author', 'epoch', 'genre',
48 'cover', 'simple_thumb', 'cover_thumb']
51 class BookListSerializer(BookSerializer):
52 cover = serializers.CharField()
53 cover_thumb = serializers.CharField()
55 Meta = BookSerializer.Meta
58 class MediaSerializer(LegacyMixin, serializers.ModelSerializer):
59 url = serializers.FileField(source='file')
63 fields = ['url', 'director', 'type', 'name', 'artist']
64 legacy_non_null_fields = ['director', 'artist']
67 class BookDetailSerializer(LegacyMixin, serializers.ModelSerializer):
68 url = AbsoluteURLField()
70 authors = TagSerializer(many=True)
71 epochs = TagSerializer(many=True)
72 genres = TagSerializer(many=True)
73 kinds = TagSerializer(many=True)
75 fragment_data = serializers.DictField()
76 parent = BookSerializer()
77 children = BookSerializer(many=True)
79 xml = AbsoluteURLField(source='xml_url')
80 html = AbsoluteURLField(source='html_url')
81 txt = AbsoluteURLField(source='txt_url')
82 fb2 = AbsoluteURLField(source='fb2_url')
83 epub = AbsoluteURLField(source='epub_url')
84 mobi = AbsoluteURLField(source='mobi_url')
85 pdf = AbsoluteURLField(source='pdf_url')
86 media = MediaSerializer(many=True)
87 cover_thumb = ThumbnailField('139x193', source='cover')
88 simple_thumb = serializers.FileField(source='cover_api_thumb')
94 'epochs', 'genres', 'kinds', 'authors',
95 'fragment_data', 'children', 'parent', 'preview',
96 'epub', 'mobi', 'pdf', 'html', 'txt', 'fb2', 'xml', 'media', 'audio_length',
97 'cover_color', 'simple_cover', 'cover_thumb', 'cover', 'simple_thumb'
99 legacy_non_null_fields = ['html', 'txt', 'fb2', 'epub', 'mobi', 'pdf',
100 'cover', 'simple_cover', 'cover_thumb', 'simple_thumb']
103 class BookPreviewSerializer(BookDetailSerializer):
106 fields = BookDetailSerializer.Meta.fields + ['slug']
107 legacy_non_null_fields = BookDetailSerializer.Meta.legacy_non_null_fields
110 class EbookSerializer(BookSerializer):
113 fields = ['author', 'href', 'title', 'cover', 'slug'] + Book.ebook_formats
114 legacy_non_null_fields = ['author', 'cover'] + Book.ebook_formats
117 class CollectionListSerializer(serializers.ModelSerializer):
118 url = AbsoluteURLField()
119 href = AbsoluteURLField(view_name='collection-detail', view_args=['slug'])
123 fields = ['url', 'href', 'title']
126 class CollectionSerializer(serializers.ModelSerializer):
127 books = BookSerializer(many=True, source='get_books')
128 url = AbsoluteURLField()
132 fields = ['url', 'books', 'description', 'title']
135 class FragmentSerializer(serializers.ModelSerializer):
136 book = BookSerializer()
137 url = AbsoluteURLField()
138 href = AbsoluteURLField(source='get_api_url')
142 fields = ['book', 'url', 'anchor', 'href']
145 class FragmentDetailSerializer(serializers.ModelSerializer):
146 book = BookSerializer()
147 url = AbsoluteURLField()
148 themes = TagSerializer(many=True)
152 fields = ['book', 'anchor', 'text', 'url', 'themes']