From 67483fe979b2e3bde09722e2c00bfbdaddf268ba Mon Sep 17 00:00:00 2001 From: Steven Bal Date: Thu, 6 Feb 2025 17:08:27 +0100 Subject: [PATCH] :ok_hand: [#5016] Process PR feedback --- src/openapi.yaml | 13 ++---- src/openforms/api/urls.py | 12 +++--- src/openforms/api/utils.py | 9 +++- .../referentielijsten/api/serializers.py | 4 -- .../contrib/referentielijsten/api/viewsets.py | 41 +++++++------------ .../tests/test_tabellen_list.py | 8 ++-- .../formio_builder/referentielijsten.js | 2 +- src/openforms/services/api/viewsets.py | 5 +-- 8 files changed, 38 insertions(+), 56 deletions(-) diff --git a/src/openapi.yaml b/src/openapi.yaml index 0df897a580..4084d36b17 100644 --- a/src/openapi.yaml +++ b/src/openapi.yaml @@ -3719,7 +3719,7 @@ paths: get: operationId: referentielijst_tabellen_list description: |- - Return a list of available (JSON) tabellen in a given Referentielijsten service configured in the backend. + Return a list of available tabellen in a given Referentielijsten service configured in the backend. Note that this endpoint is **EXPERIMENTAL**. summary: List tabellen for a Referentielijsten service @@ -3728,7 +3728,6 @@ paths: name: service_slug schema: type: string - pattern: ^[-a-zA-Z0-9_]+$ required: true tags: - referentielijst-tabellen @@ -4210,7 +4209,9 @@ paths: name: type schema: type: string - description: 'The type of Services to return, possible values: `referentielijsten`' + enum: + - referentielijsten + description: The type of Services to return tags: - services security: @@ -9704,18 +9705,12 @@ components: naam: type: string description: The name of the table. - einddatumGeldigheid: - type: string - format: date-time - writeOnly: true - description: The timestamp on which the tabel expires. isGeldig: type: boolean readOnly: true description: Indicates whether or not the table is expired. required: - code - - einddatumGeldigheid - isGeldig - naam x-experimental: true diff --git a/src/openforms/api/urls.py b/src/openforms/api/urls.py index 9ba9b8337e..5aa0982187 100644 --- a/src/openforms/api/urls.py +++ b/src/openforms/api/urls.py @@ -63,13 +63,6 @@ # services router.register("services", ServiceViewSet) -# referentielijsten -router.register( - "referentielijst-tabellen", - ReferentielijstenTabellenViewSet, - basename="referentielijst-tabellen", -) - # service fetch configurations router.register("service-fetch-configurations", ServiceFetchConfigurationViewSet) @@ -130,6 +123,11 @@ path("formio/", include("openforms.formio.api.urls")), path("geo/", include("openforms.contrib.kadaster.api.urls")), path("i18n/", include("openforms.translations.api.urls")), + path( + "referentielijst-tabellen/", + ReferentielijstenTabellenViewSet.as_view(), + name="referentielijst-tabellen-list", + ), path("", include(router.urls)), path("", include(forms_router.urls)), path("", include(submissions_router.urls)), diff --git a/src/openforms/api/utils.py b/src/openforms/api/utils.py index f711cb9e23..e2df09d20a 100644 --- a/src/openforms/api/utils.py +++ b/src/openforms/api/utils.py @@ -1,6 +1,6 @@ import re from collections.abc import Mapping -from typing import Any +from typing import Any, TypeVar from django.db import models @@ -46,7 +46,12 @@ def get_model_serializer_instance(serializer: ModelSerializer) -> models.Model: return serializer.Meta.model() -def mark_experimental(func_or_class): +T = TypeVar( + "T", bound=type +) # ensure `mark_experimental` does not change the type signature + + +def mark_experimental(func_or_class: T) -> T: if issubclass(func_or_class, Serializer): extend_fn = extend_schema_serializer elif issubclass(func_or_class, APIView): diff --git a/src/openforms/contrib/referentielijsten/api/serializers.py b/src/openforms/contrib/referentielijsten/api/serializers.py index 208518d9a1..2b7fa35e0b 100644 --- a/src/openforms/contrib/referentielijsten/api/serializers.py +++ b/src/openforms/contrib/referentielijsten/api/serializers.py @@ -13,10 +13,6 @@ class ReferentielijstTabellenSerializer(serializers.Serializer): help_text=_("The unique code that identifies the table.") ) naam = serializers.CharField(help_text=_("The name of the table.")) - einddatumGeldigheid = serializers.DateTimeField( - help_text=_("The timestamp on which the tabel expires."), write_only=True - ) - is_geldig = serializers.SerializerMethodField( help_text=_("Indicates whether or not the table is expired.") ) diff --git a/src/openforms/contrib/referentielijsten/api/viewsets.py b/src/openforms/contrib/referentielijsten/api/viewsets.py index b4f2994910..1f7ca7444b 100644 --- a/src/openforms/contrib/referentielijsten/api/viewsets.py +++ b/src/openforms/contrib/referentielijsten/api/viewsets.py @@ -1,46 +1,38 @@ from django.shortcuts import get_object_or_404 from django.utils.translation import gettext_lazy as _ -from drf_spectacular.utils import extend_schema, extend_schema_view +from drf_spectacular.utils import extend_schema from requests.exceptions import RequestException -from rest_framework import authentication, permissions, serializers, status, viewsets -from rest_framework.decorators import action -from rest_framework.response import Response +from rest_framework import authentication, permissions +from rest_framework.views import APIView from zgw_consumers.client import build_client from zgw_consumers.models import Service from openforms.api.utils import mark_experimental +from openforms.api.views import ListMixin -from ..client import ReferentielijstenClient +from ..client import ReferentielijstenClient, Tabel from .serializers import ReferentielijstTabellenSerializer -@extend_schema_view( - get=extend_schema( - summary=_("List tabellen for a Referentielijsten service"), - description=_( - "Return a list of available (JSON) tabellen in a given Referentielijsten service configured " - "in the backend.\n\n" - "Note that this endpoint is **EXPERIMENTAL**." - ), - responses={ - 200: ReferentielijstTabellenSerializer(many=True), - }, - ) +@extend_schema( + summary=_("List tabellen for a Referentielijsten service"), ) @mark_experimental -class ReferentielijstenTabellenViewSet(viewsets.ViewSet): +class ReferentielijstenTabellenViewSet(ListMixin, APIView): """ - List tabellen for a given Referentielijst service + Return a list of available tabellen in a given Referentielijsten service configured in the backend. + + Note that this endpoint is **EXPERIMENTAL**. """ authentication_classes = (authentication.SessionAuthentication,) permission_classes = (permissions.IsAdminUser,) serializer_class = ReferentielijstTabellenSerializer - @action(detail=False, methods=["get"], url_path="(?P[-a-zA-Z0-9_]+)") - def get(self, request, service_slug: str | None = None): - service = get_object_or_404(Service, slug=service_slug) + def get_objects(self) -> list[Tabel]: + service = get_object_or_404(Service, slug=self.kwargs["service_slug"]) + try: with build_client( service, client_factory=ReferentielijstenClient @@ -49,7 +41,4 @@ def get(self, request, service_slug: str | None = None): except RequestException: result = [] - assert issubclass(self.serializer_class, serializers.Serializer) - serializer = self.serializer_class(data=result, many=True) - serializer.is_valid() - return Response(serializer.data, status=status.HTTP_200_OK) + return result diff --git a/src/openforms/contrib/referentielijsten/tests/test_tabellen_list.py b/src/openforms/contrib/referentielijsten/tests/test_tabellen_list.py index a24b8e2302..a5b4253558 100644 --- a/src/openforms/contrib/referentielijsten/tests/test_tabellen_list.py +++ b/src/openforms/contrib/referentielijsten/tests/test_tabellen_list.py @@ -28,7 +28,7 @@ def setUpTestData(cls): def test_tabellen_list_is_forbidden_for_normal_users(self): endpoint = reverse( - "api:referentielijst-tabellen-get", + "api:referentielijst-tabellen-list", kwargs={"service_slug": "referentielijsten"}, ) self.client.force_authenticate(user=self.user) @@ -39,7 +39,7 @@ def test_tabellen_list_is_forbidden_for_normal_users(self): def test_returned_tabellen_have_the_right_properties(self): endpoint = reverse( - "api:referentielijst-tabellen-get", + "api:referentielijst-tabellen-list", kwargs={"service_slug": "referentielijsten"}, ) self.client.force_authenticate(user=self.admin_user) @@ -69,7 +69,7 @@ def test_returned_tabellen_have_the_right_properties(self): def test_service_not_found(self): endpoint = reverse( - "api:referentielijst-tabellen-get", kwargs={"service_slug": "non-existent"} + "api:referentielijst-tabellen-list", kwargs={"service_slug": "non-existent"} ) self.client.force_authenticate(user=self.admin_user) @@ -82,7 +82,7 @@ def test_referentielijsten_api_returns_404(self): slug="incorrect-api-root", api_root="http://localhost:8004/incorrect/" ) endpoint = reverse( - "api:referentielijst-tabellen-get", + "api:referentielijst-tabellen-list", kwargs={"service_slug": "incorrect-api-root"}, ) self.client.force_authenticate(user=self.admin_user) diff --git a/src/openforms/js/components/formio_builder/referentielijsten.js b/src/openforms/js/components/formio_builder/referentielijsten.js index b9a4bab578..f4ac014277 100644 --- a/src/openforms/js/components/formio_builder/referentielijsten.js +++ b/src/openforms/js/components/formio_builder/referentielijsten.js @@ -1,7 +1,7 @@ import {get} from 'utils/fetch'; export const getServices = async type => { - const resp = await get(`/api/v2/services?type=${encodeURIComponent(type)}`); + const resp = await get(`/api/v2/services`, {type: type}); return resp.data; }; diff --git a/src/openforms/services/api/viewsets.py b/src/openforms/services/api/viewsets.py index 2492378842..621f7c53a7 100644 --- a/src/openforms/services/api/viewsets.py +++ b/src/openforms/services/api/viewsets.py @@ -23,10 +23,9 @@ name="type", type=str, location=OpenApiParameter.QUERY, - description=_( - "The type of Services to return, possible values: `referentielijsten`" - ), + description=_("The type of Services to return."), required=False, + enum=["referentielijsten"], ) ], ),