Skip to content

Commit d4aff1c

Browse files
authored
Merge pull request #710 from maykinmedia/feature/706-behandelend-afdeling-filters
[#706] Behandelend afdeling filters - Backend
2 parents e4ca185 + 03c5eca commit d4aff1c

File tree

6 files changed

+263
-71
lines changed

6 files changed

+263
-71
lines changed

backend/src/openarchiefbeheer/api/urls.py

+6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
SelectionView,
3131
)
3232
from openarchiefbeheer.zaken.api.views import (
33+
BehandelendAfdelingInternalChoicesView,
3334
CacheZakenView,
3435
ExternalResultaattypeChoicesView,
3536
ExternalZaaktypenChoicesView,
@@ -182,6 +183,11 @@
182183
InternalResultaattypeChoicesView.as_view(),
183184
name="retrieve-internal-resultaattype-choices",
184185
),
186+
path(
187+
"_retrieve-behandelend-afdeling-choices-choices/",
188+
BehandelendAfdelingInternalChoicesView.as_view(),
189+
name="retrieve-behandelend-afdeling-choices",
190+
),
185191
path("", include(router.urls)),
186192
path("", include(destruction_list_router.urls)),
187193
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from django.core.cache import cache
2+
3+
4+
class ClearCacheMixin:
5+
def setUp(self):
6+
super().setUp()
7+
8+
self.addCleanup(cache.clear)

backend/src/openarchiefbeheer/zaken/api/filtersets.py

+4-23
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
from django.db.models import (
44
Case,
5-
CharField,
6-
F,
75
Func,
86
IntegerField,
97
Q,
@@ -121,10 +119,10 @@ class ZaakFilterSet(FilterSet):
121119
lookup_expr="icontains",
122120
)
123121

124-
behandelend_afdeling__icontains = CharFilter(
125-
field_name="behandelend_afdeling__icontains",
122+
behandelend_afdeling = CharFilter(
123+
field_name="behandelend_afdeling",
126124
method="filter_behandelend_afdeling",
127-
help_text="The 'behandelend afdeling' is the 'betrokkeneIdentificatie.identificatie' field of the roles related to the case which have betrokkeneType = organisatorische_eenheid.",
125+
help_text="Filter the zaken that contain in their `_expand.rollen` field the specified rol.",
128126
)
129127

130128
class Meta:
@@ -265,24 +263,7 @@ def filter_heeft_relaties(
265263
def filter_behandelend_afdeling(
266264
self, queryset: QuerySet[Zaak], name: str, value: str
267265
) -> QuerySet[Zaak]:
268-
zaken_with_afdeling = queryset.filter(
269-
_expand__rollen__contains=[
270-
{
271-
"betrokkene_type": "organisatorische_eenheid",
272-
}
273-
]
274-
)
275-
json_path_expression = '$.rollen[*] ? (@.betrokkene_type == "organisatorische_eenheid").betrokkene_identificatie.identificatie'
276-
zaken_with_afdeling = zaken_with_afdeling.annotate(
277-
behandelend_afdeling=Func(
278-
F("_expand"),
279-
Value(json_path_expression),
280-
function="jsonb_path_query_array",
281-
output_field=CharField(),
282-
)
283-
)
284-
285-
return zaken_with_afdeling.filter(behandelend_afdeling__contains=value)
266+
return queryset.filter(_expand__rollen__contains=[{"url": value}])
286267

287268

288269
class ZaakFilterBackend(DjangoFilterBackend):

backend/src/openarchiefbeheer/zaken/api/views.py

+44
Original file line numberDiff line numberDiff line change
@@ -276,5 +276,49 @@ def get(self, request, *args, **kwargs):
276276
"value": resultaattype["url"],
277277
}
278278
)
279+
return Response(formatted_choices, status=status.HTTP_200_OK)
280+
281+
282+
class BehandelendAfdelingInternalChoicesView(APIView):
283+
permission_classes = [IsAuthenticated]
284+
filter_backends = (NoModelFilterBackend,)
285+
filterset_class = ZaakFilterSet
286+
287+
@extend_schema(
288+
summary=_("Retrieve behandelend afdeling choices"),
289+
description=_(
290+
"Retrieve the behandelend afdelingen the zaken in the database. "
291+
'These have rollen with betrokkeneType equal to "organisatorische_eenheid".'
292+
),
293+
tags=["private"],
294+
responses={
295+
200: ChoiceSerializer(many=True),
296+
},
297+
)
298+
@method_decorator(cache_page(60 * 15))
299+
def get(self, request, *args, **kwargs):
300+
filterset = ZaakFilterSet(data=request.query_params or request.data)
301+
is_valid = filterset.is_valid()
302+
if not is_valid:
303+
raise ValidationError(filterset.errors)
304+
305+
zaken_rollen = filterset.qs.filter(
306+
_expand__rollen__contains=[{"betrokkene_type": "organisatorische_eenheid"}]
307+
).values_list("_expand__rollen", flat=True)
308+
309+
existing_rollen = []
310+
formatted_choices = []
311+
for zaak_rollen in zaken_rollen:
312+
for rol in zaak_rollen:
313+
if (
314+
rol["url"] in existing_rollen
315+
or rol["betrokkene_type"] != "organisatorische_eenheid"
316+
):
317+
continue
318+
319+
existing_rollen.append(rol["url"])
320+
formatted_choices.append(
321+
{"label": rol.get("omschrijving", rol["url"]), "value": rol["url"]}
322+
)
279323

