-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#310] Add validate_bag_id + migrations
- Loading branch information
1 parent
5639fa0
commit 1a21eb9
Showing
16 changed files
with
716 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
...s/contactgegevens/migrations/0005_alter_organisatie_adres_nummeraanduiding_id_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# Generated by Django 4.2.17 on 2025-01-28 09:10 | ||
|
||
import logging | ||
import openklant.utils.validators | ||
from django.core.exceptions import ValidationError | ||
from django.db import IntegrityError | ||
from django.db import migrations, models | ||
from openklant.utils.validators import validate_bag_id | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def _check_records(records): | ||
total_failed_records = 0 | ||
for record in records: | ||
if record.adres_nummeraanduiding_id: | ||
try: | ||
validate_bag_id(record.adres_nummeraanduiding_id) | ||
except ValidationError: | ||
logger.warning( | ||
"%s(pk=%s, uuid=%s) Field: 'adres_nummeraanduiding_id'. Invalid BAG ID: %s", | ||
type(record).__qualname__, | ||
record.pk, | ||
record.uuid, | ||
record.adres_nummeraanduiding_id, | ||
) | ||
total_failed_records += 1 | ||
return total_failed_records | ||
|
||
|
||
def _check_records_field_length(apps, schema_editor): | ||
Organisatie = apps.get_model("contactgegevens", "Organisatie") | ||
Persoon = apps.get_model("contactgegevens", "Persoon") | ||
|
||
for model in [Organisatie, Persoon]: | ||
records = model.objects.all() | ||
if total_failed_records := _check_records(records): | ||
raise IntegrityError( | ||
"The migration cannot proceed due to %s records that don't comply with the %s model's requirements. " | ||
"Possible data inconsistency or mapping error." | ||
% (total_failed_records, model.__qualname__) | ||
) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
( | ||
"contactgegevens", | ||
"0004_alter_organisatie_adres_land_alter_organisatie_land_and_more", | ||
), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython( | ||
code=_check_records_field_length, | ||
reverse_code=migrations.RunPython.noop, | ||
), | ||
migrations.AlterField( | ||
model_name="organisatie", | ||
name="adres_nummeraanduiding_id", | ||
field=models.CharField( | ||
blank=True, | ||
help_text="Identificatie van het adres bij de Basisregistratie Adressen en Gebouwen.", | ||
max_length=16, | ||
validators=[ | ||
openklant.utils.validators.CustomRegexValidator( | ||
message="Ongeldige nummeraanduiding BAG-ID", regex="^[0-9]{16}$" | ||
) | ||
], | ||
verbose_name="nummeraanduiding ID", | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name="persoon", | ||
name="adres_nummeraanduiding_id", | ||
field=models.CharField( | ||
blank=True, | ||
help_text="Identificatie van het adres bij de Basisregistratie Adressen en Gebouwen.", | ||
max_length=16, | ||
validators=[ | ||
openklant.utils.validators.CustomRegexValidator( | ||
message="Ongeldige nummeraanduiding BAG-ID", regex="^[0-9]{16}$" | ||
) | ||
], | ||
verbose_name="nummeraanduiding ID", | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.