Skip to content

Commit

Permalink
Merge pull request #16 from maykinmedia/feature/15-expand-zaaktype-an…
Browse files Browse the repository at this point in the history
…d-resultaat

[#15] Expand zaaktype (including procestype) and resultaat
  • Loading branch information
svenvandescheur authored May 14, 2024
2 parents 65f561e + c71d1d6 commit afd7eaa
Show file tree
Hide file tree
Showing 7 changed files with 430 additions and 11 deletions.
64 changes: 59 additions & 5 deletions backend/src/openarchiefbeheer/zaken/api/filtersets.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from django.db.models import Q, QuerySet, Subquery
from decimal import Decimal

from django.db.models import Func, IntegerField, Q, QuerySet, Subquery
from django.utils.translation import gettext_lazy as _

from django_filters import BooleanFilter, FilterSet
from django_filters import BooleanFilter, CharFilter, FilterSet, NumberFilter

from openarchiefbeheer.destruction.constants import ListItemStatus
from openarchiefbeheer.destruction.models import DestructionListItem
Expand All @@ -17,17 +19,44 @@ class ZaakFilter(FilterSet):
"If True, only cases not already included in a destruction list are returned."
),
)
resultaat__resultaattype__url = CharFilter(
help_text=_("Filter on the exact URL of resultaattype."),
)
bewaartermijn = CharFilter(
field_name="bewaartermijn",
method="filter_bewaartermijn",
help_text=_(
"Filter on bewaartermijn. "
"This corresponds to the property 'resultaat.resultaattype.archiefactietermijn'. "
"This field is expressed in Open Zaak as a ISO8601 duration."
),
)
vcs = NumberFilter(
field_name="vcs",
method="filter_vcs",
help_text=_(
"Filter on VCS. This stands for 'Vernietigings-Categorie Selectielijst'. "
"It is obtained through 'zaak.zaaktype.procestype.nummer'."
),
decimal_places=0,
)
heeft_relaties = BooleanFilter(
field_name="heeft_relaties",
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'."
),
)

class Meta:
model = Zaak
fields = {
"uuid": ["exact"],
"url": ["exact"],
"status": ["exact"],
"zaaktype": ["exact", "in"],
"einddatum": ["exact", "gt", "lt", "isnull"],
"hoofdzaak": ["exact"],
"resultaat": ["exact"],
"startdatum": ["exact", "gt", "lt", "gte", "lte"],
"toelichting": ["exact"],
"omschrijving": ["exact"],
Expand Down Expand Up @@ -63,7 +92,6 @@ class Meta:
# "verlenging": ["exact"],
# "opschorting": ["exact"],
# "processobject": ["exact"],
# "relevante_andere_zaken": ["exact"],
# # Geometry Field
# "zaakgeometrie": ["exact"],
}
Expand All @@ -79,3 +107,29 @@ def filter_not_in_destruction_list(
).values_list("zaak", flat=True)

return queryset.exclude(url__in=Subquery(zaken_to_exclude))

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)

def filter_vcs(
self, queryset: QuerySet[Zaak], name: str, value: Decimal
) -> QuerySet[Zaak]:
return queryset.filter(zaaktype__selectielijst_procestype__nummer=int(value))

def filter_heeft_relaties(
self, queryset: QuerySet[Zaak], name: str, value: bool
) -> QuerySet[Zaak]:
annotated_zaken = queryset.annotate(
number_relations=Func(
"relevante_andere_zaken",
function="jsonb_array_length",
output_field=IntegerField(),
)
)
if value:
return annotated_zaken.filter(number_relations__gt=0)

return annotated_zaken.filter(number_relations=0)
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by Django 4.2.11 on 2024-05-13 14:11

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("zaken", "0001_initial"),
]

