diff options
| author | Étienne Loks <etienne.loks@iggdrasil.net> | 2025-09-30 14:06:45 +0200 |
|---|---|---|
| committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2025-10-02 23:15:26 +0200 |
| commit | 93ece4d67007e86066726d9b724a7fefa86db68f (patch) | |
| tree | 33979ef9781993a9e983b0dcf4b6937fdead229a /archaeological_finds/migrations/0141_data_migration_recommanded_treatments.py | |
| parent | cacd9f9389b4ccf136cf50191a36b7fbaf4bbded (diff) | |
| download | Ishtar-93ece4d67007e86066726d9b724a7fefa86db68f.tar.bz2 Ishtar-93ece4d67007e86066726d9b724a7fefa86db68f.zip | |
🗃️ finds - specific tables for recommended treatments
Diffstat (limited to 'archaeological_finds/migrations/0141_data_migration_recommanded_treatments.py')
| -rw-r--r-- | archaeological_finds/migrations/0141_data_migration_recommanded_treatments.py | 44 |
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) + ] |
