From 5c6596a9d98ef5d6f94b8df4bdc791c17dadf2aa Mon Sep 17 00:00:00 2001 From: Mattia Date: Thu, 20 Jun 2024 11:42:43 +0200 Subject: [PATCH] Add ID for base handlers --- Dockerfile | 2 +- importer/api/views.py | 2 +- importer/handlers/base.py | 12 +++++++++++- importer/handlers/gpkg/tests.py | 2 +- importer/orchestrator.py | 7 +++++++ importer/tests/unit/test_orchestrator.py | 8 ++++++-- importer/tests/unit/test_task.py | 2 +- 7 files changed, 28 insertions(+), 7 deletions(-) diff --git a/Dockerfile b/Dockerfile index 03e9cb5..e7c2cc3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ FROM geonode/geonode-base:latest-ubuntu-22.04 RUN rm -rf /usr/src/geonode RUN git clone https://github.com/GeoNode/geonode.git /usr/src/geonode -RUN cd /usr/src/geonode && git fetch --all && git checkout 12226_directory_assets_3 && cd - +RUN cd /usr/src/geonode && git fetch --all && git checkout assets_master && cd - RUN mkdir -p /usr/src/importer RUN cd .. diff --git a/importer/api/views.py b/importer/api/views.py index 8b603a3..dcaf414 100644 --- a/importer/api/views.py +++ b/importer/api/views.py @@ -201,7 +201,7 @@ def generate_asset_and_retrieve_paths(self, request, storage_manager, handler): title="Original", owner=request.user, description=None, - type=str(handler), + type=handler.id, files=list(set(_files.values())), clone_files=False, ) diff --git a/importer/handlers/base.py b/importer/handlers/base.py index e005fcd..6c74ae8 100644 --- a/importer/handlers/base.py +++ b/importer/handlers/base.py @@ -4,6 +4,7 @@ from geonode.resource.enumerator import ExecutionRequestAction as exa from geonode.layers.models import Dataset +from importer.api.exception import ImportException from importer.utils import ImporterRequestAction as ira from django_celery_results.models import TaskResult from django.db.models import Q @@ -56,9 +57,18 @@ def get_task_list(cls, action) -> tuple: def default_geometry_column_name(self): return "geometry" + @property + def id(self): + pk = self.supported_file_extension_config.get("id", None) + if pk is None: + raise ImportException( + "PK must be defined, check that supported_file_extension_config had been correctly defined, it cannot be empty" + ) + return pk + @property def supported_file_extension_config(self): - return NotImplementedError + return {} @property def can_handle_xml_file(self) -> bool: diff --git a/importer/handlers/gpkg/tests.py b/importer/handlers/gpkg/tests.py index 91c5cd0..4374099 100644 --- a/importer/handlers/gpkg/tests.py +++ b/importer/handlers/gpkg/tests.py @@ -126,7 +126,7 @@ def test_single_message_error_handler(self): title="Original", owner=user, description=None, - type="importer.handlers.gpkg.handler.GPKGFileHandler", + type="gpkg", files=["/tmp/valid.gpkg"], clone_files=False, ) diff --git a/importer/orchestrator.py b/importer/orchestrator.py index 81b63ff..afd18f8 100644 --- a/importer/orchestrator.py +++ b/importer/orchestrator.py @@ -63,6 +63,13 @@ def load_handler(self, module_path): except Exception: raise ImportException(detail=f"The handler is not available: {module_path}") + def load_handler_by_id(self, handler_id): + for handler in BaseHandler.get_registry(): + if handler().id == handler_id: + return handler + logger.error("Handler not found") + return None + def get_execution_object(self, exec_id): """ Returns the ExecutionRequest object with the detail about the diff --git a/importer/tests/unit/test_orchestrator.py b/importer/tests/unit/test_orchestrator.py index 4f6ef31..d3ab4e7 100644 --- a/importer/tests/unit/test_orchestrator.py +++ b/importer/tests/unit/test_orchestrator.py @@ -60,6 +60,10 @@ def test_load_handler(self): ) self.assertIsInstance(actual(), BaseHandler) + def test_load_handler_by_id(self): + actual = self.orchestrator.load_handler_by_id("gpkg") + self.assertIsInstance(actual(), BaseHandler) + def test_get_execution_object_raise_exp_if_not_exists(self): with self.assertRaises(ImportException) as _exc: self.orchestrator.get_execution_object(str(uuid.uuid4())) @@ -197,7 +201,7 @@ def test_set_as_failed(self): title="Original", owner=user, description=None, - type="importer.handlers.gpkg.handler.GPKGFileHandler", + type="gpkg", files=[fake_path], clone_files=False, ) @@ -345,7 +349,7 @@ def test_evaluate_execution_progress_should_fail_if_one_task_is_failed(self): title="Original", owner=user, description=None, - type="importer.handlers.gpkg.handler.GPKGFileHandler", + type="gpkg", files=[fake_path], clone_files=False, ) diff --git a/importer/tests/unit/test_task.py b/importer/tests/unit/test_task.py index d61cf5c..34dee1b 100644 --- a/importer/tests/unit/test_task.py +++ b/importer/tests/unit/test_task.py @@ -48,7 +48,7 @@ def setUp(self): title="Original", owner=self.user, description=None, - type="importer.handlers.gpkg.handler.GPKGFileHandler", + type="gpkg", files=[self.existing_file], clone_files=False, )