From b8f302003c6c9eda078eceea16e3dd0546b6aea2 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Fri, 8 Mar 2024 11:02:06 +0100 Subject: 🏷️ update french states and departments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../migrations/0241_migrate_old_department.py | 50 ++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 ishtar_common/migrations/0241_migrate_old_department.py (limited to 'ishtar_common/migrations/0241_migrate_old_department.py') diff --git a/ishtar_common/migrations/0241_migrate_old_department.py b/ishtar_common/migrations/0241_migrate_old_department.py new file mode 100644 index 000000000..4e5c2432a --- /dev/null +++ b/ishtar_common/migrations/0241_migrate_old_department.py @@ -0,0 +1,50 @@ +# Generated by Django 2.2.24 on 2024-03-07 17:52 + +from django.db import migrations +from django.db.models.functions import Length +from ishtar_common.data import FRENCH_STATES, FRENCH_TOM_STATES + + +def migrate(apps, __): + Area = apps.get_model('ishtar_common', 'Area') + Department = apps.get_model('ishtar_common', 'Department') + State = apps.get_model('ishtar_common', 'State') + Town = apps.get_model('ishtar_common', 'Town') + + if not State.objects.filter(label="Nord-Pas-de-Calais").count(): + # already OK or not french + return + + State.objects.filter(pk__isnull=False).delete() + Area.objects.filter(txt_idx__startswith="dep-").delete() + Area.objects.filter(txt_idx__startswith="state-").delete() + + for lbl, code, departments in (FRENCH_STATES + FRENCH_TOM_STATES): + state = State.objects.create(label=lbl, number=code) + state_area = Area.objects.create(label=lbl, txt_idx=f'state-{code}') + for dep_code in departments: + q = Department.objects.filter(number=dep_code) + if not q.count(): + print(f"{dep_code} department is missing") + continue + dep = q.all()[0] + dep.state = state + dep.save() + dep_area = Area.objects.create(label=dep.label, txt_idx=f'dep-{dep.number}') + dep_area.parent = state_area + dep_area.save() + dep_area.towns.clear() + q2 = Town.objects.annotate(insee_len=Length('numero_insee')).filter( + numero_insee__startswith=dep_code, insee_len=5) + dep_area.towns.add(*list(q2.all())) + + +class Migration(migrations.Migration): + + dependencies = [ + ('ishtar_common', '0240_state_number_max_len'), + ] + + operations = [ + migrations.RunPython(migrate) + ] -- cgit v1.2.3