summaryrefslogtreecommitdiff
path: root/archaeological_finds/migrations/0141_data_migration_recommanded_treatments.py
diff options
context:
space:
mode:
Diffstat (limited to 'archaeological_finds/migrations/0141_data_migration_recommanded_treatments.py')
-rw-r--r--archaeological_finds/migrations/0141_data_migration_recommanded_treatments.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/archaeological_finds/migrations/0141_data_migration_recommanded_treatments.py b/archaeological_finds/migrations/0141_data_migration_recommanded_treatments.py
new file mode 100644
index 000000000..034c34a4c
--- /dev/null
+++ b/archaeological_finds/migrations/0141_data_migration_recommanded_treatments.py
@@ -0,0 +1,44 @@
+import os
+
+from django.db import migrations
+from django.core.management import call_command
+
+
+def load_data(apps, __):
+ migration = "0141_data_migration_recommanded_treatments.json"
+ json_path = os.sep.join(os.path.abspath(__file__).split(os.sep)[:-1] + [migration])
+ call_command("loaddata", json_path)
+ Find = apps.get_model("archaeological_finds", "find")
+ if not hasattr(Find, "preservation_to_considers"):
+ return
+ q = Find.objects.filter(preservation_to_considers__isnull=False)
+ if not q.count():
+ return
+ TreatmentType = apps.get_model("archaeological_finds", "TreatmentType")
+ RecommendedTreatmentType = apps.get_model("archaeological_finds", "RecommendedTreatmentType")
+ parent = RecommendedTreatmentType.objects.create(
+ label="Anciennes recommandations",
+ txt_idx="anciennes-recommandations",
+ order=999,
+ )
+ reco = {}
+ for find in q.all():
+ for treatment_type in find.preservation_to_considers.all():
+ if treatment_type.txt_idx not in reco:
+ reco[treatment_type.txt_idx] = RecommendedTreatmentType.objects.create(
+ label=treatment_type.label,
+ txt_idx=treatment_type.txt_idx,
+ parent=parent
+ )
+ find.recommended_treatments.add(reco[treatment_type.txt_idx])
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('archaeological_finds', '0140_recommanded_treatments'),
+ ]
+
+ operations = [
+ migrations.RunPython(load_data)
+ ]