Skip to content

Commit

Permalink
Feature/expand to zoek (#230)
Browse files Browse the repository at this point in the history
* feature : exclusions to zoek and bugfix to exclusions

* update : openapi
  • Loading branch information
MatthijsBekendam authored Sep 25, 2023
1 parent b7b8a9b commit 3808104
Show file tree
Hide file tree
Showing 5 changed files with 2,870 additions and 2,141 deletions.
5 changes: 4 additions & 1 deletion src/drc/api/exclusions.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ def build_expand_schema(
):
"""Build the expand schema for the response. First, the fields to expand are split on the "." character. Then, the first part of the split is used to get the urls from the result. The urls are then used to get the corresponding data from the external api or from the local database. The data is then gathered/collected inside a list consisted of namedtuples. When all data is collected, it calls the _build_json method which builds the json response."""
expansion = {"_expand": {}}
self.expanded_fields_all = []

for exp_field in fields_to_expand:
loop_id = str(uuid.uuid4())
Expand Down Expand Up @@ -509,9 +510,11 @@ def remove_key(self, data, target_key):

def inclusions(self, response):
expand_filter = self.request.query_params.get("expand", "")
if self.action == "_zoek":
expand_filter = self.get_search_input().get("expand", "")
if expand_filter:
fields_to_expand = expand_filter.split(",")
if self.action == "list":
if self.action == "list" or self.action == "_zoek":
for response_data in (
response.data
if isinstance(response.data, list)
Expand Down
8 changes: 8 additions & 0 deletions src/drc/api/serializers/enkelvoudig_informatieobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,14 @@ class EIOZoekSerializer(serializers.Serializer):
child=serializers.UUIDField(),
help_text=_("Array of unieke resource identifiers (UUID4)"),
)
expand = serializers.CharField(
help_text=_(
"Examples: \n"
"`expand=zaaktype, status, status.statustype, hoofdzaak.status.statustype, hoofdzaak.deelzaken.status.statustype`\n"
"Haal details van gelinkte resources direct op. Als je meerdere resources tegelijk wilt ophalen kun je deze scheiden met een komma. Voor het ophalen van resources die een laag dieper genest zijn wordt de punt-notatie gebruikt.",
),
required=False,
)


class SchemaEIOSerializer(EnkelvoudigInformatieObjectSerializer):
Expand Down
13 changes: 13 additions & 0 deletions src/drc/api/tests/test_enkelvoudiginformatieobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,19 @@ def test_zoek_uuid_in(self):
self.assertEqual(data[0]["url"], f"http://testserver{reverse(eio1)}")
self.assertEqual(data[1]["url"], f"http://testserver{reverse(eio2)}")

def test_zoek_expand(self):
eio1, eio2, eio3 = EnkelvoudigInformatieObjectFactory.create_batch(3)
url = get_operation_url("enkelvoudiginformatieobject__zoek")
data = {"uuid__in": [eio1.uuid, eio2.uuid], "expand": "url"}
response = self.client.post(url, data)

self.assertEqual(response.status_code, status.HTTP_200_OK)

data = response.json()["results"]
data = sorted(data, key=lambda eio: eio["identificatie"])
self.assertEqual(len(data), 2)
self.assertTrue(bool(data[0]["_expand"]))

def test_zoek_without_params(self):
url = get_operation_url("enkelvoudiginformatieobject__zoek")
response = self.client.post(url, {})
Expand Down
9 changes: 8 additions & 1 deletion src/drc/api/views/enkelvoudig_informatieobject.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.db import transaction
from django.db import models, transaction
from django.utils.translation import gettext as _

from drf_spectacular.types import OpenApiTypes
Expand Down Expand Up @@ -214,12 +214,18 @@ def _zoek(self, request, *args, **kwargs):
search_input = self.get_search_input()
queryset = self.filter_queryset(self.get_queryset())
for name, value in search_input.items():
if name == "expand":
continue
queryset = queryset.filter(**{name: value})

return self.get_search_output(queryset)

_zoek.is_search_action = True

def get_search_output(self, queryset: models.QuerySet):
response = super().get_search_output(queryset)
return self.inclusions(response)

@transaction.atomic
def perform_destroy(self, instance):
if instance.canonical.objectinformatieobject_set.exists():
Expand Down Expand Up @@ -270,6 +276,7 @@ def retrieve(self, request, *args, **kwargs):
)
def create(self, request, *args, **kwargs):
return super().create(request, *args, **kwargs)

@extend_schema(
# see https://swagger.io/docs/specification/2-0/describing-responses/ and
# https://swagger.io/docs/specification/2-0/mime-types/
Expand Down
Loading

0 comments on commit 3808104

Please sign in to comment.