Skip to content

Commit

Permalink
[Fixes #12657] Refactor supported types
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiagiupponi committed Oct 17, 2024
1 parent 4faae74 commit e253dda
Show file tree
Hide file tree
Showing 25 changed files with 174 additions and 207 deletions.
1 change: 1 addition & 0 deletions geonode/resource/enumerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

class ExecutionRequestAction(enum.Enum):
IMPORT = _("import")
UPLOAD = _("upload")
CREATE = _("create")
COPY = _("copy")
DELETE = _("delete")
Expand Down
50 changes: 0 additions & 50 deletions geonode/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2240,56 +2240,6 @@ def get_geonode_catalogue_service():
"document_upload",
)

SUPPORTED_DATASET_FILE_TYPES = [
{
"id": "shp",
"label": "ESRI Shapefile",
"format": "vector",
"ext": ["shp"],
"requires": ["shp", "prj", "dbf", "shx"],
"optional": ["xml", "sld"],
},
{
"id": "tiff",
"label": "GeoTIFF",
"format": "raster",
"ext": ["tiff", "tif", "geotiff", "geotif"],
"mimeType": ["image/tiff"],
"optional": ["xml", "sld"],
},
{
"id": "csv",
"label": "Comma Separated Value (CSV)",
"format": "vector",
"ext": ["csv"],
"mimeType": ["text/csv"],
"optional": ["xml", "sld"],
},
{
"id": "zip",
"label": "Zip Archive",
"format": "archive",
"ext": ["zip"],
"mimeType": ["application/zip"],
"optional": ["xml", "sld"],
},
{
"id": "xml",
"label": "XML Metadata File",
"format": "metadata",
"ext": ["xml"],
"mimeType": ["application/json"],
"needsFiles": ["shp", "prj", "dbf", "shx", "csv", "tiff", "zip", "sld"],
},
{
"id": "sld",
"label": "Styled Layer Descriptor (SLD)",
"format": "metadata",
"ext": ["sld"],
"mimeType": ["application/json"],
"needsFiles": ["shp", "prj", "dbf", "shx", "csv", "tiff", "zip", "xml"],
},
]
INSTALLED_APPS += (
"dynamic_models",
# "importer",
Expand Down
17 changes: 0 additions & 17 deletions geonode/storage/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,23 +573,6 @@ def test_zip_file_should_correctly_index_file_extensions(self):
# extensions found more than once get indexed
self.assertIsNotNone(_files.get("csv_file_1"))

@override_settings(
SUPPORTED_DATASET_FILE_TYPES=[
{"id": "kmz", "label": "kmz", "format": "vector", "ext": ["kmz"]},
{"id": "kml", "label": "kml", "format": "vector", "ext": ["kml"]},
]
)
def test_zip_file_should_correctly_recognize_main_extension_with_kmz(self):
# reinitiate the storage manager with the zip file
storage_manager = self.sut(
remote_files={"base_file": os.path.join(f"{self.project_root}", "tests/data/Italy.kmz")}
)
storage_manager.clone_remote_files()

self.assertIsNotNone(storage_manager.data_retriever.temporary_folder)
_files = storage_manager.get_retrieved_paths()
self.assertTrue("doc.kml" in _files.get("base_file"), msg=f"files available: {_files}")

def test_zip_file_should_correctly_recognize_main_extension_with_shp(self):
# zipping files
storage_manager = self.sut(remote_files=self.local_files_paths)
Expand Down
37 changes: 1 addition & 36 deletions geonode/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#########################################################################
import copy
from unittest import TestCase
from django.test import override_settings

from unittest.mock import patch
from datetime import datetime, timedelta
Expand All @@ -32,8 +31,7 @@
from geonode.geoserver.helpers import set_attributes
from geonode.tests.base import GeoNodeBaseTestSupport
from geonode.br.management.commands.utils.utils import ignore_time
from geonode.utils import copy_tree, get_supported_datasets_file_types, bbox_to_wkt
from geonode import settings
from geonode.utils import copy_tree, bbox_to_wkt


class TestCopyTree(GeoNodeBaseTestSupport):
Expand Down Expand Up @@ -205,39 +203,6 @@ def setUp(self):
},
]

@override_settings(
ADDITIONAL_DATASET_FILE_TYPES=[
{"id": "dummy_type", "label": "Dummy Type", "format": "dummy", "ext": ["dummy"]},
]
)
def test_should_append_additional_type_if_config_is_provided(self):
prev_count = len(settings.SUPPORTED_DATASET_FILE_TYPES)
supported_types = get_supported_datasets_file_types()
supported_keys = [t.get("id") for t in supported_types]
self.assertIn("dummy_type", supported_keys)
self.assertEqual(len(supported_keys), prev_count + 1)

