|
| 1 | +# Generated by Django 3.2.15 on 2022-10-04 13:03 |
| 2 | + |
| 3 | +import logging |
| 4 | +from django.db import migrations |
| 5 | +from importer.orchestrator import orchestrator |
| 6 | +from geonode.layers.models import Dataset |
| 7 | +from geonode.assets.utils import get_default_asset |
| 8 | +from geonode.utils import get_allowed_extensions |
| 9 | + |
| 10 | +logger = logging.getLogger("django") |
| 11 | + |
| 12 | +def dataset_migration(apps, _): |
| 13 | + NewResources = apps.get_model("importer", "ResourceHandlerInfo") |
| 14 | + for old_resource in Dataset.objects.exclude( |
| 15 | + pk__in=NewResources.objects.values_list("resource_id", flat=True) |
| 16 | + ).exclude(subtype__in=["remote", None]): |
| 17 | + # generating orchestrator expected data file |
| 18 | + if old_resource.resourcehandlerinfo_set.first() is None: |
| 19 | + if get_default_asset(old_resource): |
| 20 | + available_choices = get_allowed_extensions() |
| 21 | + not_main_files = ["xml", "sld", "zip", "kmz"] |
| 22 | + base_file_choices = set(x for x in available_choices if x not in not_main_files) |
| 23 | + output_files = dict() |
| 24 | + for _file in get_default_asset(old_resource).location: |
| 25 | + if _file.split(".")[-1] in base_file_choices: |
| 26 | + output_files.update({"base_file": _file}) |
| 27 | + break |
| 28 | + else: |
| 29 | + if old_resource.is_vector(): |
| 30 | + output_files = {"base_file": "placeholder.shp"} |
| 31 | + else: |
| 32 | + output_files = {"base_file": "placeholder.tiff"} |
| 33 | + |
| 34 | + handler = orchestrator.get_handler(output_files) |
| 35 | + if handler is None: |
| 36 | + logger.error(f"Handler not found for resource: {old_resource}") |
| 37 | + continue |
| 38 | + handler.create_resourcehandlerinfo( |
| 39 | + handler_module_path=str(handler), |
| 40 | + resource=old_resource, |
| 41 | + execution_id=None |
| 42 | + ) |
| 43 | + else: |
| 44 | + logger.debug(f"resourcehandler info already exists for the resource") |
| 45 | + |
| 46 | + |
| 47 | +class Migration(migrations.Migration): |
| 48 | + dependencies = [ |
| 49 | + ("importer", "0006_dataset_migration"), |
| 50 | + ] |
| 51 | + |
| 52 | + operations = [ |
| 53 | + migrations.RunPython(dataset_migration), |
| 54 | + ] |
0 commit comments