From c3e563338050cf161ed6568d01c8a5f010257c2f Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Tue, 5 Feb 2019 09:52:45 +0100 Subject: [PATCH 1/1] Preserve the weirdness of the API. --- src/api/tests/res/responses/books.json | 3 +++ src/api/tests/res/responses/books.xml | 3 +-- src/catalogue/api/serializers.py | 18 ++++++++---------- src/catalogue/api/views.py | 4 ++-- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/api/tests/res/responses/books.json b/src/api/tests/res/responses/books.json index cfd5ee3dd..4b8d11eba 100644 --- a/src/api/tests/res/responses/books.json +++ b/src/api/tests/res/responses/books.json @@ -8,6 +8,7 @@ "has_audio": false, "title": "Child", "cover": "", + "liked": null, "epoch": "", "href": "http://testserver/api/books/child/", "genre": "Wiersz", @@ -24,6 +25,7 @@ "has_audio": false, "title": "Grandchild", "cover": "", + "liked": null, "epoch": "", "href": "http://testserver/api/books/grandchild/", "genre": "Sonet", @@ -40,6 +42,7 @@ "has_audio": true, "title": "Parent", "cover": "cover/parent.jpg", + "liked": null, "epoch": "Barok", "href": "http://testserver/api/books/parent/", "genre": "Sonet", diff --git a/src/api/tests/res/responses/books.xml b/src/api/tests/res/responses/books.xml index 967f8e2ed..f39864480 100644 --- a/src/api/tests/res/responses/books.xml +++ b/src/api/tests/res/responses/books.xml @@ -1,3 +1,2 @@ -$child$2Childhttps://example.com/katalog/lektura/child/Falsechildhttps://example.com/api/books/child/Wiersz#000000$grandchild$3Grandchildhttps://example.com/katalog/lektura/grandchild/Falsegrandchildhttps://example.com/api/books/grandchild/Sonet#000000Lirykajohn doe$parent$1Parenthttps://example.com/katalog/lektura/parent/TrueJohn Doecover/parent.jpgparentBarokhttps://example.com/api/books/parent/Sonethttps://example.com/media/cover_api_thumb/parent.jpg#a6820ahttps://example.com/media/cover/parent.jpg-139x193 - +$child$2Childhttp://testserver/katalog/lektura/child/#000000http://testserver/api/books/child/FalseWierszchild$grandchild$3Grandchildhttp://testserver/katalog/lektura/grandchild/#000000http://testserver/api/books/grandchild/FalseSonetgrandchildLirykajohn doe$parent$1Parenthttp://testserver/katalog/lektura/parent/#a6820aJohn Doecover/parent.jpgBarokhttp://testserver/api/books/parent/TrueSonethttp://testserver/media/cover_api_thumb/parent.jpgparentcover_thumb/parent.jpg diff --git a/src/catalogue/api/serializers.py b/src/catalogue/api/serializers.py index e8f04ca70..ea48cdba4 100644 --- a/src/catalogue/api/serializers.py +++ b/src/catalogue/api/serializers.py @@ -24,16 +24,17 @@ class TagDetailSerializer(serializers.ModelSerializer): fields = ['name', 'url', 'sort_key', 'description'] -class BaseBookSerializer(LegacyMixin, serializers.ModelSerializer): +class BookSerializer(LegacyMixin, serializers.ModelSerializer): author = serializers.CharField(source='author_unicode') kind = serializers.CharField(source='kind_unicode') epoch = serializers.CharField(source='epoch_unicode') genre = serializers.CharField(source='genre_unicode') + liked = BookLiked() simple_thumb = serializers.FileField(source='cover_api_thumb') href = AbsoluteURLField(view_name='catalogue_api_book', view_args=['slug']) url = AbsoluteURLField() - cover = serializers.CharField() + cover = serializers.FileField() cover_thumb = ThumbnailField('139x193', source='cover') class Meta: @@ -41,20 +42,17 @@ class BaseBookSerializer(LegacyMixin, serializers.ModelSerializer): fields = [ 'kind', 'full_sort_key', 'title', 'url', 'cover_color', 'author', 'cover', 'epoch', 'href', 'has_audio', 'genre', - 'simple_thumb', 'slug', 'cover_thumb'] + 'simple_thumb', 'slug', 'cover_thumb', 'liked'] legacy_non_null_fields = [ 'kind', 'author', 'epoch', 'genre', 'cover', 'simple_thumb', 'cover_thumb'] -class BookSerializer(BaseBookSerializer): - liked = BookLiked() - cover = serializers.FileField() +class BookListSerializer(BookSerializer): + cover = serializers.CharField() + cover_thumb = serializers.CharField() - class Meta: - model = Book - fields = BaseBookSerializer.Meta.fields + ['liked'] - legacy_non_null_fields = BaseBookSerializer.Meta.legacy_non_null_fields + Meta = BookSerializer.Meta class MediaSerializer(LegacyMixin, serializers.ModelSerializer): diff --git a/src/catalogue/api/views.py b/src/catalogue/api/views.py index e21aab707..f3b719309 100644 --- a/src/catalogue/api/views.py +++ b/src/catalogue/api/views.py @@ -24,12 +24,12 @@ class CollectionDetail(RetrieveAPIView): class BookList(ListAPIView): permission_classes = [DjangoModelPermissionsOrAnonReadOnly] queryset = Book.objects.none() # Required for DjangoModelPermissions - serializer_class = serializers.BaseBookSerializer + serializer_class = serializers.BookListSerializer def get_queryset(self): try: tags, ancestors = read_tags( - self.kwargs['tags'], self.request, + self.kwargs.get('tags', ''), self.request, allowed=('author', 'epoch', 'kind', 'genre') ) except ValueError: -- 2.20.1