@override_settings(
ADDITIONAL_DATASET_FILE_TYPES=[
{
"id": "shp",
"label": "Replaced type",
"format": "vector",
"ext": ["shp"],
"requires": ["shp", "prj", "dbf", "shx"],
"optional": ["xml", "sld"],
},
]
)
def test_should_replace_the_type_id_if_already_exists(self):
prev_count = len(settings.SUPPORTED_DATASET_FILE_TYPES)
supported_types = get_supported_datasets_file_types()
supported_keys = [t.get("id") for t in supported_types]
self.assertIn("shp", supported_keys)
self.assertEqual(len(supported_keys), prev_count)
shp_type = [t for t in supported_types if t["id"] == "shp"][0]
self.assertEqual(shp_type["label"], "Replaced type")


class TestRegionsCrossingDateLine(TestCase):
def setUp(self):
Expand Down
2 changes: 1 addition & 1 deletion geonode/upload/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def create(self, request, *args, **kwargs):

self.validate_upload(request, storage_manager)

action = ExecutionRequestAction.IMPORT.value
action = ExecutionRequestAction.UPLOAD.value

input_params = {
**{"files": files, "handler_module_path": str(handler)},
Expand Down
6 changes: 3 additions & 3 deletions geonode/upload/celery_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def import_orchestrator(
step="start_import",
layer_name=None,
alternate=None,
action=exa.IMPORT.value,
action=exa.UPLOAD.value,
**kwargs,
):
"""
Expand Down Expand Up @@ -179,7 +179,7 @@ def import_resource(self, execution_id, /, handler_module_path, action, **kwargs
call_rollback_function(
execution_id,
handlers_module_path=handler_module_path,
prev_action=exa.IMPORT.value,
prev_action=exa.UPLOAD.value,
layer=None,
alternate=None,
error=e,
Expand Down Expand Up @@ -309,7 +309,7 @@ def create_geonode_resource(
layer_name: Optional[str] = None,
alternate: Optional[str] = None,
handler_module_path: str = None,
action: str = exa.IMPORT.value,
action: str = exa.UPLOAD.value,
**kwargs,
):
"""
Expand Down
18 changes: 0 additions & 18 deletions geonode/upload/handlers/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,3 @@ def run_setup_hooks(*args, **kwargs):
for item in _handlers:
item.register()
logger.info(f"The following handlers have been registered: {', '.join(available_handlers)}")

_available_settings = [
import_string(module_path)().supported_file_extension_config
for module_path in available_handlers
if import_string(module_path)().supported_file_extension_config
]
# injecting the new config required for FE
supported_type = []
supported_type.extend(_available_settings)
if not getattr(settings, "ADDITIONAL_DATASET_FILE_TYPES", None):
setattr(settings, "ADDITIONAL_DATASET_FILE_TYPES", supported_type)
elif "gpkg" not in [x.get("id") for x in settings.ADDITIONAL_DATASET_FILE_TYPES]:
settings.ADDITIONAL_DATASET_FILE_TYPES.extend(supported_type)
setattr(
settings,
"ADDITIONAL_DATASET_FILE_TYPES",
settings.ADDITIONAL_DATASET_FILE_TYPES,
)
2 changes: 1 addition & 1 deletion geonode/upload/handlers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class BaseHandler(ABC):
REGISTRY = []

TASKS = {
exa.IMPORT.value: (),
exa.UPLOAD.value: (),
exa.COPY.value: (),
exa.DELETE.value: (),
exa.UPDATE.value: (),
Expand Down
3 changes: 1 addition & 2 deletions geonode/upload/handlers/common/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#
#########################################################################
import logging
from geonode.resource.enumerator import ExecutionRequestAction as exa
from geonode.upload.handlers.base import BaseHandler
from geonode.upload.handlers.utils import UploadSourcesEnum
from geonode.upload.models import ResourceHandlerInfo
Expand All @@ -37,7 +36,7 @@ class MetadataFileHandler(BaseHandler):
"""

TASKS = {
exa.IMPORT.value: ("start_import", "geonode.upload.import_resource"),
ira.RESOURCE_METADATA_UPLOAD.value: ("start_import", "geonode.upload.import_resource"),
ira.ROLLBACK.value: (
"start_rollback",
"geonode.upload.rollback",
Expand Down
2 changes: 1 addition & 1 deletion geonode/upload/handlers/common/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ def import_resource(self, files: dict, execution_id: str, **kwargs) -> str:
"geonode.upload.import_resource",
layer_name,
alternate,
exa.IMPORT.value,
exa.UPLOAD.value,
)
)
return layer_name, alternate, execution_id
Expand Down
4 changes: 2 additions & 2 deletions geonode/upload/handlers/common/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class BaseRemoteResourceHandler(BaseHandler):
"""

TASKS = {
exa.IMPORT.value: (
exa.UPLOAD.value: (
"start_import",
"geonode.upload.import_resource",
"geonode.upload.create_geonode_resource",
Expand Down Expand Up @@ -163,7 +163,7 @@ def import_resource(self, files: dict, execution_id: str, **kwargs) -> str:
"geonode.upload.import_resource",
layer_name,
alternate,
exa.IMPORT.value,
exa.UPLOAD.value,
)
)
return layer_name, alternate, execution_id
Expand Down
6 changes: 3 additions & 3 deletions geonode/upload/handlers/common/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,15 +851,15 @@ def import_next_step(
actual_step,
layer_name,
alternate,
exa.IMPORT.value,
exa.UPLOAD.value,
)

import_orchestrator.apply_async(task_params, kwargs)
except Exception as e:
call_rollback_function(
execution_id,
handlers_module_path=handlers_module_path,
prev_action=exa.IMPORT.value,
prev_action=exa.UPLOAD.value,
layer=layer_name,
alternate=alternate,
error=e,
Expand Down Expand Up @@ -927,7 +927,7 @@ def import_with_ogr2ogr(
call_rollback_function(
execution_id,
handlers_module_path=handler_module_path,
prev_action=exa.IMPORT.value,
prev_action=exa.UPLOAD.value,
layer=original_name,
alternate=alternate,
error=e,
Expand Down
22 changes: 16 additions & 6 deletions geonode/upload/handlers/csv/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class CSVFileHandler(BaseVectorFileHandler):
"""

TASKS = {
exa.IMPORT.value: (
exa.UPLOAD.value: (
"start_import",
"geonode.upload.import_resource",
"geonode.upload.publish_resource",
Expand All @@ -58,6 +58,12 @@ class CSVFileHandler(BaseVectorFileHandler):
"start_rollback",
"geonode.upload.rollback",
),
ira.REPLACE.value: (
"start_import",
"geonode.upload.import_resource",
"geonode.upload.publish_resource",
"geonode.upload.create_geonode_resource",
),
}

possible_geometry_column_name = ["geom", "geometry", "wkt_geom", "the_geom"]
Expand All @@ -69,11 +75,15 @@ class CSVFileHandler(BaseVectorFileHandler):
def supported_file_extension_config(self):
return {
"id": "csv",
"label": "CSV",
"format": "vector",
"mimeType": ["text/csv"],
"ext": ["csv"],
"optional": ["sld", "xml"],
"formats": [
{
"label": "CSV",
"required_ext": ["csv"],
"optional_ext": ["sld", "xml"],
}
],
"actions": list(self.TASKS.keys()),
"type": "vector",
}

@staticmethod
Expand Down
26 changes: 21 additions & 5 deletions geonode/upload/handlers/geojson/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class GeoJsonFileHandler(BaseVectorFileHandler):
"""

TASKS = {
exa.IMPORT.value: (
exa.UPLOAD.value: (
"start_import",
"geonode.upload.import_resource",
"geonode.upload.publish_resource",
Expand All @@ -54,16 +54,32 @@ class GeoJsonFileHandler(BaseVectorFileHandler):
"start_rollback",
"geonode.upload.rollback",
),
ira.REPLACE.value: (
"start_import",
"geonode.upload.import_resource",
"geonode.upload.publish_resource",
"geonode.upload.create_geonode_resource",
),
}

@property
def supported_file_extension_config(self):
return {
"id": "geojson",
"label": "GeoJSON",
"format": "vector",
"ext": ["json", "geojson"],
"optional": ["xml", "sld"],
"formats": [
{
"label": "GeoJSON",
"required_ext": ["geojson"],
"optional_ext": ["sld", "xml"],
},
{
"label": "GeoJSON",
"required_ext": ["json"],
"optional_ext": ["sld", "xml"],
},
],
"actions": list(self.TASKS.keys()),
"type": "vector",
}

@staticmethod
Expand Down
Loading

0 comments on commit e253dda

Please sign in to comment.