From 29fba52bfdb8967e46411287750a60fbc77212e2 Mon Sep 17 00:00:00 2001 From: Daniel Mursa Date: Fri, 10 Jan 2025 11:42:15 +0100 Subject: [PATCH] [#311] Upgrade migration --- .../components/contactgegevens/models.py | 2 +- ...er_betrokkene_bezoekadres_land_and_more.py | 60 +++++++++++++++++++ .../klantinteracties/models/mixins.py | 3 +- 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/src/openklant/components/contactgegevens/models.py b/src/openklant/components/contactgegevens/models.py index 81299793..0b8f04ff 100644 --- a/src/openklant/components/contactgegevens/models.py +++ b/src/openklant/components/contactgegevens/models.py @@ -1,6 +1,6 @@ import uuid -from django.core.validators import MinLengthValidator, validate_integer +from django.core.validators import MinLengthValidator from django.db import models from django.utils.translation import gettext_lazy as _ diff --git a/src/openklant/components/klantinteracties/migrations/0024_alter_betrokkene_bezoekadres_land_and_more.py b/src/openklant/components/klantinteracties/migrations/0024_alter_betrokkene_bezoekadres_land_and_more.py index 222151f0..4e5eb146 100644 --- a/src/openklant/components/klantinteracties/migrations/0024_alter_betrokkene_bezoekadres_land_and_more.py +++ b/src/openklant/components/klantinteracties/migrations/0024_alter_betrokkene_bezoekadres_land_and_more.py @@ -1,10 +1,66 @@ # Generated by Django 4.2.15 on 2025-01-09 15:19 +import logging import django.core.validators +import openklant.utils.validators from django.db import migrations, models import openklant.utils.validators +from openklant.utils.converters import nl_code_to_iso_code_country_converter + +logger = logging.getLogger(__name__) + + +def _update_records(records, name): + logger.info("Start updating records for %s", name) + for record in records: + updated = False + if record.bezoekadres_land: + value = nl_code_to_iso_code_country_converter(record.bezoekadres_land) + if not value: + logger.warning( + "No match found for the nl_code: %s for object %s with pk: %s and uuid %s", + record.bezoekadres_land, + name, + record.pk, + record.uuid, + ) + record.bezoekadres_land = value + updated = True + if record.correspondentieadres_land: + value = nl_code_to_iso_code_country_converter( + record.correspondentieadres_land + ) + if not value: + logger.warning( + "No match found for the nl_code: %s for object %s with pk: %s and uuid %s", + record.bezoekadres_land, + name, + record.pk, + record.uuid, + ) + record.correspondentieadres_land = value + updated = True + if updated: + logger.debug( + "Object %s pk: %s uuid %s updated", + name, + record.pk, + record.uuid, + ) + record.save() + logger.info("Records update finished") + + +def _update_country_code(apps, schema_editor): + Betrokkene = apps.get_model("klantinteracties", "Betrokkene") + Partij = apps.get_model("klantinteracties", "Partij") + + _update_records(Betrokkene.objects.all(), "Betrokkene") + _update_records(Partij.objects.all(), "Partij") + + class Migration(migrations.Migration): dependencies = [ @@ -12,6 +68,10 @@ class Migration(migrations.Migration): ] operations = [ + migrations.RunPython( + code=_update_country_code, + reverse_code=migrations.RunPython.noop, + ), migrations.AlterField( model_name="betrokkene", name="bezoekadres_land", diff --git a/src/openklant/components/klantinteracties/models/mixins.py b/src/openklant/components/klantinteracties/models/mixins.py index 82d5ec8a..d553e753 100644 --- a/src/openklant/components/klantinteracties/models/mixins.py +++ b/src/openklant/components/klantinteracties/models/mixins.py @@ -1,8 +1,9 @@ -from django.core.validators import MinLengthValidator, validate_integer +from django.core.validators import MinLengthValidator from django.db import models from django.utils.translation import gettext_lazy as _ from vng_api_common.descriptors import GegevensGroepType + from openklant.utils.validators import validate_country