Skip to content

Commit

Permalink
[#2871] Refactor ZGW import logic
Browse files Browse the repository at this point in the history
    - overwrite only editable fields when importing ZGW entities,
      skip read-only fields (url, domein, rsin)
  • Loading branch information
pi-sigma committed Nov 15, 2024
1 parent 101011b commit 7bca41d
Show file tree
Hide file tree
Showing 2 changed files with 312 additions and 134 deletions.
165 changes: 164 additions & 1 deletion src/open_inwoner/openzaak/import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from django.core.files.storage import Storage
from django.db import transaction
from django.db.models import QuerySet
from django.db.utils import IntegrityError

from .models import (
CatalogusConfig,
Expand All @@ -22,6 +23,156 @@
logger = logging.getLogger(__name__)


def update_catalogus_config(source):
try:
target = CatalogusConfig.objects.get_by_natural_key(
domein=source.domein, rsin=source.rsin
)
except CatalogusConfig.MultipleObjectsReturned:
logger.error(
"Got multiple results for CatalogusConfig with domain=%s and rsin=%s ",
source.url,
source.domein,
source.rsin,
)
except CatalogusConfig.DoesNotExist:
try:
source.save()
except IntegrityError:
logger.error("CatalogusConfig with url %s already exists", source.url)
else:
target.service = source.service
target.save()


def update_zaaktype_config(source):
try:
target = ZaakTypeConfig.objects.get_by_natural_key(
identificatie=source.identificatie,
catalogus_domein=source.catalogus.domein,
catalogus_rsin=source.catalogus.rsin,
)
except ZaakTypeConfig.MultipleObjectsReturned:
logger.error(
"Got multiple results for ZaakTypeConfig with catalogus_domain=%s and catalogus_rsin=%s ",
source.catalogus.domein,
source.catalogu.rsin,
)
except ZaakTypeConfig.DoesNotExist:
source.save()
else:
update_fields = [
"omschrijving",
"notify_status_changes",
"external_document_upload_url",
"document_upload_enabled",
"contact_form_enabled",
"contact_subject_code",
"relevante_zaakperiode",
]
for field in update_fields:
val = getattr(source, field, None)
setattr(target, field, val)
target.save()


def update_zaaktype_informatie_objecttype_config(source):
try:
target = ZaakTypeInformatieObjectTypeConfig.objects.get_by_natural_key(
informatieobjecttype_url=source.informatieobjecttype_url,
zaak_type_config_identificatie=source.zaaktype_config.identificatie,
catalogus_domein=source.zaaktype_config.catalogus.domein,
catalogus_rsin=source.zaaktype_config.catalogus.rsin,
)
except ZaakTypeInformatieObjectTypeConfig.MultipleObjectsReturned:
logger.error(
"Got multiple results for ZaakTypeInformatieObjectTypeConfig with "
"catalogus_domain=%s and catalogus_rsin=%s ",
source.zaaktype_config.catalogus.domein,
source.zaaktype_config.catalogu.rsin,
)
except ZaakTypeInformatieObjectTypeConfig.DoesNotExist:
source.save()
else:
update_fields = [
"omschrijving",
"zaaktype_uuids",
"document_upload_enabled",
"document_notification_enabled",
]
for field in update_fields:
val = getattr(source, field, None)
setattr(target, field, val)
target.save()


def update_zaaktype_statustype_config(source):
try:
target = ZaakTypeStatusTypeConfig.objects.get_by_natural_key(
statustype_url=source.statustype_url,
zaak_type_config_identificatie=source.zaaktype_config.identificatie,
catalogus_domein=source.zaaktype_config.catalogus.domein,
catalogus_rsin=source.zaaktype_config.catalogus.rsin,
)
except ZaakTypeStatusTypeConfig.MultipleObjectsReturned:
logger.error(
"Got multiple results for ZaakTypeStatusTypeConfig with "
"catalogus_domain=%s and catalogus_rsin=%s ",
source.zaaktype_config.catalogus.domein,
source.zaaktype_config.catalogu.rsin,
)
except ZaakTypeStatusTypeConfig.DoesNotExist:
source.save()
else:
update_fields = [
"omschrijving",
"statustekst",
"zaaktype_uuids",
"status_indicator",
"status_indicator_text" "document_upload_description",
"desciption",
"notify_status_change",
"action_required",
"document_upload_enabled",
"call_to_action_url",
"call_to_action_text",
"case_link_text",
]
for field in update_fields:
val = getattr(source, field, None)
setattr(target, field, val)
target.save()


def update_zaaktype_resultaattype_config(source):
try:
target = ZaakTypeResultaatTypeConfig.objects.get_by_natural_key(
resultaattype_url=source.resultaattype_url,
zaak_type_config_identificatie=source.zaaktype_config.identificatie,
catalogus_domein=source.zaaktype_config.catalogus.domein,
catalogus_rsin=source.zaaktype_config.catalogus.rsin,
)
except ZaakTypeResultaatTypeConfig.MultipleObjectsReturned:
logger.error(
"Got multiple results for ZaakTypeResultaatTypeConfig with "
"catalogus_domain=%s and catalogus_rsin=%s ",
source.zaaktype_config.catalogus.domein,
source.zaaktype_config.catalogu.rsin,
)
except ZaakTypeResultaatTypeConfig.DoesNotExist:
source.save()
else:
update_fields = [
"omschrijving",
"zaaktype_uuids",
"description",
]
for field in update_fields:
val = getattr(source, field, None)
setattr(target, field, val)
target.save()


@dataclasses.dataclass(frozen=True)
class CatalogusConfigExport:
"""Gather and export CatalogusConfig(s) and all associated relations."""
Expand Down Expand Up @@ -227,7 +378,19 @@ def from_jsonl_stream_or_string(cls, stream_or_string: IO | str) -> Self:
use_natural_foreign_keys=True,
use_natural_primary_keys=True,
):
deserialized_object.save()
source = deserialized_object.object
match source:
case CatalogusConfig():
update_catalogus_config(source=source)
case ZaakTypeConfig():
update_zaaktype_config(source=source)
case ZaakTypeInformatieObjectTypeConfig():
update_zaaktype_informatie_objecttype_config(source=source)
case ZaakTypeStatusTypeConfig():
update_zaaktype_statustype_config(source=source)
case ZaakTypeResultaatTypeConfig():
update_zaaktype_resultaattype_config(source=source)

object_type = deserialized_object.object.__class__.__name__
object_type_counts[object_type] += 1

Expand Down
Loading

0 comments on commit 7bca41d

Please sign in to comment.