From e4b24e3f93845c5b366f4089cfe4f78930d48e78 Mon Sep 17 00:00:00 2001 From: SilviaAmAm Date: Tue, 14 May 2024 13:27:40 +0200 Subject: [PATCH 1/2] :construction: [#15] Keep structure of expanded data from OZ --- backend/src/openarchiefbeheer/conf/base.py | 1 + .../openarchiefbeheer/zaken/api/filtersets.py | 14 +++++---- .../zaken/api/serializers.py | 1 + ...k__expand_alter_zaak_resultaat_and_more.py | 30 +++++++++++++++++++ backend/src/openarchiefbeheer/zaken/models.py | 5 ++-- backend/src/openarchiefbeheer/zaken/utils.py | 18 ++++------- 6 files changed, 49 insertions(+), 20 deletions(-) create mode 100644 backend/src/openarchiefbeheer/zaken/migrations/0003_zaak__expand_alter_zaak_resultaat_and_more.py diff --git a/backend/src/openarchiefbeheer/conf/base.py b/backend/src/openarchiefbeheer/conf/base.py index da887699b..309ec4954 100644 --- a/backend/src/openarchiefbeheer/conf/base.py +++ b/backend/src/openarchiefbeheer/conf/base.py @@ -537,6 +537,7 @@ "djangorestframework_camel_case.parser.CamelCaseMultiPartParser", ], "DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema", + "JSON_UNDERSCOREIZE": {"ignore_keys": ("_expand",)}, } diff --git a/backend/src/openarchiefbeheer/zaken/api/filtersets.py b/backend/src/openarchiefbeheer/zaken/api/filtersets.py index 8060eae6c..dab1a71f8 100644 --- a/backend/src/openarchiefbeheer/zaken/api/filtersets.py +++ b/backend/src/openarchiefbeheer/zaken/api/filtersets.py @@ -19,7 +19,7 @@ class ZaakFilter(FilterSet): "If True, only cases not already included in a destruction list are returned." ), ) - resultaat__resultaattype__url = CharFilter( + _expand__resultaat__resultaattype = CharFilter( help_text=_("Filter on the exact URL of resultaattype."), ) bewaartermijn = CharFilter( @@ -36,7 +36,7 @@ class ZaakFilter(FilterSet): method="filter_vcs", help_text=_( "Filter on VCS. This stands for 'Vernietigings-Categorie Selectielijst'. " - "It is obtained through 'zaak.zaaktype.procestype.nummer'." + "It is obtained through 'zaaktype.procestype.nummer'." ), decimal_places=0, ) @@ -45,7 +45,7 @@ class ZaakFilter(FilterSet): method="filter_heeft_relaties", help_text=_( "Filter on whether this case has other related cases. " - "This is done by looking at the property 'zaak.relevanteAndereZaken'." + "This is done by looking at the property 'relevanteAndereZaken'." ), ) @@ -112,12 +112,16 @@ def filter_bewaartermijn( self, queryset: QuerySet[Zaak], name: str, value: str ) -> QuerySet[Zaak]: # TODO it would be nice to do comparisons for periods such as gt/lt - return queryset.filter(resultaat__resultaattype__archiefactietermijn=value) + return queryset.filter( + _expand__resultaat___expand__resultaattype__archiefactietermijn=value + ) def filter_vcs( self, queryset: QuerySet[Zaak], name: str, value: Decimal ) -> QuerySet[Zaak]: - return queryset.filter(zaaktype__selectielijst_procestype__nummer=int(value)) + return queryset.filter( + _expand__zaaktype__selectielijst_procestype__nummer=int(value) + ) def filter_heeft_relaties( self, queryset: QuerySet[Zaak], name: str, value: bool diff --git a/backend/src/openarchiefbeheer/zaken/api/serializers.py b/backend/src/openarchiefbeheer/zaken/api/serializers.py index bb7386e6c..f19712194 100644 --- a/backend/src/openarchiefbeheer/zaken/api/serializers.py +++ b/backend/src/openarchiefbeheer/zaken/api/serializers.py @@ -48,4 +48,5 @@ class Meta: "vertrouwelijkheidaanduiding", "uiterlijke_einddatum_afdoening", "verantwoordelijke_organisatie", + "_expand", ) diff --git a/backend/src/openarchiefbeheer/zaken/migrations/0003_zaak__expand_alter_zaak_resultaat_and_more.py b/backend/src/openarchiefbeheer/zaken/migrations/0003_zaak__expand_alter_zaak_resultaat_and_more.py new file mode 100644 index 000000000..ab345d50a --- /dev/null +++ b/backend/src/openarchiefbeheer/zaken/migrations/0003_zaak__expand_alter_zaak_resultaat_and_more.py @@ -0,0 +1,30 @@ +# Generated by Django 4.2.11 on 2024-05-14 12:03 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("zaken", "0002_alter_zaak_resultaat_and_zaak_zaaktype"), + ] + + operations = [ + migrations.AddField( + model_name="zaak", + name="_expand", + field=models.JSONField( + blank=True, default=dict, null=True, verbose_name="expand" + ), + ), + migrations.AlterField( + model_name="zaak", + name="resultaat", + field=models.URLField(blank=True, null=True, verbose_name="resultaat"), + ), + migrations.AlterField( + model_name="zaak", + name="zaaktype", + field=models.URLField(blank=True, null=True, verbose_name="zaaktype"), + ), + ] diff --git a/backend/src/openarchiefbeheer/zaken/models.py b/backend/src/openarchiefbeheer/zaken/models.py index e577a369c..3acdc52c6 100644 --- a/backend/src/openarchiefbeheer/zaken/models.py +++ b/backend/src/openarchiefbeheer/zaken/models.py @@ -10,7 +10,7 @@ class Zaak(models.Model): models.URLField("rollen", max_length=1000, blank=True), null=True, blank=True ) status = models.URLField("status", max_length=1000, blank=True, null=True) - zaaktype = models.JSONField("zaaktype", blank=True, null=True) + zaaktype = models.URLField("zaaktype", blank=True, null=True) deelzaken = ArrayField( models.URLField("deelzaken", max_length=1000, blank=True), null=True, @@ -19,7 +19,7 @@ class Zaak(models.Model): einddatum = models.DateField("einddatum", blank=True, null=True) hoofdzaak = models.URLField("hoofdzaak", max_length=1000, blank=True, null=True) kenmerken = models.JSONField("kenmerken", blank=True, null=True) - resultaat = models.JSONField("resultaat", blank=True, null=True) + resultaat = models.URLField("resultaat", blank=True, null=True) startdatum = models.DateField("startdatum") verlenging = models.JSONField("verlenging", blank=True, null=True) opschorting = models.JSONField("opschorting", blank=True, null=True) @@ -93,6 +93,7 @@ class Zaak(models.Model): verantwoordelijke_organisatie = models.CharField( "verantwoordelijke organisatie", max_length=9 ) + _expand = models.JSONField("expand", blank=True, null=True, default=dict) class Meta: verbose_name = "Zaak" diff --git a/backend/src/openarchiefbeheer/zaken/utils.py b/backend/src/openarchiefbeheer/zaken/utils.py index 5078321cb..7dcaca220 100644 --- a/backend/src/openarchiefbeheer/zaken/utils.py +++ b/backend/src/openarchiefbeheer/zaken/utils.py @@ -44,27 +44,19 @@ def get_procestype(url: str) -> dict | None: def process_expanded_data(zaken: list[dict]) -> list[dict]: - def _format_expanded_data(zaak: dict) -> dict: + def expand_procestype(zaak: dict) -> dict: if "_expand" not in zaak: return zaak extra_data = zaak["_expand"] - - zaak["zaaktype"] = extra_data["zaaktype"] - if procestype_url := zaak["zaaktype"].get("selectielijst_procestype"): + if procestype_url := extra_data["zaaktype"].get("selectielijst_procestype"): expanded_procestype = get_procestype(procestype_url) if expanded_procestype is not None: - zaak["zaaktype"]["selectielijst_procestype"] = expanded_procestype - - if "resultaat" in extra_data: - resultaat = extra_data["resultaat"] - resultaat_extra_data = resultaat.pop("_expand") - resultaat["resultaattype"] = resultaat_extra_data["resultaattype"] - zaak["resultaat"] = resultaat + extra_data["zaaktype"]["selectielijst_procestype"] = expanded_procestype return zaak with parallel() as executor: - zaken_with_expanded_info = list(executor.map(_format_expanded_data, zaken)) + processed_zaken = list(executor.map(expand_procestype, zaken)) - return zaken_with_expanded_info + return processed_zaken From 820b42132dfeb84e9788e45c221a375d4f1ff41b Mon Sep 17 00:00:00 2001 From: SilviaAmAm Date: Tue, 14 May 2024 13:28:02 +0200 Subject: [PATCH 2/2] :white_check_mark: [#15] Update tests --- .../zaken/tests/test_tasks.py | 14 ++++--- .../zaken/tests/test_viewsets.py | 41 ++++++++++++++----- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/backend/src/openarchiefbeheer/zaken/tests/test_tasks.py b/backend/src/openarchiefbeheer/zaken/tests/test_tasks.py index 1248d709e..992da40b0 100644 --- a/backend/src/openarchiefbeheer/zaken/tests/test_tasks.py +++ b/backend/src/openarchiefbeheer/zaken/tests/test_tasks.py @@ -179,19 +179,23 @@ def test_expanded_correctly(self, m): zaak_with_resultaat = Zaak.objects.get(identificatie="ZAAK-01") - self.assertEqual(zaak_with_resultaat.resultaat["toelichting"], "Test result") self.assertEqual( - zaak_with_resultaat.resultaat["resultaattype"], + zaak_with_resultaat._expand["resultaat"]["toelichting"], "Test result" + ) + self.assertEqual( + zaak_with_resultaat._expand["resultaat"]["_expand"]["resultaattype"], { "url": "http://catalogue-api.nl/catalogi/api/v1/resultaattypen/bd84c463-fa65-46ef-8a9e-dd887e005aea", }, ) self.assertEqual( - zaak_with_resultaat.zaaktype["url"], + zaak_with_resultaat._expand["zaaktype"]["url"], "http://catalogue-api.nl/zaaktypen/111-111-111", ) self.assertEqual( - zaak_with_resultaat.zaaktype["selectielijst_procestype"]["nummer"], + zaak_with_resultaat._expand["zaaktype"]["selectielijst_procestype"][ + "nummer" + ], 1, ) @@ -244,6 +248,6 @@ def test_expand_no_selectielijst_service(self, m): zaak = Zaak.objects.get(identificatie="ZAAK-01") self.assertEqual( - zaak.zaaktype["selectielijst_procestype"], + zaak._expand["zaaktype"]["selectielijst_procestype"], "https://selectielijst.openzaak.nl/api/v1/procestypen/e1b73b12-b2f6-4c4e-8929-94f84dd2a57d", ) diff --git a/backend/src/openarchiefbeheer/zaken/tests/test_viewsets.py b/backend/src/openarchiefbeheer/zaken/tests/test_viewsets.py index 59f373fb5..70aebab2c 100644 --- a/backend/src/openarchiefbeheer/zaken/tests/test_viewsets.py +++ b/backend/src/openarchiefbeheer/zaken/tests/test_viewsets.py @@ -94,9 +94,14 @@ def test_using_query_params_to_filter(self): def test_filter_resultaattype(self): zaak_1 = ZaakFactory.create( - resultaat={ - "resultaattype": { - "url": "http://catalogue-api.nl/catalogi/api/v1/resultaattypen/111-111-111" + _expand={ + "resultaat": { + "resultaattype": "http://catalogue-api.nl/catalogi/api/v1/resultaattypen/111-111-111", + "_expand": { + "resultaattype": { + "url": "http://catalogue-api.nl/catalogi/api/v1/resultaattypen/111-111-111" + } + }, } } ) @@ -105,9 +110,14 @@ def test_filter_resultaattype(self): ) # Not expanded ZaakFactory.create_batch( 2, - resultaat={ - "resultaattype": { - "url": "http://catalogue-api.nl/catalogi/api/v1/resultaattypen/222-222-222" + _expand={ + "resultaat": { + "resultaattype": "http://catalogue-api.nl/catalogi/api/v1/resultaattypen/222-222-222", + "_expand": { + "resultaattype": { + "url": "http://catalogue-api.nl/catalogi/api/v1/resultaattypen/222-222-222" + } + }, } }, ) @@ -115,7 +125,7 @@ def test_filter_resultaattype(self): user = UserFactory(username="record_manager", role__can_start_destruction=True) endpoint = furl(reverse("api:zaken-list")) - endpoint.args["resultaat__resultaattype__url"] = ( + endpoint.args["_expand__resultaat__resultaattype"] = ( "http://catalogue-api.nl/catalogi/api/v1/resultaattypen/111-111-111" ) @@ -129,13 +139,22 @@ def test_filter_resultaattype(self): def test_filter_bewaartermijn(self): zaak_1 = ZaakFactory.create( - resultaat={"resultaattype": {"archiefactietermijn": "P1D"}} + _expand={ + "resultaat": { + "_expand": {"resultaattype": {"archiefactietermijn": "P1D"}} + } + } ) ZaakFactory.create( resultaat="http://zaken-api.nl/zaken/api/v1/resultaten/111-111-111" ) # Not expanded ZaakFactory.create_batch( - 2, resultaat={"resultaattype": {"archiefactietermijn": "P2D"}} + 2, + _expand={ + "resultaat": { + "_expand": {"resultaattype": {"archiefactietermijn": "P2D"}} + } + }, ) user = UserFactory(username="record_manager", role__can_start_destruction=True) @@ -152,13 +171,13 @@ def test_filter_bewaartermijn(self): def test_filter_vcs(self): zaak_1 = ZaakFactory.create( - zaaktype={"selectielijst_procestype": {"nummer": 1}} + _expand={"zaaktype": {"selectielijst_procestype": {"nummer": 1}}} ) ZaakFactory.create( zaaktype="http://catalogue-api.nl/zaaktypen/111-111-111", ) # Not expanded ZaakFactory.create_batch( - 2, zaaktype={"selectielijst_procestype": {"nummer": 2}} + 2, _expand={"zaaktype": {"selectielijst_procestype": {"nummer": 2}}} ) user = UserFactory(username="record_manager", role__can_start_destruction=True)