operations = [
migrations.RemoveField(
model_name="zaak",
name="resultaat",
),
migrations.RemoveField(
model_name="zaak",
name="zaaktype",
),
migrations.AddField(
model_name="zaak",
name="resultaat",
field=models.JSONField(blank=True, null=True, verbose_name="resultaat"),
),
migrations.AddField(
model_name="zaak",
name="zaaktype",
field=models.JSONField(blank=True, null=True, verbose_name="zaaktype"),
),
]
4 changes: 2 additions & 2 deletions backend/src/openarchiefbeheer/zaken/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.URLField("zaaktype", max_length=1000)
zaaktype = models.JSONField("zaaktype", blank=True, null=True)
deelzaken = ArrayField(
models.URLField("deelzaken", max_length=1000, blank=True),
null=True,
Expand All @@ -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.URLField("resultaat", max_length=1000, blank=True, null=True)
resultaat = models.JSONField("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)
Expand Down
6 changes: 4 additions & 2 deletions backend/src/openarchiefbeheer/zaken/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from .api.serializers import ZaakSerializer
from .models import Zaak
from .utils import pagination_helper
from .utils import pagination_helper, process_expanded_data


@app.task
Expand All @@ -20,6 +20,7 @@ def retrieve_and_cache_zaken_from_openzaak() -> None:
response = zrc_client.get(
"zaken",
headers={"Accept-Crs": "EPSG:4326"},
params={"expand": "resultaat,resultaat.resultaattype,zaaktype"},
)
response.raise_for_status()

Expand All @@ -30,6 +31,7 @@ def retrieve_and_cache_zaken_from_openzaak() -> None:
Zaak.objects.all().delete()

for data in data_iterator:
serializer = ZaakSerializer(data=data["results"], many=True)
zaken = process_expanded_data(data["results"])
serializer = ZaakSerializer(data=zaken, many=True)
serializer.is_valid(raise_exception=True)
serializer.save()
165 changes: 163 additions & 2 deletions backend/src/openarchiefbeheer/zaken/tests/test_tasks.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from django.test import TestCase
from django.test import TestCase, TransactionTestCase

from requests_mock import Mocker
from zgw_consumers.constants import APITypes
from zgw_consumers.test.factories import ServiceFactory

from ..models import Zaak
from ..tasks import retrieve_and_cache_zaken_from_openzaak
from ..utils import get_procestype
from .factories import ZaakFactory

PAGE_1 = {
Expand All @@ -14,6 +15,7 @@
"identificatie": "ZAAK-01",
"url": "http://zaken-api.nl/zaken/api/v1/zaken/75f4c682-1e16-45ea-8f78-99b4474986ac",
"uuid": "75f4c682-1e16-45ea-8f78-99b4474986ac",
"resultaat": "http://zaken-api.nl/zaken/api/v1/resultaten/ffaa6410-0319-4a6b-b65a-fb209798e81c",
"startdatum": "2020-02-01",
"zaaktype": "http://catalogue-api.nl/zaaktypen/111-111-111",
"bronorganisatie": "000000000",
Expand Down Expand Up @@ -62,7 +64,7 @@


@Mocker()
class TasksTest(TestCase):
class RetrieveCachedZakenTest(TestCase):
@classmethod
def setUpTestData(cls):
super().setUpTestData()
Expand All @@ -86,3 +88,162 @@ def test_retrieve_and_cache_zaken(self, m):
zaken = Zaak.objects.all()

self.assertEqual(zaken.count(), 4)


PAGE_WITH_EXPAND = {
"results": [
{
"identificatie": "ZAAK-01",
"url": "http://zaken-api.nl/zaken/api/v1/zaken/75f4c682-1e16-45ea-8f78-99b4474986ac",
"uuid": "75f4c682-1e16-45ea-8f78-99b4474986ac",
"resultaat": "http://zaken-api.nl/zaken/api/v1/resultaten/ffaa6410-0319-4a6b-b65a-fb209798e81c",
"startdatum": "2020-02-01",
"zaaktype": "http://catalogue-api.nl/zaaktypen/111-111-111",
"bronorganisatie": "000000000",
"verantwoordelijkeOrganisatie": "000000000",
"_expand": {
"zaaktype": {
"url": "http://catalogue-api.nl/zaaktypen/111-111-111",
"selectielijstProcestype": "https://selectielijst.openzaak.nl/api/v1/procestypen/e1b73b12-b2f6-4c4e-8929-94f84dd2a57d",
},
"resultaat": {
"url": "http://zaken-api.nl/zaken/api/v1/resultaten/ffaa6410-0319-4a6b-b65a-fb209798e81c",
"resultaattype": "http://catalogue-api.nl/catalogi/api/v1/resultaattypen/bd84c463-fa65-46ef-8a9e-dd887e005aea",
"toelichting": "Test result",
"_expand": {
"resultaattype": {
"url": "http://catalogue-api.nl/catalogi/api/v1/resultaattypen/bd84c463-fa65-46ef-8a9e-dd887e005aea",
},
},
},
},
},
{
"identificatie": "ZAAK-02",
"url": "http://zaken-api.nl/zaken/api/v1/zaken/79dbdbb6-b903-4655-84de-d0b9e106b781",
"uuid": "79dbdbb6-b903-4655-84de-d0b9e106b781",
"startdatum": "2020-02-01",
"zaaktype": "http://catalogue-api.nl/zaaktypen/111-111-111",
"bronorganisatie": "000000000",
"verantwoordelijkeOrganisatie": "000000000",
"resultaat": "http://zaken-api.nl/zaken/api/v1/resultaten/ffaa6410-0319-4a6b-b65a-fb209798e81c",
"_expand": {
"zaaktype": {
"url": "http://catalogue-api.nl/zaaktypen/111-111-111",
},
},
},
{
"identificatie": "ZAAK-03",
"url": "http://zaken-api.nl/zaken/api/v1/zaken/89dbdbb6-b903-4655-84de-d0b9e106b781",
"uuid": "89dbdbb6-b903-4655-84de-d0b9e106b781",
"startdatum": "2020-02-01",
"zaaktype": "http://catalogue-api.nl/zaaktypen/111-111-111",
"bronorganisatie": "000000000",
"verantwoordelijkeOrganisatie": "000000000",
},
],
"count": 3,
"previous": None,
"next": None,
}


@Mocker()
class RetrieveCachedZakenWithProcestypeTest(TransactionTestCase):
def setUp(self):
super().setUp()

self.addCleanup(get_procestype.cache_clear)

def test_expanded_correctly(self, m):
ServiceFactory.create(
api_type=APITypes.zrc,
api_root="http://zaken-api.nl/zaken/api/v1",
)
ServiceFactory.create(
api_type=APITypes.orc,
api_root="https://selectielijst.openzaak.nl/api/v1/",
)

m.get("http://zaken-api.nl/zaken/api/v1/zaken", json=PAGE_WITH_EXPAND)
m.get(
"https://selectielijst.openzaak.nl/api/v1/procestypen/e1b73b12-b2f6-4c4e-8929-94f84dd2a57d",
json={
"url": "https://selectielijst.openzaak.nl/api/v1/procestypen/e1b73b12-b2f6-4c4e-8929-94f84dd2a57d",
"nummer": 1,
},
)

retrieve_and_cache_zaken_from_openzaak()

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"],
{
"url": "http://catalogue-api.nl/catalogi/api/v1/resultaattypen/bd84c463-fa65-46ef-8a9e-dd887e005aea",
},
)
self.assertEqual(
zaak_with_resultaat.zaaktype["url"],
"http://catalogue-api.nl/zaaktypen/111-111-111",
)
self.assertEqual(
zaak_with_resultaat.zaaktype["selectielijst_procestype"]["nummer"],
1,
)

zaak_without_expanded_resultaat = Zaak.objects.get(identificatie="ZAAK-02")

self.assertEqual(
zaak_without_expanded_resultaat.resultaat,
"http://zaken-api.nl/zaken/api/v1/resultaten/ffaa6410-0319-4a6b-b65a-fb209798e81c",
)

zaak_without_expand = Zaak.objects.get(identificatie="ZAAK-03")

self.assertEqual(
zaak_without_expand.zaaktype,
"http://catalogue-api.nl/zaaktypen/111-111-111",
)

def test_expand_no_selectielijst_service(self, m):
ServiceFactory.create(
api_type=APITypes.zrc,
api_root="http://zaken-api.nl/zaken/api/v1",
)

m.get(
"http://zaken-api.nl/zaken/api/v1/zaken",
json={
"results": [
{
"identificatie": "ZAAK-01",
"url": "http://zaken-api.nl/zaken/api/v1/zaken/75f4c682-1e16-45ea-8f78-99b4474986ac",
"uuid": "75f4c682-1e16-45ea-8f78-99b4474986ac",
"resultaat": "http://zaken-api.nl/zaken/api/v1/resultaten/ffaa6410-0319-4a6b-b65a-fb209798e81c",
"startdatum": "2020-02-01",
"zaaktype": "http://catalogue-api.nl/zaaktypen/111-111-111",
"bronorganisatie": "000000000",
"verantwoordelijkeOrganisatie": "000000000",
"_expand": {
"zaaktype": {
"url": "http://catalogue-api.nl/zaaktypen/111-111-111",
"selectielijstProcestype": "https://selectielijst.openzaak.nl/api/v1/procestypen/e1b73b12-b2f6-4c4e-8929-94f84dd2a57d",
},
},
}
]
},
)

retrieve_and_cache_zaken_from_openzaak()

zaak = Zaak.objects.get(identificatie="ZAAK-01")

self.assertEqual(
zaak.zaaktype["selectielijst_procestype"],
"https://selectielijst.openzaak.nl/api/v1/procestypen/e1b73b12-b2f6-4c4e-8929-94f84dd2a57d",
)
Loading

0 comments on commit afd7eaa

Please sign in to comment.