diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2022-02-17 13:03:40 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2022-12-12 12:21:00 +0100 |
commit | 1205622c9acfef772a73c15352a4fa812ca7e358 (patch) | |
tree | 00b6f9ca825f89ba76900c3095e9191410c5451f /ishtar_common/management/commands/migrate_to_geo_v4.py | |
parent | b93a677cc30d92a0efa0ca8be52614ed1eb67329 (diff) | |
download | Ishtar-1205622c9acfef772a73c15352a4fa812ca7e358.tar.bz2 Ishtar-1205622c9acfef772a73c15352a4fa812ca7e358.zip |
Geodata redesign: operation migrations (1/2) - operation geo post save - town area creation
Diffstat (limited to 'ishtar_common/management/commands/migrate_to_geo_v4.py')
-rw-r--r-- | ishtar_common/management/commands/migrate_to_geo_v4.py | 69 |
1 files changed, 60 insertions, 9 deletions
diff --git a/ishtar_common/management/commands/migrate_to_geo_v4.py b/ishtar_common/management/commands/migrate_to_geo_v4.py index f747cb72d..df275bd85 100644 --- a/ishtar_common/management/commands/migrate_to_geo_v4.py +++ b/ishtar_common/management/commands/migrate_to_geo_v4.py @@ -1,16 +1,19 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -from django.contrib.contenttypes.models import ContentType import csv import datetime import os import sys from django.conf import settings +from django.contrib.contenttypes.models import ContentType from django.core.management.base import BaseCommand -from ishtar_common import models_common +from ishtar_common.utils import ugettext_lazy as _ + +from ishtar_common import models_common, models +from archaeological_operations.models import Operation log_path = os.sep.join([settings.ROOT_PATH, "logs"]) @@ -22,13 +25,16 @@ def migrate(quiet=False, log=True): changed = [] # create towns q = models_common.Town.objects.exclude( - center__isnull=True, limit__isnull=True).exclude(main_geodata__isnull=False) + center__isnull=True, limit__isnull=True + ).exclude(main_geodata__isnull=False) nb = q.count() - town_content_type = ContentType.objects.get(app_label='ishtar_common', model='town') + town_content_type = ContentType.objects.get(app_label="ishtar_common", model="town") data_type, __ = models_common.GeoDataType.objects.get_or_create( - txt_idx="town-limit", defaults={"label": "Limites commune"}) + txt_idx="town-limit", defaults={"label": "Limites commune"} + ) provider, __ = models_common.GeoProviderType.objects.get_or_create( - txt_idx="france-ign", defaults={"label": "IGN"}) + txt_idx="france-ign", defaults={"label": "IGN"} + ) for idx, town in enumerate(q.all()): if not quiet: sys.stdout.write(f"\r[{percent(idx, nb)}] Migrate towns {idx + 1}/{nb}") @@ -46,16 +52,61 @@ def migrate(quiet=False, log=True): attrs["point_2d"] = town.center data, created = models_common.GeoVectorData.objects.get_or_create(**attrs) if created: - changed.append(["geovectordata", data.name, data.pk]) + changed.append(["geovectordata", data.name, data.pk, "Création commune"]) town.main_geodata = data town.save() + # manage operation vector sources + operation_content_type = ContentType.objects.get( + app_label="archaeological_operations", model="operation" + ) + q = Operation.objects.exclude(main_geodata__isnull=False) + nb = q.count() + for idx, operation in enumerate(q.all()): + if operation.multi_polygon_source == "T": + operation._no_move = True + operation.skip_history_when_saving = True + operation.save() # auto managed + elif operation.multi_polygon_source == "P": + # TODO + + """ + q = operation.towns.filter(limit__isnull=False) + nb = q.count + if not nb: + break + elif nb == 1: + town = q.all()[0] + geo = models_common.GeoVectorData.objects.get( + source_content_type=town_content_type, + source_id=town.pk + ) + else: + + attrs = { + "name": name, + "source_content_type": town_content_type, + "source_id": town.pk, + "data_type": data_type, + "provider": provider, + "multi_polygon": poly, + } + geo, created = models_common.GeoVectorData.objects.get_or_create( + **attrs) + + operation.main_geodata = geo + operation.skip_history_when_saving = True + operation._no_move = True + operation.save() + """ + + if log and changed: filename = f"geo_migration-created-{get_time().replace(':', '')}.txt" path = os.sep.join([log_path, filename]) - with open(path, 'w+') as fle: + with open(path, "w+") as fle: writer = csv.writer(fle) - writer.writerow(["model", "name", "id"]) + writer.writerow(["model", "name", "id", "context"]) for change in changed: writer.writerow(change) if not quiet: |