Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge hotfix sur staging #748

Merged
merged 15 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 34 additions & 10 deletions project/migrations/0094_auto_20241016_2217.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,50 @@
from public_data.models import AdminRef


def set_land_id_from_primary_keys_to_natural_key(apps, schema_editor):
def set_land_id_from_primary_keys_to_natural_key(apps, schema_editor): # noqa: C901
Project = apps.get_model("project", "Project")

for project in Project.objects.all():
land_type = project.land_type
klass = None

if not project.land_id:
project.delete()
continue
if project.land_id and "," in str(project.land_id):
project.delete()
continue
if land_type == AdminRef.COMMUNE:
klass = apps.get_model("public_data", "Commune")
new_land_id = klass.objects.get(id=project.land_id).insee
try:
klass = apps.get_model("public_data", "Commune")
new_land_id = klass.objects.get(id=project.land_id).insee
except klass.DoesNotExist:
project.delete()
continue
elif land_type == AdminRef.EPCI:
klass = apps.get_model("public_data", "Epci")
new_land_id = klass.objects.get(id=project.land_id).source_id
try:
klass = apps.get_model("public_data", "Epci")
new_land_id = klass.objects.get(id=project.land_id).source_id
except klass.DoesNotExist:
project.delete()
continue
elif land_type == AdminRef.DEPARTEMENT:
klass = apps.get_model("public_data", "Departement")
new_land_id = klass.objects.get(id=project.land_id).source_id
try:
klass = apps.get_model("public_data", "Departement")
new_land_id = klass.objects.get(id=project.land_id).source_id
except klass.DoesNotExist:
project.delete()
continue
elif land_type == AdminRef.REGION:
klass = apps.get_model("public_data", "Region")
new_land_id = klass.objects.get(id=project.land_id).source_id
try:
klass = apps.get_model("public_data", "Region")
new_land_id = klass.objects.get(id=project.land_id).source_id
except klass.DoesNotExist:
project.delete()
continue
else:
raise ValueError(f"Unknown land type: {land_type}")
project.delete()
continue

project.land_id = new_land_id
project.save()
Expand Down
54 changes: 43 additions & 11 deletions project/migrations/0095_auto_20241016_2221.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,62 @@
from public_data.models import AdminRef


def modify_lookalike_from_primary_keys_to_natural_key(apps, schema_editor):
def modify_lookalike_from_primary_keys_to_natural_key(apps, schema_editor): # noqa: C901
Project = apps.get_model("project", "Project")
for project in Project.objects.all():
land_keys = []
if not project.look_a_like:
continue
for land in project.look_a_like.split(";"):
if not project.id:
# Project has been deleted
continue
if not land:
raise ValueError("Empty land key")
project.delete()
continue
land_type, land_id = land.split("_")
klass = None
if land_type == AdminRef.COMMUNE:
klass = apps.get_model("public_data", "Commune")
new_land_id = klass.objects.get(id=land_id).insee
try:
klass = apps.get_model("public_data", "Commune")
new_land_id = klass.objects.get(id=land_id).insee
except klass.DoesNotExist:
project.delete()
continue
elif land_type == AdminRef.EPCI:
klass = apps.get_model("public_data", "Epci")
new_land_id = klass.objects.get(id=land_id).source_id
try:
klass = apps.get_model("public_data", "Epci")
new_land_id = klass.objects.get(id=land_id).source_id
except klass.DoesNotExist:
project.delete()
continue
elif land_type == AdminRef.DEPARTEMENT:
klass = apps.get_model("public_data", "Departement")
new_land_id = klass.objects.get(id=land_id).source_id
try:
klass = apps.get_model("public_data", "Departement")
new_land_id = klass.objects.get(id=land_id).source_id
except klass.DoesNotExist:
project.delete()
continue
elif land_type == AdminRef.REGION:
klass = apps.get_model("public_data", "Region")
new_land_id = klass.objects.get(id=land_id).source_id
try:
klass = apps.get_model("public_data", "Region")
new_land_id = klass.objects.get(id=land_id).source_id
except klass.DoesNotExist:
project.delete()
continue
else:
raise ValueError(f"Unknown land type: {land_type}")
project.delete()
continue
land_keys.append(f"{land_type}_{new_land_id}")
if not project.id:
continue

new_value = ";".join(land_keys)

if len(new_value) > 250:
project.delete()
continue

project.look_a_like = ";".join(land_keys)
project.save()

Expand Down
Loading