Skip to content

Commit 3231f58

Browse files
Fix migrations for asset (#283)
* Fix migrations for create handlerinfo via asset * Fix migrations for create handlerinfo via asset
1 parent f9e7a74 commit 3231f58

File tree

3 files changed

+63
-6
lines changed

3 files changed

+63
-6
lines changed

importer/handlers/tiles3d/handler.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,15 @@ def can_handle(_data) -> bool:
5656
This endpoint will return True or False if with the info provided
5757
the handler is able to handle the file or not
5858
"""
59-
base = _data.get("base_file")
60-
if not base:
59+
try:
60+
base = _data.get("base_file")
61+
if not base:
62+
return False
63+
ext = base.split(".")[-1] if isinstance(base, str) else base.name.split(".")[-1]
64+
if ext in ["json"] and Tiles3DFileHandler.is_3dtiles_json(base):
65+
return True
66+
except Exception:
6167
return False
62-
ext = base.split(".")[-1] if isinstance(base, str) else base.name.split(".")[-1]
63-
if ext in ["json"] and Tiles3DFileHandler.is_3dtiles_json(base):
64-
return True
6568
return False
6669

6770
@staticmethod

importer/migrations/0006_dataset_migration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def dataset_migration(apps, _):
1111
pk__in=NewResources.objects.values_list("resource_id", flat=True)
1212
).exclude(subtype__in=["remote", None]):
1313
# generating orchestrator expected data file
14-
if not old_resource.files:
14+
if not hasattr(old_resource, "files"):
1515
if old_resource.is_vector():
1616
converted_files = [{"base_file": "placeholder.shp"}]
1717
else:
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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

Comments
 (0)