280324
return Response(formatted_choices, status=status.HTTP_200_OK)

backend/src/openarchiefbeheer/zaken/tests/test_filtersets.py

+36-24
Original file line numberDiff line numberDiff line change
@@ -258,63 +258,71 @@ def test_filter_out_zaken_already_in_destruction_lists_except_one(self):
258258
self.assertEqual(item.zaak.url, urls_zaken[0])
259259

260260
def test_filter_behandelend_afdeling(self):
261-
# Should NOT be returned: natuurlijk_persoon != organisatorische_eenheid
262261
ZaakFactory.create(
262+
identificatie="ZAAK-01",
263263
post___expand={
264264
"rollen": [
265265
{
266-
"betrokkene_type": "natuurlijk_persoon",
267-
"betrokkene_identificatie": {"identificatie": "BLA1"},
266+
"url": "http://localhost:8003/zaken/api/v1/rollen/111-111-111",
267+
"betrokkene_type": "organisatorische_eenheid",
268+
"omschrijving": "Maykin Support Afdeling",
269+
},
270+
{
271+
"url": "http://localhost:8003/zaken/api/v1/rollen/222-222-222",
272+
"betrokkene_type": "organisatorische_eenheid",
273+
"omschrijving": "Maykin Dev Afdeling",
268274
},
269275
]
270-
}
276+
},
271277
)
272-
# Should be returned
273278
ZaakFactory.create(
279+
identificatie="ZAAK-02",
274280
post___expand={
275281
"rollen": [
276282
{
283+
"url": "http://localhost:8003/zaken/api/v1/rollen/111-111-111",
277284
"betrokkene_type": "organisatorische_eenheid",
278-
"betrokkene_identificatie": {"identificatie": "BLA1"},
285+
"omschrijving": "Maykin Support Afdeling",
279286
},
280287
]
281-
}
288+
},
282289
)
283-
# Should NOT be returned: identificatie does not contain BLA
284290
ZaakFactory.create(
291+
identificatie="ZAAK-03",
285292
post___expand={
286293
"rollen": [
287294
{
295+
"url": "http://localhost:8003/zaken/api/v1/rollen/333-333-333",
288296
"betrokkene_type": "organisatorische_eenheid",
289-
"betrokkene_identificatie": {"identificatie": "BLU1"},
297+
"omschrijving": "Maykin Design Afdeling",
298+
},
299+
{
300+
"url": "http://localhost:8003/zaken/api/v1/rollen/444-444-444",
301+
"betrokkene_type": "vestiging",
302+
"omschrijving": "Kantoor",
290303
},
291304
]
292-
}
305+
},
293306
)
294-
295-
# Should NOT be returned: no roles
296-
ZaakFactory.create(_expand={"rollen": []})
297-
298-
# Should be returned
299307
ZaakFactory.create(
308+
identificatie="ZAAK-04",
300309
post___expand={
301310
"rollen": [
302311
{
303-
"betrokkene_type": "organisatorische_eenheid",
304-
"betrokkene_identificatie": {"identificatie": "BLO1"},
305-
},
306-
{
307-
"betrokkene_type": "organisatorische_eenheid",
308-
"betrokkene_identificatie": {"identificatie": "BLA2"},
309-
},
312+
"url": "http://localhost:8003/zaken/api/v1/rollen/444-444-444",
313+
"betrokkene_type": "vestiging",
314+
"omschrijving": "Kantoor",
315+
}
310316
]
311-
}
317+
},
312318
)
313319

314320
user = UserFactory(username="record_manager", post__can_start_destruction=True)
315321

316322
endpoint = furl(reverse("api:zaken-list"))
317-
endpoint.args["behandelend_afdeling__icontains"] = "BLA"
323+
endpoint.args["behandelend_afdeling"] = (
324+
"http://localhost:8003/zaken/api/v1/rollen/111-111-111"
325+
)
318326

319327
self.client.force_authenticate(user)
320328
response = self.client.get(endpoint.url)
@@ -323,6 +331,10 @@ def test_filter_behandelend_afdeling(self):
323331
self.assertEqual(response.status_code, status.HTTP_200_OK)
324332
self.assertEqual(data["count"], 2)
325333

334+
zaken_returned = [zaak["identificatie"] for zaak in data["results"]]
335+
self.assertIn("ZAAK-01", zaken_returned)
336+
self.assertIn("ZAAK-02", zaken_returned)
337+
326338
def test_ordering(self):
327339
zaak_2 = ZaakFactory.create(identificatie="ZAAK-0000-0000000002")
328340
zaak_1 = ZaakFactory.create(identificatie="ZAAK-0000-0000000001")

0 commit comments

Comments
 (0)