Skip to content

Commit

Permalink
Fixes #200: Remove import_metadata, collapse it to import_resource
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiagiupponi committed Sep 12, 2023
1 parent ef12bd5 commit 910b0f8
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 67 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ CELERY_TASK_QUEUES += (
Queue('importer.copy_geonode_data_table', GEONODE_EXCHANGE, routing_key='importer.copy_geonode_data_table'),
Queue('importer.copy_raster_file', GEONODE_EXCHANGE, routing_key='importer.copy_raster_file'),
Queue('importer.rollback', GEONODE_EXCHANGE, routing_key='importer.rollback'),
Queue("importer.import_metadata", GEONODE_EXCHANGE, routing_key="importer.import_metadata"),

)

Expand Down
48 changes: 0 additions & 48 deletions importer/celery_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,51 +772,3 @@ def dynamic_model_error_callback(*args, **kwargs):
drop_dynamic_model_schema(schema_model)

return "error"


@importer_app.task(
base=ErrorBaseTaskClass,
name="importer.import_metadata",
queue="importer.import_metadata",
task_track_started=True,
)
def import_metadata(execution_id, /, handler_module_path, action=exa.IMPORT.value, **kwargs):
"""
Import the metadata XML file via "upload metadata" form in geonode
"""
try:
orchestrator.update_execution_request_status(
execution_id=execution_id,
last_updated=timezone.now(),
func_name="import_metadata",
step=ugettext("importer.import_metadata"),
)

handler = orchestrator.load_handler(handler_module_path)()

_exec = orchestrator.get_execution_object(execution_id)

_files = _exec.input_params.get("files")

handler.is_valid(_files)

dataset_obj = handler.import_metadata_file(execution_id)

task_params = (
{},
execution_id,
handler_module_path,
"importer.import_metadata",
dataset_obj.name,
dataset_obj.alternate,
action,
)

kwargs = kwargs.get("kwargs") if "kwargs" in kwargs else kwargs

import_orchestrator.apply_async(task_params, kwargs)

except Exception as e:
raise InvalidMetadataException(detail=e)
return execution_id, kwargs

12 changes: 9 additions & 3 deletions importer/handlers/common/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from geonode.resource.enumerator import ExecutionRequestAction as exa
from importer.handlers.base import BaseHandler
from importer.handlers.metadata.serializer import MetadataFileSerializer
from importer.utils import ImporterRequestAction as ira

logger = logging.getLogger(__name__)

Expand All @@ -15,14 +16,19 @@ class MetadataFileHandler(BaseHandler):
ACTIONS = {
exa.IMPORT.value: (
"start_import",
"importer.import_metadata"
)
"importer.import_resource"
),
ira.ROLLBACK.value: ()
}

@staticmethod
def has_serializer(_data) -> bool:
return MetadataFileSerializer

@property
def supported_file_extension_config(self):
return None

@staticmethod
def extract_params_from_data(_data, action=None):
"""
Expand All @@ -40,6 +46,6 @@ def extract_params_from_data(_data, action=None):
def perform_last_step(execution_id):
pass

def import_metadata_file(self, execution_id):
def import_resource(self, files: dict, execution_id: str, **kwargs):
pass

15 changes: 5 additions & 10 deletions importer/handlers/metadata/xml/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ class XMLFileHandler(MetadataFileHandler):
It must provide the task_lists required to comple the upload
"""

ACTIONS = {
exa.IMPORT.value: (
"start_import",
"importer.import_metadata"
)
}

@staticmethod
def can_handle(_data) -> bool:
"""
Expand All @@ -41,7 +34,7 @@ def can_handle(_data) -> bool:
)

@staticmethod
def is_valid(files):
def is_valid(files, user):
"""
Define basic validation steps
"""
Expand All @@ -54,7 +47,7 @@ def is_valid(files):
raise InvalidXmlException(f"Uploaded document is not XML or is invalid: {str(err)}")
return True

def import_metadata_file(self, execution_id):
def import_resource(self, files: dict, execution_id: str, **kwargs):
_exec = orchestrator.get_execution_object(execution_id)
# getting the dataset
alternate = _exec.input_params.get("dataset_title")
Expand All @@ -79,5 +72,7 @@ def import_metadata_file(self, execution_id):
vals={"dirty_state": True},
)
dataset.refresh_from_db()
return dataset

orchestrator.evaluate_execution_progress(execution_id, handler_module_path=str(self))
return

4 changes: 2 additions & 2 deletions importer/handlers/metadata/xml/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def setUpClass(cls):
def test_task_list_is_the_expected_one(self):
expected = (
"start_import",
"importer.import_metadata",
"importer.import_resource",
)
self.assertEqual(len(self.handler.ACTIONS["import"]), 2)
self.assertTupleEqual(expected, self.handler.ACTIONS["import"])
Expand Down Expand Up @@ -69,7 +69,7 @@ def test_can_successfully_import_metadata_file(self):

self.assertEqual(self.layer.title, "extruded_polygon")

self.handler.import_metadata_file(str(exec_id))
self.handler.import_resource({}, str(exec_id))

self.layer.refresh_from_db()
self.assertEqual(self.layer.title, "test_dataset")
5 changes: 2 additions & 3 deletions importer/tests/unit/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
import_resource,
orchestrator,
publish_resource,
rollback,
import_metadata
rollback
)
from geonode.resource.models import ExecutionRequest
from geonode.layers.models import Dataset
Expand Down Expand Up @@ -487,7 +486,7 @@ def test_import_metadata_should_work_as_expected(self):
handler_module_path="importer.handlers.shapefile.handler.ShapeFileHandler",
)

import_metadata(str(exec_id), handler)
import_resource(str(exec_id), handler, "import")

layer.refresh_from_db()
self.assertEqual(layer.title, "test_dataset")
Expand Down

0 comments on commit 910b0f8

Please sign in to comment.