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')
33 simple_thumb = serializers.FileField(source='cover_api_thumb')
34 href = AbsoluteURLField(view_name='catalogue_api_book', view_args=['slug'])
35 url = AbsoluteURLField()
37 cover_thumb = ThumbnailField('139x193', source='cover')
42 'kind', 'full_sort_key', 'title', 'url', 'cover_color', 'author',
43 'cover', 'liked', '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 MediaSerializer(LegacyMixin, serializers.ModelSerializer):
51 url = serializers.FileField(source='file')
55 fields = ['url', 'director', 'type', 'name', 'artist']
56 legacy_non_null_fields = ['director', 'artist']
59 class BookDetailSerializer(LegacyMixin, serializers.ModelSerializer):
60 url = AbsoluteURLField()
62 authors = TagSerializer(many=True)
63 epochs = TagSerializer(many=True)
64 genres = TagSerializer(many=True)
65 kinds = TagSerializer(many=True)
67 fragment_data = serializers.DictField()
68 parent = BookSerializer()
69 children = BookSerializer(many=True)
71 xml = AbsoluteURLField(source='xml_url')
72 html = AbsoluteURLField(source='html_url')
73 txt = AbsoluteURLField(source='txt_url')
74 fb2 = AbsoluteURLField(source='fb2_url')
75 epub = AbsoluteURLField(source='epub_url')
76 mobi = AbsoluteURLField(source='mobi_url')
77 pdf = AbsoluteURLField(source='pdf_url')
78 media = MediaSerializer(many=True)
79 cover_thumb = ThumbnailField('139x193', source='cover')
80 simple_thumb = serializers.FileField(source='cover_api_thumb')
86 'epochs', 'genres', 'kinds', 'authors',
87 'fragment_data', 'children', 'parent', 'preview',
88 'epub', 'mobi', 'pdf', 'html', 'txt', 'fb2', 'xml', 'media', 'audio_length',
89 'cover_color', 'simple_cover', 'cover_thumb', 'cover', 'simple_thumb'
91 legacy_non_null_fields = ['html', 'txt', 'fb2', 'epub', 'mobi', 'pdf',
92 'cover', 'simple_cover', 'cover_thumb', 'simple_thumb']
95 class CollectionListSerializer(serializers.ModelSerializer):
96 url = AbsoluteURLField()
97 href = AbsoluteURLField(view_name='collection-detail', view_args=['slug'])
101 fields = ['url', 'href', 'title']
104 class CollectionSerializer(serializers.ModelSerializer):
105 books = BookSerializer(many=True, source='get_books')
106 url = AbsoluteURLField()
110 fields = ['url', 'books', 'description', 'title']
113 class FragmentSerializer(serializers.ModelSerializer):
114 book = BookSerializer()
115 url = AbsoluteURLField()
116 href = AbsoluteURLField(source='get_api_url')
120 fields = ['book', 'url', 'anchor', 'href']
123 class FragmentDetailSerializer(serializers.ModelSerializer):
124 book = BookSerializer()
125 url = AbsoluteURLField()
126 themes = TagSerializer(many=True)
130 fields = ['book', 'anchor', 'text', 'url', 'themes']