summaryrefslogtreecommitdiff
path: root/ishtar_common/migrations/0241_migrate_old_department.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2024-03-08 11:02:06 +0100
committerÉtienne Loks <etienne.loks@iggdrasil.net>2024-03-08 11:02:06 +0100
commitb8f302003c6c9eda078eceea16e3dd0546b6aea2 (patch)
tree7c940a4d1f4572fcbe00612a506fa2ba61a416a3 /ishtar_common/migrations/0241_migrate_old_department.py
parenta3f6cb07e8e2340196530d9bca7466a83ab364d0 (diff)
downloadIshtar-b8f302003c6c9eda078eceea16e3dd0546b6aea2.tar.bz2
Ishtar-b8f302003c6c9eda078eceea16e3dd0546b6aea2.zip
🏷️ update french states and departments
Diffstat (limited to 'ishtar_common/migrations/0241_migrate_old_department.py')
-rw-r--r--ishtar_common/migrations/0241_migrate_old_department.py50
1 files changed, 50 insertions, 0 deletions
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)
+ ]