Skip to content

Commit

Permalink
Merge pull request #241 from GeoNode/ISSUE_240
Browse files Browse the repository at this point in the history
[Fixes #240] Include the id of the new resource inside the executionr…
  • Loading branch information
giohappy authored May 10, 2024
2 parents c1f205c + 6514de8 commit 4774a3c
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 64 deletions.
34 changes: 34 additions & 0 deletions importer/handlers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from geonode.resource.enumerator import ExecutionRequestAction as exa
from geonode.layers.models import Dataset
from importer.utils import ImporterRequestAction as ira
from django_celery_results.models import TaskResult
from django.db.models import Q

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -117,6 +119,38 @@ def extract_params_from_data(_data):
"""
return []

@staticmethod
def perform_last_step(execution_id):
"""
Override this method if there is some extra step to perform
before considering the execution as completed.
For example can be used to trigger an email-send to notify
that the execution is completed
"""
from importer.orchestrator import orchestrator
from importer.models import ResourceHandlerInfo

# as last step, we delete the celery task to keep the number of rows under control
lower_exec_id = execution_id.replace("-", "_").lower()
TaskResult.objects.filter(
Q(task_args__icontains=lower_exec_id)
| Q(task_kwargs__icontains=lower_exec_id)
| Q(result__icontains=lower_exec_id)
| Q(task_args__icontains=execution_id)
| Q(task_kwargs__icontains=execution_id)
| Q(result__icontains=execution_id)
).delete()

_exec = orchestrator.get_execution_object(execution_id)

resource_output_params = [
{"detail_url": x.resource.detail_url, "id": x.resource.pk}
for x in ResourceHandlerInfo.objects.filter(execution_request=_exec)
]
_exec.output_params.update({"resources": resource_output_params})
_exec.save()
return _exec

def fixup_name(self, name):
"""
Emulate the LAUNDER option in ogr2ogr which will normalize the string.
Expand Down
12 changes: 1 addition & 11 deletions importer/handlers/common/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,7 @@ def extract_params_from_data(_data, action=None):

@staticmethod
def perform_last_step(execution_id):
_exec = orchestrator.get_execution_object(execution_id)

_exec.output_params.update(
**{
"detail_url": [
x.resource.detail_url
for x in ResourceHandlerInfo.objects.filter(execution_request=_exec)
]
}
)
_exec.save()
BaseHandler.perform_last_step(execution_id=execution_id)

def import_resource(self, files: dict, execution_id: str, **kwargs):
_exec = orchestrator.get_execution_object(execution_id)
Expand Down
30 changes: 1 addition & 29 deletions importer/handlers/common/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

from django.conf import settings
from django.db.models import Q
from django_celery_results.models import TaskResult
from geonode.base.models import ResourceBase
from geonode.layers.models import Dataset
from geonode.resource.enumerator import ExecutionRequestAction as exa
Expand Down Expand Up @@ -186,34 +185,7 @@ def delete_resource(instance):

@staticmethod
def perform_last_step(execution_id):
"""
Override this method if there is some extra step to perform
before considering the execution as completed.
For example can be used to trigger an email-send to notify
that the execution is completed
"""
# as last step, we delete the celery task to keep the number of rows under control
lower_exec_id = execution_id.replace("-", "_").lower()
TaskResult.objects.filter(
Q(task_args__icontains=lower_exec_id)
| Q(task_kwargs__icontains=lower_exec_id)
| Q(result__icontains=lower_exec_id)
| Q(task_args__icontains=execution_id)
| Q(task_kwargs__icontains=execution_id)
| Q(result__icontains=execution_id)
).delete()

_exec = orchestrator.get_execution_object(execution_id)

_exec.output_params.update(
**{
"detail_url": [
x.resource.detail_url
for x in ResourceHandlerInfo.objects.filter(execution_request=_exec)
]
}
)
_exec.save()
BaseHandler.perform_last_step(execution_id=execution_id)

def extract_resource_to_publish(
self, files, action, layer_name, alternate, **kwargs
Expand Down
33 changes: 32 additions & 1 deletion importer/handlers/common/tests_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ def test_import_resource_should_not_be_imported(self, celery_chord, ogr2ogr_driv
files=self.valid_files, execution_id=str(exec_id)
)
self.assertIn(
"No valid layers found", exception.exception.args[0], 'No valid layers found.'
"No valid layers found",
exception.exception.args[0],
"No valid layers found.",
)

celery_chord.assert_not_called()
Expand Down Expand Up @@ -319,3 +321,32 @@ def test_select_valid_layers(self):
)
self.assertEqual(1, len(valid_layer))
self.assertEqual("mattia_test", valid_layer[0].GetName())

def test_perform_last_step(self):
"""
Output params in perform_last_step should return the detail_url and the ID
of the resource created
"""
# creating exec_id for the import
exec_id = orchestrator.create_execution_request(
user=get_user_model().objects.first(),
func_name="funct1",
step="step",
input_params={"files": self.valid_files, "store_spatial_file": True},
)

# create_geonode_resource
resource = self.handler.create_geonode_resource(
"layer_name",
"layer_alternate",
str(exec_id),
)
exec_obj = orchestrator.get_execution_object(str(exec_id))
self.handler.create_resourcehandlerinfo(str(self.handler), resource, exec_obj)
# calling the last_step
self.handler.perform_last_step(str(exec_id))
expected_output = {
"resources": [{"id": resource.pk, "detail_url": resource.detail_url}]
}
exec_obj.refresh_from_db()
self.assertDictEqual(expected_output, exec_obj.output_params)
24 changes: 1 addition & 23 deletions importer/handlers/common/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from celery import chord, group

from django.conf import settings
from django_celery_results.models import TaskResult
from dynamic_models.models import ModelSchema
from dynamic_models.schema import ModelSchemaEditor
from geonode.base.models import ResourceBase
Expand Down Expand Up @@ -220,28 +219,7 @@ def perform_last_step(execution_id):
For example can be used to trigger an email-send to notify
that the execution is completed
"""
# as last step, we delete the celery task to keep the number of rows under control
lower_exec_id = execution_id.replace("-", "_").lower()
TaskResult.objects.filter(
Q(task_args__icontains=lower_exec_id)
| Q(task_kwargs__icontains=lower_exec_id)
| Q(result__icontains=lower_exec_id)
| Q(task_args__icontains=execution_id)
| Q(task_kwargs__icontains=execution_id)
| Q(result__icontains=execution_id)
).delete()

_exec = orchestrator.get_execution_object(execution_id)

_exec.output_params.update(
**{
"detail_url": [
x.resource.detail_url
for x in ResourceHandlerInfo.objects.filter(execution_request=_exec)
]
}
)
_exec.save()
_exec = BaseHandler.perform_last_step(execution_id=execution_id)
if _exec and not _exec.input_params.get("store_spatial_file", False):
resources = ResourceHandlerInfo.objects.filter(execution_request=_exec)
# getting all files list
Expand Down

0 comments on commit 4774a3c

Please sign in to comment.