Skip to content

Commit

Permalink
Merge pull request #1538 from open-zaak/feature/1531-import-zaaktype-…
Browse files Browse the repository at this point in the history
…under-different-name

✨[#1531] Add ability to import zaaktype under different iden…
  • Loading branch information
Coperh committed Feb 6, 2024
2 parents e08dbbc + 39a06b3 commit ce24ee6
Show file tree
Hide file tree
Showing 5 changed files with 231 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/openzaak/components/catalogi/admin/admin_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copyright (C) 2019 - 2020 Dimpact
from django.contrib import messages
from django.contrib.auth.mixins import PermissionRequiredMixin
from django.core.exceptions import ValidationError
from django.core.management import CommandError
from django.db import transaction
from django.db.utils import IntegrityError
Expand Down Expand Up @@ -40,6 +41,10 @@ def post(self, request, *args, **kwargs):
import_file = form.cleaned_data["file"]
request.session["file_content"] = import_file.read()

request.session["identificatie_prefix"] = request.POST.get(
"identificatie_prefix"
)

iotypen = retrieve_iotypen(catalogus_pk, request.session["file_content"])
request.session["iotypen"] = (
sorted(iotypen, key=lambda x: x["omschrijving"]) if iotypen else iotypen
Expand All @@ -65,7 +70,11 @@ def post(self, request, *args, **kwargs):
try:
with transaction.atomic():
import_zaaktype_for_catalogus(
catalogus_pk, request.session["file_content"], {}, {}
request.session["identificatie_prefix"],
catalogus_pk,
request.session["file_content"],
{},
{},
)

messages.add_message(
Expand Down Expand Up @@ -195,6 +204,7 @@ def post(self, request, *args, **kwargs):
)

import_zaaktype_for_catalogus(
request.session["identificatie_prefix"],
kwargs["catalogus_pk"],
request.session["file_content"],
iotypen_uuid_mapping,
Expand All @@ -205,7 +215,7 @@ def post(self, request, *args, **kwargs):
request, messages.SUCCESS, _("ZaakType successfully imported")
)
return HttpResponseRedirect(reverse("admin:catalogi_catalogus_changelist"))
except (CommandError, IntegrityError) as exc:
except (CommandError, IntegrityError, ValidationError) as exc:
messages.add_message(request, messages.ERROR, exc)

return TemplateResponse(request, self.template_name, context)
9 changes: 9 additions & 0 deletions src/openzaak/components/catalogi/admin/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
BrondatumArchiefprocedureAfleidingswijze as Afleidingswijze,
)
from vng_api_common.tests import reverse as _reverse
from vng_api_common.validators import alphanumeric_excluding_diacritic
from zds_client import ClientError
from zgw_consumers.models import Service

Expand Down Expand Up @@ -485,6 +486,14 @@ class CatalogusImportForm(forms.Form):


class ZaakTypeImportForm(forms.Form):

identificatie_prefix = forms.CharField(
label=_("identificatie prefix"),
required=False,
help_text=_("Zaaktype identification prefix. Leave blank to use imported."),
validators=[alphanumeric_excluding_diacritic,],
)

file = forms.FileField(
label=_("bestand"),
required=True,
Expand Down
22 changes: 21 additions & 1 deletion src/openzaak/components/catalogi/admin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import json
import zipfile

from django.core.exceptions import ValidationError
from django.core.management import CommandError
from django.utils.translation import ngettext, ugettext_lazy as _

Expand Down Expand Up @@ -164,7 +165,11 @@ def construct_besluittypen(

@requests_cache_enabled("import", backend=DjangoRequestsCache())
def import_zaaktype_for_catalogus(
catalogus_pk, import_file_content, iotypen_uuid_mapping, besluittypen_uuid_mapping
identificatie_prefix,
catalogus_pk,
import_file_content,
iotypen_uuid_mapping,
besluittypen_uuid_mapping,
):
catalogus = Catalogus.objects.get(pk=catalogus_pk)
catalogus_uuid = str(catalogus.uuid)
Expand Down Expand Up @@ -201,6 +206,21 @@ def import_zaaktype_for_catalogus(

for entry in data:
if resource == "ZaakType":
if identificatie_prefix:

new_identification = (
f"{identificatie_prefix}_{entry['identificatie']}"
)

if len(new_identification) > 50:
raise ValidationError(
_(
"Identification {} is too long with prefix. Max 50 characters."
).format(new_identification)
)

entry["identificatie"] = new_identification

entry["informatieobjecttypen"] = []
old_catalogus_uuid = entry["catalogus"].split("/")[-1]
entry["catalogus"] = entry["catalogus"].replace(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1379,6 +1379,194 @@ def test_import_iotype_without_omschrijving_generiek(self, *mocks):

self.assertEqual(response.status_code, 302)

def test_import_zaaktype_with_no_identification_selected(self, *mocks):
catalogus = CatalogusFactory.create(rsin="000000000", domein="TEST")
zaaktype = ZaakTypeFactory.create(
catalogus=catalogus,
identificatie="ZAAKTYPE_1",
vertrouwelijkheidaanduiding="openbaar",
zaaktype_omschrijving="bla",
datum_begin_geldigheid="2023-01-01",
)

InformatieObjectTypeFactory.create(
catalogus=catalogus,
vertrouwelijkheidaanduiding="openbaar",
omschrijving="Alpha",
zaaktypen__zaaktype=zaaktype,
datum_begin_geldigheid="2023-01-01",
datum_einde_geldigheid="2023-03-31",
)

BesluitTypeFactory.create(
catalogus=catalogus,
omschrijving="Apple",
zaaktypen=[zaaktype],
datum_begin_geldigheid="2023-01-01",
datum_einde_geldigheid="2023-03-31",
)

# create zip
url = reverse("admin:catalogi_zaaktype_change", args=(zaaktype.pk,))
response = self.app.get(url)
form = response.forms["zaaktype_form"]
response = form.submit("_export")
data = response.content

url = reverse("admin:catalogi_catalogus_import_zaaktype", args=(catalogus.pk,))
response = self.app.get(url)

form = response.form
f = io.BytesIO(data)
f.name = "test.zip"
f.seek(0)
form["file"] = (
"test.zip",
f.read(),
)
form["identificatie_prefix"] = ""

zaaktype.delete()
self.assertFalse(ZaakType.objects.filter(identificatie="ZAAKTYPE_1").exists())

response = form.submit("_import_zaaktype").follow()
response = response.form.submit("_select")

self.assertEqual(response.status_code, 302)
self.assertTrue(ZaakType.objects.filter(identificatie="ZAAKTYPE_1").exists())

def test_import_zaaktype_with_different_identification(self, *mocks):
catalogus = CatalogusFactory.create(rsin="000000000", domein="TEST")
zaaktype = ZaakTypeFactory.create(
catalogus=catalogus,
identificatie="ZAAKTYPE_1",
vertrouwelijkheidaanduiding="openbaar",
zaaktype_omschrijving="bla",
datum_begin_geldigheid="2023-01-01",
)

InformatieObjectTypeFactory.create(
catalogus=catalogus,
vertrouwelijkheidaanduiding="openbaar",
omschrijving="Alpha",
zaaktypen__zaaktype=zaaktype,
datum_begin_geldigheid="2023-01-01",
datum_einde_geldigheid="2023-03-31",
)

BesluitTypeFactory.create(
catalogus=catalogus,
omschrijving="Apple",
zaaktypen=[zaaktype],
datum_begin_geldigheid="2023-01-01",
datum_einde_geldigheid="2023-03-31",
)

# create zip
url = reverse("admin:catalogi_zaaktype_change", args=(zaaktype.pk,))
response = self.app.get(url)
form = response.forms["zaaktype_form"]
response = form.submit("_export")
data = response.content

url = reverse("admin:catalogi_catalogus_import_zaaktype", args=(catalogus.pk,))
response = self.app.get(url)

form = response.form
f = io.BytesIO(data)
f.name = "test.zip"
f.seek(0)
form["file"] = (
"test.zip",
f.read(),
)

response = form.submit("_import_zaaktype").follow()
response = response.form.submit("_select")
# fails (dues to a overlap error)
self.assertEqual(response.status_code, 200)
self.assertEqual(ZaakType.objects.all().count(), 1)

response = self.app.get(url)
form = response.form
f.seek(0)
form["file"] = (
"test.zip",
f.read(),
)
form["identificatie_prefix"] = "PREFIX"
response = form.submit("_import_zaaktype").follow()

response = response.form.submit("_select")
# succeeds as it is imported under a different name
self.assertEqual(response.status_code, 302)
self.assertEqual(ZaakType.objects.all().count(), 2)
self.assertTrue(
ZaakType.objects.filter(identificatie="PREFIX_ZAAKTYPE_1").exists()
)

def test_import_zaaktype_with_different_identification_exceeds_max_length(
self, *mocks
):
catalogus = CatalogusFactory.create(rsin="000000000", domein="TEST")
zaaktype = ZaakTypeFactory.create(
catalogus=catalogus,
identificatie="Identification_that_is_fifty_characters_long_00000",
vertrouwelijkheidaanduiding="openbaar",
zaaktype_omschrijving="bla",
datum_begin_geldigheid="2023-01-01",
)

InformatieObjectTypeFactory.create(
catalogus=catalogus,
vertrouwelijkheidaanduiding="openbaar",
omschrijving="Alpha",
zaaktypen__zaaktype=zaaktype,
datum_begin_geldigheid="2023-01-01",
datum_einde_geldigheid="2023-03-31",
)

BesluitTypeFactory.create(
catalogus=catalogus,
omschrijving="Apple",
zaaktypen=[zaaktype],
datum_begin_geldigheid="2023-01-01",
datum_einde_geldigheid="2023-03-31",
)

# create zip
url = reverse("admin:catalogi_zaaktype_change", args=(zaaktype.pk,))
response = self.app.get(url)
form = response.forms["zaaktype_form"]
response = form.submit("_export")
data = response.content

url = reverse("admin:catalogi_catalogus_import_zaaktype", args=(catalogus.pk,))
response = self.app.get(url)

form = response.form
f = io.BytesIO(data)
f.name = "test.zip"
f.seek(0)
form["file"] = (
"test.zip",
f.read(),
)

form["identificatie_prefix"] = "PREFIX"
response = form.submit("_import_zaaktype").follow()

response = response.form.submit("_select")
self.assertEqual(response.status_code, 200)
self.assertEqual(ZaakType.objects.all().count(), 1)

self.assertIn(
_("Identification {} is too long with prefix. Max 50 characters.").format(
"PREFIX_" + "Identification_that_is_fifty_characters_long_00000"
),
response.text,
)


@patch(
"openzaak.components.catalogi.models.zaaktype.Service.get_client",
Expand Down
1 change: 1 addition & 0 deletions src/openzaak/templates/admin/catalogi/import_zaaktype.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ <h1>{% trans 'Importeer zaaktype' %}</h1>
<div>
<fieldset class="module aligned ">
{% csrf_token %}
{{ form.errors }}
{% for field in form %}
{{ field.label.capitalize }}: {{ field }}<br/>
{{ field.help_text }}<br/><br/>
Expand Down

0 comments on commit ce24ee6

Please sign in to comment.