From 3231f58b9cdc5a0d840b497c45288c56668bb991 Mon Sep 17 00:00:00 2001 From: mattiagiupponi <51856725+mattiagiupponi@users.noreply.github.com> Date: Thu, 30 Jan 2025 11:00:19 +0100 Subject: [PATCH] Fix migrations for asset (#283) * Fix migrations for create handlerinfo via asset * Fix migrations for create handlerinfo via asset --- importer/handlers/tiles3d/handler.py | 13 +++-- importer/migrations/0006_dataset_migration.py | 2 +- .../0007_align_resourcehandler_with_asset.py | 54 +++++++++++++++++++ 3 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 importer/migrations/0007_align_resourcehandler_with_asset.py diff --git a/importer/handlers/tiles3d/handler.py b/importer/handlers/tiles3d/handler.py index b0ff2038..7e5d5250 100755 --- a/importer/handlers/tiles3d/handler.py +++ b/importer/handlers/tiles3d/handler.py @@ -56,12 +56,15 @@ def can_handle(_data) -> bool: This endpoint will return True or False if with the info provided the handler is able to handle the file or not """ - base = _data.get("base_file") - if not base: + try: + base = _data.get("base_file") + if not base: + return False + ext = base.split(".")[-1] if isinstance(base, str) else base.name.split(".")[-1] + if ext in ["json"] and Tiles3DFileHandler.is_3dtiles_json(base): + return True + except Exception: return False - ext = base.split(".")[-1] if isinstance(base, str) else base.name.split(".")[-1] - if ext in ["json"] and Tiles3DFileHandler.is_3dtiles_json(base): - return True return False @staticmethod diff --git a/importer/migrations/0006_dataset_migration.py b/importer/migrations/0006_dataset_migration.py index 768d6a49..840b95ca 100644 --- a/importer/migrations/0006_dataset_migration.py +++ b/importer/migrations/0006_dataset_migration.py @@ -11,7 +11,7 @@ def dataset_migration(apps, _): pk__in=NewResources.objects.values_list("resource_id", flat=True) ).exclude(subtype__in=["remote", None]): # generating orchestrator expected data file - if not old_resource.files: + if not hasattr(old_resource, "files"): if old_resource.is_vector(): converted_files = [{"base_file": "placeholder.shp"}] else: diff --git a/importer/migrations/0007_align_resourcehandler_with_asset.py b/importer/migrations/0007_align_resourcehandler_with_asset.py new file mode 100644 index 00000000..7ca9b9c0 --- /dev/null +++ b/importer/migrations/0007_align_resourcehandler_with_asset.py @@ -0,0 +1,54 @@ +# Generated by Django 3.2.15 on 2022-10-04 13:03 + +import logging +from django.db import migrations +from importer.orchestrator import orchestrator +from geonode.layers.models import Dataset +from geonode.assets.utils import get_default_asset +from geonode.utils import get_allowed_extensions + +logger = logging.getLogger("django") + +def dataset_migration(apps, _): + NewResources = apps.get_model("importer", "ResourceHandlerInfo") + for old_resource in Dataset.objects.exclude( + pk__in=NewResources.objects.values_list("resource_id", flat=True) + ).exclude(subtype__in=["remote", None]): + # generating orchestrator expected data file + if old_resource.resourcehandlerinfo_set.first() is None: + if get_default_asset(old_resource): + available_choices = get_allowed_extensions() + not_main_files = ["xml", "sld", "zip", "kmz"] + base_file_choices = set(x for x in available_choices if x not in not_main_files) + output_files = dict() + for _file in get_default_asset(old_resource).location: + if _file.split(".")[-1] in base_file_choices: + output_files.update({"base_file": _file}) + break + else: + if old_resource.is_vector(): + output_files = {"base_file": "placeholder.shp"} + else: + output_files = {"base_file": "placeholder.tiff"} + + handler = orchestrator.get_handler(output_files) + if handler is None: + logger.error(f"Handler not found for resource: {old_resource}") + continue + handler.create_resourcehandlerinfo( + handler_module_path=str(handler), + resource=old_resource, + execution_id=None + ) + else: + logger.debug(f"resourcehandler info already exists for the resource") + + +class Migration(migrations.Migration): + dependencies = [ + ("importer", "0006_dataset_migration"), + ] + + operations = [ + migrations.RunPython(dataset_migration), + ]