1 from rest_framework import serializers
2 from api.fields import AbsoluteURLField, LegacyMixin
3 from catalogue.models import Book, Collection, Tag, BookMedia
4 from .fields import BookLiked, ThumbnailField
7 class TagSerializer(serializers.ModelSerializer):
8 url = AbsoluteURLField()
9 href = AbsoluteURLField(
11 view_args=('category:category_plural', 'slug')
16 fields = ['url', 'href', 'name', 'slug']
19 class BookSerializer(LegacyMixin, serializers.ModelSerializer):
20 author = serializers.CharField(source='author_unicode')
21 kind = serializers.CharField(source='kind_unicode')
22 epoch = serializers.CharField(source='epoch_unicode')
23 genre = serializers.CharField(source='genre_unicode')
25 simple_thumb = serializers.FileField(source='cover_api_thumb')
26 href = AbsoluteURLField(view_name='catalogue_api_book', view_args=['slug'])
27 url = AbsoluteURLField()
29 cover_thumb = ThumbnailField('139x193', source='cover')
34 'kind', 'full_sort_key', 'title', 'url', 'cover_color', 'author',
35 'cover', 'liked', 'epoch', 'href', 'has_audio', 'genre',
36 'simple_thumb', 'slug', 'cover_thumb']
37 legacy_non_null_fields = [
38 'kind', 'author', 'epoch', 'genre',
39 'cover', 'simple_thumb', 'cover_thumb']
42 class MediaSerializer(LegacyMixin, serializers.ModelSerializer):
43 url = serializers.FileField(source='file')
47 fields = ['url', 'director', 'type', 'name', 'artist']
48 legacy_non_null_fields = ['director', 'artist']
51 class BookDetailSerializer(LegacyMixin, serializers.ModelSerializer):
52 url = AbsoluteURLField()
54 authors = TagSerializer(many=True)
55 epochs = TagSerializer(many=True)
56 genres = TagSerializer(many=True)
57 kinds = TagSerializer(many=True)
59 fragment_data = serializers.DictField()
60 parent = BookSerializer()
61 children = BookSerializer(many=True)
63 xml = AbsoluteURLField(source='xml_url')
64 html = AbsoluteURLField(source='html_url')
65 txt = AbsoluteURLField(source='txt_url')
66 fb2 = AbsoluteURLField(source='fb2_url')
67 epub = AbsoluteURLField(source='epub_url')
68 mobi = AbsoluteURLField(source='mobi_url')
69 pdf = AbsoluteURLField(source='pdf_url')
70 media = MediaSerializer(many=True)
71 cover_thumb = ThumbnailField('139x193', source='cover')
72 simple_thumb = serializers.FileField(source='cover_api_thumb')
78 'epochs', 'genres', 'kinds', 'authors',
79 'fragment_data', 'children', 'parent', 'preview',
80 'epub', 'mobi', 'pdf', 'html', 'txt', 'fb2', 'xml', 'media', 'audio_length',
81 'cover_color', 'simple_cover', 'cover_thumb', 'cover', 'simple_thumb'
83 legacy_non_null_fields = ['html', 'txt', 'fb2', 'epub', 'mobi', 'pdf',
84 'cover', 'simple_cover', 'cover_thumb', 'simple_thumb']
87 class CollectionListSerializer(serializers.ModelSerializer):
88 url = AbsoluteURLField()
89 href = AbsoluteURLField(view_name='collection-detail', view_args=['slug'])
93 fields = ['url', 'href', 'title']
96 class CollectionSerializer(serializers.ModelSerializer):
97 books = BookSerializer(many=True, source='get_books')
98 url = AbsoluteURLField()
102 fields = ['url', 'books', 'description', 'title']