diff --git a/backend/src/openarchiefbeheer/api/urls.py b/backend/src/openarchiefbeheer/api/urls.py index 310915f4..40c7b1fe 100644 --- a/backend/src/openarchiefbeheer/api/urls.py +++ b/backend/src/openarchiefbeheer/api/urls.py @@ -23,7 +23,10 @@ ) from openarchiefbeheer.zaken.api.views import ( CacheZakenView, + InformatieobjecttypeChoicesView, + ResultaattypeChoicesView, SelectielijstklasseChoicesView, + StatustypeChoicesView, ZaaktypenChoicesView, ) from openarchiefbeheer.zaken.api.viewsets import ZakenViewSet @@ -111,6 +114,21 @@ SelectielijstklasseChoicesView.as_view(), name="retrieve-selectielijstklasse-choices", ), + path( + "_statustype-choices/", + StatustypeChoicesView.as_view(), + name="retrieve-statustype-choices", + ), + path( + "_informatieobjecttype-choices/", + InformatieobjecttypeChoicesView.as_view(), + name="retrieve-informatieobjecttype-choices", + ), + path( + "_resultaattype-choices/", + ResultaattypeChoicesView.as_view(), + name="retrieve-resultaattype-choices", + ), path("", include(router.urls)), ] ), diff --git a/backend/src/openarchiefbeheer/zaken/api/serializers.py b/backend/src/openarchiefbeheer/zaken/api/serializers.py index 44bb826e..f27d2a8c 100644 --- a/backend/src/openarchiefbeheer/zaken/api/serializers.py +++ b/backend/src/openarchiefbeheer/zaken/api/serializers.py @@ -60,6 +60,15 @@ class Meta: ) +class ChoiceSerializer(serializers.Serializer): + label = serializers.CharField(help_text=_("The description field of the choice.")) + value = serializers.CharField(help_text=_("The URL of the choice.")) + extra = serializers.CharField( + help_text=_("Any extra information about this choice."), + required=False, + ) + + class ZaaktypeChoiceSerializer(serializers.Serializer): label = serializers.CharField(help_text=_("The description field of the zaaktype.")) value = serializers.CharField(help_text=_("The URL field of the zaaktype.")) diff --git a/backend/src/openarchiefbeheer/zaken/api/views.py b/backend/src/openarchiefbeheer/zaken/api/views.py index 16b71c2d..b553fd13 100644 --- a/backend/src/openarchiefbeheer/zaken/api/views.py +++ b/backend/src/openarchiefbeheer/zaken/api/views.py @@ -13,9 +13,14 @@ from ..models import Zaak from ..tasks import retrieve_and_cache_zaken_from_openzaak -from ..utils import format_zaaktype_choices, retrieve_selectielijstklasse_choices +from ..utils import ( + format_zaaktype_choices, + retrieve_paginated_type, + retrieve_selectielijstklasse_choices, +) from .filtersets import ZaakFilter from .serializers import ( + ChoiceSerializer, SelectielijstklasseChoicesQueryParamSerializer, SelectielijstklasseChoicesSerializer, ZaaktypeChoiceSerializer, @@ -108,3 +113,75 @@ def get(self, request, *args, **kwargs): choices = retrieve_selectielijstklasse_choices(query_params) return Response(data=choices) + + +class StatustypeChoicesView(APIView): + permission_classes = [IsAuthenticated] + + @extend_schema( + summary=_("Retrieve statustypen choices"), + description=_( + "Retrieve statustypen from Open Zaak and return a " + "value and a label per statustype. The label is the field 'omschrijving'." + ), + tags=["private"], + responses={ + 200: ChoiceSerializer(many=True), + }, + ) + @method_decorator(cache_page(60 * 15)) + def get(self, request, *args, **kwargs): + results = retrieve_paginated_type("statustypen") + + serializer = ChoiceSerializer(data=results, many=True) + serializer.is_valid(raise_exception=True) + + return Response(serializer.data, status=status.HTTP_200_OK) + + +class InformatieobjecttypeChoicesView(APIView): + permission_classes = [IsAuthenticated] + + @extend_schema( + summary=_("Retrieve informatieobjecttypen choices"), + description=_( + "Retrieve informatieobjecttypen from Open Zaak and return a " + "value and a label per informatieobjecttype. The label is the field 'omschrijving'." + ), + tags=["private"], + responses={ + 200: ChoiceSerializer(many=True), + }, + ) + @method_decorator(cache_page(60 * 15)) + def get(self, request, *args, **kwargs): + results = retrieve_paginated_type("informatieobjecttypen") + + serializer = ChoiceSerializer(data=results, many=True) + serializer.is_valid(raise_exception=True) + + return Response(serializer.data, status=status.HTTP_200_OK) + + +class ResultaattypeChoicesView(APIView): + permission_classes = [IsAuthenticated] + + @extend_schema( + summary=_("Retrieve resultaattypen choices"), + description=_( + "Retrieve resultaattypen from Open Zaak and return a " + "value and a label per resultaattype. The label is the field 'omschrijving'." + ), + tags=["private"], + responses={ + 200: ChoiceSerializer(many=True), + }, + ) + @method_decorator(cache_page(60 * 15)) + def get(self, request, *args, **kwargs): + results = retrieve_paginated_type("resultaattypen") + + serializer = ChoiceSerializer(data=results, many=True) + serializer.is_valid(raise_exception=True) + + return Response(serializer.data, status=status.HTTP_200_OK) diff --git a/backend/src/openarchiefbeheer/zaken/utils.py b/backend/src/openarchiefbeheer/zaken/utils.py index 7688f324..5290347f 100644 --- a/backend/src/openarchiefbeheer/zaken/utils.py +++ b/backend/src/openarchiefbeheer/zaken/utils.py @@ -336,3 +336,23 @@ def delete_zaak_and_related_objects(zaak: "Zaak", result_store: ResultStore) -> delete_relation_object(zrc_client, "zaak", zaak.url, result_store) delete_documents(result_store) delete_zaak(zaak, zrc_client, result_store) + + +@lru_cache +def retrieve_paginated_type(resource_path: str) -> list[DropDownChoice]: + def format_choice(item: dict) -> DropDownChoice: + return {"label": item["omschrijving"] or item["url"], "value": item["url"]} + + ztc_service = Service.objects.get(api_type=APITypes.ztc) + ztc_client = build_client(ztc_service) + + with ztc_client: + response = ztc_client.get(resource_path) + response.raise_for_status() + data_iterator = pagination_helper(ztc_client, response.json()) + + results = [] + for page in data_iterator: + results += [format_choice(result) for result in page["results"]] + + return results