diff options
Diffstat (limited to 'ishtar_common/management/commands/migrate_to_geo_v4.py')
| -rw-r--r-- | ishtar_common/management/commands/migrate_to_geo_v4.py | 166 | 
1 files changed, 93 insertions, 73 deletions
| diff --git a/ishtar_common/management/commands/migrate_to_geo_v4.py b/ishtar_common/management/commands/migrate_to_geo_v4.py index 8daf921d9..ed1a877f3 100644 --- a/ishtar_common/management/commands/migrate_to_geo_v4.py +++ b/ishtar_common/management/commands/migrate_to_geo_v4.py @@ -59,84 +59,104 @@ def migrate(quiet=False, log=True):          sys.stdout.write(f"\r[{get_time()}] Towns migrated\n")          sys.stdout.flush() -    # 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() -    data_type_area, __ = models_common.GeoDataType.objects.get_or_create( -        txt_idx="operation-area", defaults={"label": "Emprise de l'opération"} -    ) -    data_type_center, __ = models_common.GeoDataType.objects.get_or_create( -        txt_idx="operation-center", defaults={"label": "Centre de l'opération"} -    ) -    for idx, operation in enumerate(q.all()): -        if not quiet: -            sys.stdout.write(f"\r[{percent(idx, nb)}] Migrate operations {idx + 1}/{nb}") -            sys.stdout.flush() - -        operation._no_move = True -        operation.skip_history_when_saving = True -        operation.save()  # auto manage geo town association -        q_towns = operation.towns.filter(main_geodata__multi_polygon__isnull=False) -        if q_towns.count() > 1: -            changed.append( -                ["operation", str(operation), operation.pk, -                 "Association géo de zone communale"]) -        elif q_towns.count() == 1: -            changed.append( -                ["operation", str(operation), operation.pk, -                 "Association géo de commune"]) -        if operation.multi_polygon_source == "P" and operation.multi_polygon: -            attrs = { -                "name": f"{_('Operation')}{_(':')} {str(operation)}", -                "source_content_type": operation_content_type, -                "source_id": operation.pk, -                "multi_polygon": operation.multi_polygon, -                "data_type": data_type_area, -            } -            data = models_common.GeoVectorData.objects.create(**attrs) -            operation.main_geodata = data -            operation.save() -            changed.append( -                    ["geovectordata", data.name, data.pk, "Multi-polygone opération"]) -        if operation.point_source == "P" and operation.point_2d: -            if operation.x and operation.y: -                attrs = { -                    "name": f"{_('Operation')}{_(':')} {str(operation)}", -                    "source_content_type": operation_content_type, -                    "source_id": operation.pk, -                    "data_type": data_type_center, -                    "x": operation.x, -                    "y": operation.y, -                    "z": operation.z, -                } -                data = models_common.GeoVectorData.objects.create(**attrs) -                operation.main_geodata = data -                operation.save() +    model_list = [ +        ("operation", "opération", "de l'opération", Operation), +        ("archaeologicalsite", "site", "du site", ArchaeologicalSite), +    ] +    for model_slug, model_name, model_full_name, model in model_list: +        # manage operation vector sources +        model_content_type = ContentType.objects.get( +            app_label="archaeological_operations", model=model_slug +        ) +        q = model.objects.exclude(main_geodata__isnull=False) +        nb = q.count() +        data_type_area, __ = models_common.GeoDataType.objects.get_or_create( +            txt_idx=f"{model_slug}-area", +            defaults={"label": f"Emprise {model_full_name}"}, +        ) +        data_type_center, __ = models_common.GeoDataType.objects.get_or_create( +            txt_idx="operation-center", defaults={"label": f"Centre {model_full_name}"} +        ) +        for idx, obj in enumerate(q.all()): +            if not quiet: +                sys.stdout.write( +                    f"\r[{percent(idx, nb)}] Migrate {model_name}s {idx + 1}/{nb}" +                ) +                sys.stdout.flush() + +            obj._no_move = True +            obj.skip_history_when_saving = True +            obj.save()  # auto manage geo town association +            q_towns = obj.towns.filter(main_geodata__multi_polygon__isnull=False) +            if q_towns.count() > 1: +                changed.append( +                    [model_slug, str(obj), obj.pk, "Association géo de zone communale"] +                ) +            elif q_towns.count() == 1:                  changed.append( -                    ["geovectordata", data.name, data.pk, "Coordonnées opération"]) -            elif operation.point_2d: +                    [model_slug, str(obj), obj.pk, "Association géo de commune"] +                ) +            if obj.multi_polygon_source == "P" and obj.multi_polygon:                  attrs = { -                    "name": f"{_('Operation')}{_(':')} {str(operation)}", -                    "source_content_type": operation_content_type, -                    "source_id": operation.pk, -                    "data_type": data_type_center, +                    "name": f"{_(model_name.capitalize())}{_(':')} {str(obj)}", +                    "source_content_type": model_content_type, +                    "source_id": obj.pk, +                    "multi_polygon": obj.multi_polygon, +                    "data_type": data_type_area,                  } -                if operation.point: -                    attrs["point_3d"] = operation.point -                else: -                    attrs["point_2d"] = operation.point_2d                  data = models_common.GeoVectorData.objects.create(**attrs) -                operation.main_geodata = data -                operation.save() +                obj.main_geodata = data +                obj.save()                  changed.append( -                    ["geovectordata", data.name, data.pk, "Point opération"]) -    if not quiet and nb: -        sys.stdout.write(f"\r[{get_time()}] Operation migrated\n") -        sys.stdout.flush() - +                    [ +                        "geovectordata", +                        data.name, +                        data.pk, +                        f"Multi-polygone {model_name}", +                    ] +                ) +            if obj.point_source == "P" and obj.point_2d: +                if obj.x and obj.y: +                    attrs = { +                        "name": f"{_(model_name.capitalize())}{_(':')} {str(obj)}", +                        "source_content_type": model_content_type, +                        "source_id": obj.pk, +                        "data_type": data_type_center, +                        "x": obj.x, +                        "y": obj.y, +                        "z": obj.z, +                    } +                    data = models_common.GeoVectorData.objects.create(**attrs) +                    obj.main_geodata = data +                    obj.save() +                    changed.append( +                        [ +                            "geovectordata", +                            data.name, +                            data.pk, +                            f"Coordonnées {model_name}", +                        ] +                    ) +                elif obj.point_2d: +                    attrs = { +                        "name": f"{_(model_name.capitalize())}{_(':')} {str(obj)}", +                        "source_content_type": model_content_type, +                        "source_id": obj.pk, +                        "data_type": data_type_center, +                    } +                    if obj.point: +                        attrs["point_3d"] = obj.point +                    else: +                        attrs["point_2d"] = obj.point_2d +                    data = models_common.GeoVectorData.objects.create(**attrs) +                    obj.main_geodata = data +                    obj.save() +                    changed.append( +                        ["geovectordata", data.name, data.pk, f"Point {model_name}"] +                    ) +        if not quiet and nb: +            sys.stdout.write(f"\r[{get_time()}] {model_name.capitalize()} migrated\n") +            sys.stdout.flush()      if log and changed:          filename = f"geo_migration-created-{get_time().replace(':', '')}.csv" | 
