Skip to content

Commit

Permalink
Add script to fix communes from 2021
Browse files Browse the repository at this point in the history
Add the field type_commune to fradm.Commune
  • Loading branch information
brmzkw committed Jul 5, 2024
1 parent 9e9ae64 commit 5693815
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 39,117 deletions.
2 changes: 1 addition & 1 deletion mesads/app/test_reversion_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class TestReversionDiff(TestCase):
def setUp(self):
self.user = User.objects.create_user(email="test@test.com")
self.commune = Commune.objects.create(
libelle="Melesse", insee="35173", departement="35"
type_commune="COM", libelle="Melesse", insee="35173", departement="35"
)
prefecture = Prefecture.objects.create(numero="35", libelle="Ille-et-Vilaine")
administrator = ADSManagerAdministrator.objects.create(prefecture=prefecture)
Expand Down
1 change: 1 addition & 0 deletions mesads/app/views/test_ads.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ def test_get_incorrect_ads_manager(self):
existing ADSManager, but not the one of the ADS, we want to make sure
the user is redirected to the correct page."""
commune = Commune.objects.create(
type_commune="COM",
insee="xx",
departement="xx",
libelle="xx",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# When MesADS started, we loaded the commune published by INSEE from the file
# https://www.insee.fr/fr/information/5057840. We didn't insert all the rows
# that were not of type "COM".
# This script loads all the other types.

import argparse
import csv
import sys
Expand All @@ -9,8 +14,6 @@


class Command(BaseCommand):
help = "Load communes from the CSV file published by INSEE (https://www.insee.fr/fr/information/5057840)"

def add_arguments(self, parser):
parser.add_argument("communes_file", type=argparse.FileType("r"))

Expand All @@ -23,20 +26,14 @@ def handle(self, *args, **options):
print(self.style.SUCCESS(f"\nCreated: {created} new entries"))

def insert_row(self, row):
if row["TYPECOM"] != "COM":
print(
self.style.SUCCESS(
"\nSkip insert of row of type %s: %s" % (row["TYPECOM"], row["NCC"])
)
)
if row["TYPECOM"] == "COM":
return 0

# The get_or_create below might fail in future updates if we attempt to
# reimport communes with different values for a row.
# If IntegrityError is raised, we need to find out why data has changed
# and change this loader accordingly.
commune, created = Commune.objects.get_or_create(
insee=row["COM"], departement=row["DEP"], libelle=row["LIBELLE"]
type_commune=row["TYPECOM"],
insee=row["COM"],
departement=row["DEP"],
libelle=row["LIBELLE"],
)
sys.stdout.write(self.style.SUCCESS("."))
sys.stdout.flush()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Generated by Django 5.0.6 on 2024-07-05 09:41

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("fradm", "0002_create_extension_unaccent"),
]

operations = [
migrations.AlterUniqueTogether(
name="commune",
unique_together=set(),
),
migrations.AddField(
model_name="commune",
name="type_commune",
field=models.CharField(
choices=[
("COM", "Commune"),
("COMA", "Commune associée"),
("COMD", "Commune déléguée"),
("ARM", "Arrondissement municipal"),
],
default="COM",
max_length=16,
verbose_name="Type de commune",
),
preserve_default=False,
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 5.0.6 on 2024-07-05 09:45

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("fradm", "0003_alter_commune_unique_together_commune_type_commune"),
]

operations = [
migrations.AlterField(
model_name="commune",
name="insee",
field=models.CharField(max_length=16),
),
migrations.AlterUniqueTogether(
name="commune",
unique_together={("type_commune", "insee")},
),
]
27 changes: 25 additions & 2 deletions mesads/fradm/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ def display_fulltext(self):
raise NotImplementedError


class CommuneManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(type_commune="COM")


class Commune(AdministrationModel):
"""Communes of France. Inserted by the django admin command
"load_communes".
Expand All @@ -39,8 +44,11 @@ class Commune(AdministrationModel):
CSV file provided by insee.
"""

objects = CommuneManager()
all_objects = models.Manager()

class Meta:
unique_together = (("departement", "libelle"),)
unique_together = (("type_commune", "insee"),)

def type_name(self):
return "commune"
Expand All @@ -59,7 +67,22 @@ def display_fulltext(self):
def __str__(self):
return f"{self.departement} - {self.libelle} (INSEE: {self.insee})"

insee = models.CharField(max_length=16, null=False, blank=False, unique=True)
TYPE_COMMUNE = (
("COM", "Commune"),
("COMA", "Commune associée"),
("COMD", "Commune déléguée"),
("ARM", "Arrondissement municipal"),
)

type_commune = models.CharField(
max_length=16,
choices=TYPE_COMMUNE,
blank=False,
null=False,
verbose_name="Type de commune",
)

insee = models.CharField(max_length=16, null=False, blank=False)
departement = models.CharField(max_length=16, blank=False, null=False)
libelle = models.CharField(max_length=255, null=False, blank=False)

Expand Down
5 changes: 4 additions & 1 deletion mesads/fradm/unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ def create_fixtures(self):

for insee, departement, libelle in self.COMMUNES:
obj = Commune.objects.create(
insee=insee, departement=departement, libelle=libelle
type_commune="COM",
insee=insee,
departement=departement,
libelle=libelle,
)
self.fixtures_communes.append(obj)

Expand Down
1 change: 1 addition & 0 deletions resources/2021/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Downloaded from https://www.insee.fr/fr/information/5057840
Loading

0 comments on commit 5693815

Please sign in to comment.