From b6d44713f806e7186589da45d7622123a9c36a60 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Wed, 22 Jan 2025 15:21:58 +0100 Subject: [PATCH] Some more data in api. --- src/catalogue/api/serializers.py | 94 +++++++++++++++++++++++++++++++- src/catalogue/api/urls2.py | 25 +++++++++ src/catalogue/api/views.py | 36 ++++++++++++ 3 files changed, 153 insertions(+), 2 deletions(-) diff --git a/src/catalogue/api/serializers.py b/src/catalogue/api/serializers.py index ee3c5067f..5f96e0270 100644 --- a/src/catalogue/api/serializers.py +++ b/src/catalogue/api/serializers.py @@ -40,6 +40,84 @@ class TagDetailSerializer(serializers.ModelSerializer): ] +class AuthorItemSerializer(serializers.ModelSerializer): + url = AbsoluteURLField() + href = AbsoluteURLField( + view_name='catalogue_api_author', + view_args=('slug',) + ) + + class Meta: + model = Tag + fields = [ + 'url', 'href', 'name' + ] + +class AuthorSerializer(AuthorItemSerializer): + photo_thumb = ThumbnailField('139x193', source='photo') + + class Meta: + model = Tag + fields = [ + 'url', 'href', 'name', 'slug', 'sort_key', 'description', + 'genitive', 'photo', 'photo_thumb', 'photo_attribution', + ] + +class EpochItemSerializer(serializers.ModelSerializer): + url = AbsoluteURLField() + href = AbsoluteURLField( + view_name='catalogue_api_epoch', + view_args=('slug',) + ) + class Meta: + model = Tag + fields = ['url', 'href', 'name'] + +class EpochSerializer(EpochItemSerializer): + class Meta: + model = Tag + fields = [ + 'url', 'href', 'name', 'slug', 'sort_key', 'description', + 'adjective_feminine_singular', 'adjective_nonmasculine_plural', + ] + +class GenreItemSerializer(serializers.ModelSerializer): + url = AbsoluteURLField() + href = AbsoluteURLField( + view_name='catalogue_api_genre', + view_args=('slug',) + ) + class Meta: + model = Tag + fields = ['url', 'href', 'name'] + +class GenreSerializer(GenreItemSerializer): + class Meta: + model = Tag + fields = [ + 'url', 'href', 'name', 'slug', 'sort_key', 'description', + 'plural', 'genre_epoch_specific', + ] + +class KindItemSerializer(serializers.ModelSerializer): + url = AbsoluteURLField() + href = AbsoluteURLField( + view_name='catalogue_api_kind', + view_args=('slug',) + ) + class Meta: + model = Tag + fields = ['url', 'href', 'name'] + +class KindSerializer(KindItemSerializer): + class Meta: + model = Tag + fields = [ + 'url', 'href', 'name', 'slug', 'sort_key', 'description', + 'collective_noun', + ] + + class TranslatorSerializer(serializers.Serializer): name = serializers.CharField(source='*') @@ -55,12 +133,24 @@ class BookSerializer2(serializers.ModelSerializer): mobi = EmbargoURLField(source='mobi_url') pdf = EmbargoURLField(source='pdf_url') + authors = AuthorItemSerializer(many=True) + translators = AuthorItemSerializer(many=True) + epochs = EpochItemSerializer(many=True) + genres = GenreItemSerializer(many=True) + kinds = KindItemSerializer(many=True) + parent = serializers.HyperlinkedRelatedField( + read_only=True, + view_name='catalogue_api_book', + lookup_field='slug' + ) + class Meta: model = Book fields = [ - 'full_sort_key', 'title', + 'slug', 'title', 'full_sort_key', 'href', 'url', 'language', - #'epochs', 'genres', 'kinds', 'authors', 'translators', + 'authors', 'translators', + 'epochs', 'genres', 'kinds', #'children', 'parent', 'preview', 'epub', 'mobi', 'pdf', 'html', 'txt', 'fb2', 'xml', diff --git a/src/catalogue/api/urls2.py b/src/catalogue/api/urls2.py index 3fd0023d1..8748e2cf1 100644 --- a/src/catalogue/api/urls2.py +++ b/src/catalogue/api/urls2.py @@ -15,4 +15,29 @@ urlpatterns = [ piwik_track_view(views.BookDetail2.as_view()), name='catalogue_api_book' ), + + path('authors/', + piwik_track_view(views.AuthorList.as_view()), + name="catalogue_api_author_list"), + path('authors//', + piwik_track_view(views.AuthorView.as_view()), + name='catalogue_api_author'), + path('epochs/', + piwik_track_view(views.EpochList.as_view()), + name="catalogue_api_epoch_list"), + path('epochs//', + piwik_track_view(views.EpochView.as_view()), + name='catalogue_api_epoch'), + path('kinds/', + piwik_track_view(views.KindList.as_view()), + name="catalogue_api_kind_list"), + path('kinds//', + piwik_track_view(views.KindView.as_view()), + name='catalogue_api_kind'), + path('genres/', + piwik_track_view(views.GenreList.as_view()), + name="catalogue_api_genre_list"), + path('genres//', + piwik_track_view(views.GenreView.as_view()), + name='catalogue_api_genre'), ] diff --git a/src/catalogue/api/views.py b/src/catalogue/api/views.py index abe8064c4..9c8f4f177 100644 --- a/src/catalogue/api/views.py +++ b/src/catalogue/api/views.py @@ -328,6 +328,42 @@ class TagCategoryView(LegacyListAPIView): return tags +class AuthorList(ListAPIView): + serializer_class = serializers.AuthorSerializer + queryset = Tag.objects.filter(category='author') + +class AuthorView(RetrieveAPIView): + serializer_class = serializers.AuthorSerializer + queryset = Tag.objects.filter(category='author') + lookup_field = 'slug' + +class EpochList(ListAPIView): + serializer_class = serializers.EpochSerializer + queryset = Tag.objects.filter(category='epoch') + +class EpochView(RetrieveAPIView): + serializer_class = serializers.EpochSerializer + queryset = Tag.objects.filter(category='epoch') + lookup_field = 'slug' + +class GenreList(ListAPIView): + serializer_class = serializers.GenreSerializer + queryset = Tag.objects.filter(category='genre') + +class GenreView(RetrieveAPIView): + serializer_class = serializers.GenreSerializer + queryset = Tag.objects.filter(category='genre') + lookup_field = 'slug' + +class KindList(ListAPIView): + serializer_class = serializers.KindSerializer + queryset = Tag.objects.filter(category='kind') + +class KindView(RetrieveAPIView): + serializer_class = serializers.KindSerializer + queryset = Tag.objects.filter(category='kind') + lookup_field = 'slug' + class TagView(RetrieveAPIView): permission_classes = [DjangoModelPermissionsOrAnonReadOnly] -- 2.20.1