From 11e2dea9f6ad6f8d470f0d4d1aa335cca3900c89 Mon Sep 17 00:00:00 2001 From: Oleg Komarov Date: Mon, 8 Apr 2024 14:32:45 +0200 Subject: [PATCH] update API response structure #71 --- extensions/tests/test_views.py | 8 +++++--- extensions/urls.py | 1 - extensions/views/api.py | 18 +++++++++++------- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/extensions/tests/test_views.py b/extensions/tests/test_views.py index e843cea2..c52bfd24 100644 --- a/extensions/tests/test_views.py +++ b/extensions/tests/test_views.py @@ -52,8 +52,10 @@ class PublicViewsTest(_BaseTestCase): response = self.client.get(url, HTTP_ACCEPT=HTTP_ACCEPT) self.assertEqual(response.status_code, 200) self.assertEqual(response['Content-Type'], 'application/json') - self.assertEqual(len(response.json()), 3) - for _, v in response.json().items(): + json = response.json() + self.assertEqual(len(json['data']), 3) + for v in json['data']: + self.assertIn('id', v) self.assertIn('name', v) self.assertIn('tagline', v) self.assertIn('version', v) @@ -191,7 +193,7 @@ class ListedExtensionsTest(_BaseTestCase): self.assertEqual(response['Content-Type'], 'application/json') # Basic sanity check to make sure we are getting the result of listed - listed_count = len(response.json()) + listed_count = len(response.json()['data']) self.assertEqual(Extension.objects.listed.count(), listed_count) return listed_count diff --git a/extensions/urls.py b/extensions/urls.py index a44d54d6..4e56523d 100644 --- a/extensions/urls.py +++ b/extensions/urls.py @@ -18,7 +18,6 @@ urlpatterns = [ path('api/v1/extensions/', api.ExtensionsAPIView.as_view(), name='api'), # Public pages path('', public.HomeView.as_view(), name='home'), - path('', api.ExtensionsAPIView.as_view(), name='home-api'), path('search/', public.SearchView.as_view(), name='search'), path('author//', public.SearchView.as_view(), name='by-author'), path('search/', public.SearchView.as_view(), name='search'), diff --git a/extensions/views/api.py b/extensions/views/api.py index a38df6bd..89d2fc93 100644 --- a/extensions/views/api.py +++ b/extensions/views/api.py @@ -53,6 +53,7 @@ class ListedExtensionsSerializer(serializers.ModelSerializer): return {} data = { + 'id': instance.extension_id, 'schema_version': instance.latest_version.schema_version, 'name': instance.name, 'version': instance.latest_version.version, @@ -75,16 +76,12 @@ class ListedExtensionsSerializer(serializers.ModelSerializer): 'tags': [str(tag) for tag in instance.latest_version.tags.all()], } - return {instance.extension_id: clean_json_dictionary_from_optional_fields(data)} + return clean_json_dictionary_from_optional_fields(data) class ExtensionsAPIView(APIView): serializer_class = ListedExtensionsSerializer - @staticmethod - def _convert_list_to_dict(data): - return {k: v for d in data for k, v in d.items()} - @extend_schema( parameters=[ OpenApiParameter( @@ -99,5 +96,12 @@ class ExtensionsAPIView(APIView): serializer = self.serializer_class( Extension.objects.listed, blender_version=blender_version, request=request, many=True ) - data_as_dict = self._convert_list_to_dict(serializer.data) - return Response(data_as_dict) + data = serializer.data + return Response( + { + # TODO implement extension blocking by moderators + 'blocklist': [], + 'data': data, + 'version': 'v1', + } + ) -- 2.30.2