|
1 |
| -# Generated by Django 4.2.15 on 2025-01-09 15:19 |
| 1 | +# Generated by Django 4.2.17 on 2025-01-27 14:35 |
2 | 2 |
|
3 |
| -import logging |
4 | 3 | import django.core.validators
|
5 |
| -import openklant.utils.validators |
6 | 4 | from django.db import migrations, models
|
7 | 5 | import openklant.utils.validators
|
8 |
| -from django.core.management import CommandError |
9 |
| -from openklant.utils.constants import COUNTRIES_DICT |
10 |
| - |
11 |
| -logger = logging.getLogger(__name__) |
12 |
| - |
13 |
| -FIELDS = ["bezoekadres_land", "correspondentieadres_land"] |
14 |
| - |
15 |
| - |
16 |
| -def _check_records(records, model_name): |
17 |
| - total_failed_records = 0 |
18 |
| - for record in records: |
19 |
| - record_failed = False |
20 |
| - for field_name in FIELDS: |
21 |
| - field_value = getattr(record, field_name, None) |
22 |
| - iso_code = COUNTRIES_DICT.get(field_value, "") |
23 |
| - if field_value and not iso_code: |
24 |
| - record_failed = True |
25 |
| - logger.warning( |
26 |
| - "%s(pk=%s, uuid=%s) Field: '%s'. No match found for nl_code: '%s'", |
27 |
| - model_name, |
28 |
| - record.pk, |
29 |
| - record.uuid, |
30 |
| - field_name, |
31 |
| - field_value, |
32 |
| - ) |
33 |
| - |
34 |
| - if record_failed: |
35 |
| - total_failed_records += 1 |
36 |
| - return total_failed_records |
37 |
| - |
38 |
| - |
39 |
| -def _update_records(records): |
40 |
| - for record in records: |
41 |
| - updated = False |
42 |
| - for field_name in FIELDS: |
43 |
| - if field_value := getattr(record, field_name, None): |
44 |
| - iso_code = COUNTRIES_DICT.get(field_value, "") |
45 |
| - setattr(record, field_name, iso_code) |
46 |
| - updated = True |
47 |
| - |
48 |
| - if updated: |
49 |
| - record.save() |
50 |
| - |
51 |
| - |
52 |
| -def _check_and_update_records(apps, schema_editor): |
53 |
| - Betrokkene = apps.get_model("klantinteracties", "Betrokkene") |
54 |
| - Partij = apps.get_model("klantinteracties", "Partij") |
55 |
| - |
56 |
| - for model_name, model in [("Betrokkene", Betrokkene), ("Partij", Partij)]: |
57 |
| - records = model.objects.all() |
58 |
| - if total_failed_records := _check_records(records, model_name): |
59 |
| - raise CommandError( |
60 |
| - "The migration cannot proceed due to %s records that don't comply with the %s model's requirements. " |
61 |
| - "Possible data inconsistency or mapping error." |
62 |
| - % (total_failed_records, model_name) |
63 |
| - ) |
64 |
| - else: |
65 |
| - _update_records(records) |
66 | 6 |
|
67 | 7 |
|
68 | 8 | class Migration(migrations.Migration):
|
69 | 9 |
|
70 | 10 | dependencies = [
|
71 |
| - ("klantinteracties", "0023_alter_digitaaladres_omschrijving"), |
| 11 | + ( |
| 12 | + "klantinteracties", |
| 13 | + "0025_alter_partijidentificator_partij_identificator_code_objecttype_and_more", |
| 14 | + ), |
72 | 15 | ]
|
73 | 16 |
|
74 | 17 | operations = [
|
75 |
| - migrations.RunPython( |
76 |
| - code=_check_and_update_records, |
77 |
| - reverse_code=migrations.RunPython.noop, |
78 |
| - ), |
79 | 18 | migrations.AlterField(
|
80 | 19 | model_name="betrokkene",
|
81 | 20 | name="bezoekadres_land",
|
|
0 commit comments