From: Radek Czajka Date: Mon, 23 Mar 2020 13:18:12 +0000 (+0100) Subject: Book translator in API. X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/099458fdc499ac939a42af27c0df30afcdff55e9?ds=sidebyside Book translator in API. --- diff --git a/src/catalogue/api/serializers.py b/src/catalogue/api/serializers.py index 79bb58b6c..d52959b92 100644 --- a/src/catalogue/api/serializers.py +++ b/src/catalogue/api/serializers.py @@ -27,6 +27,10 @@ class TagDetailSerializer(serializers.ModelSerializer): fields = ['name', 'url', 'sort_key', 'description'] +class TranslatorSerializer(serializers.Serializer): + name = serializers.CharField(source='*') + + class BookSerializer(LegacyMixin, serializers.ModelSerializer): author = serializers.CharField(source='author_unicode') kind = serializers.CharField(source='kind_unicode') @@ -79,6 +83,7 @@ class BookDetailSerializer(LegacyMixin, serializers.ModelSerializer): url = AbsoluteURLField() authors = TagSerializer(many=True) + translators = TranslatorSerializer(many=True) epochs = TagSerializer(many=True) genres = TagSerializer(many=True) kinds = TagSerializer(many=True) @@ -102,7 +107,7 @@ class BookDetailSerializer(LegacyMixin, serializers.ModelSerializer): model = Book fields = [ 'title', 'url', - 'epochs', 'genres', 'kinds', 'authors', + 'epochs', 'genres', 'kinds', 'authors', 'translators', 'fragment_data', 'children', 'parent', 'preview', 'epub', 'mobi', 'pdf', 'html', 'txt', 'fb2', 'xml', 'media', 'audio_length', 'cover_color', 'simple_cover', 'cover_thumb', 'cover', 'simple_thumb' diff --git a/src/catalogue/models/book.py b/src/catalogue/models/book.py index 534f00bae..81f3b79f8 100644 --- a/src/catalogue/models/book.py +++ b/src/catalogue/models/book.py @@ -172,6 +172,12 @@ class Book(models.Model): def genre_unicode(self): return self.tag_unicode('genre') + def translators(self): + translators = self.get_extra_info_json().get('translators') + return [ + '\xa0'.join(reversed(translator.split(', ', 1))) for translator in translators + ] + def translator(self): translators = self.get_extra_info_json().get('translators') if not translators: