Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to support Assets #235

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +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 checkout 12124_assets && cd -
mattiagiupponi marked this conversation as resolved.
Show resolved Hide resolved
RUN mkdir -p /usr/src/importer

RUN cd ..
Expand Down
4 changes: 4 additions & 0 deletions importer/handlers/common/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,10 @@ def create_geonode_resource(
dirty_state=True,
title=layer_name,
owner=_exec.user,
extension=self.supported_file_extension_config["id"],
data_title="Original",
data_type=self.supported_file_extension_config["label"],
link_type="uploaded", # should be in geonode.base.enumerations.LINK_TYPES
files=list(
set(
list(_exec.input_params.get("files", {}).values())
Expand Down
24 changes: 16 additions & 8 deletions importer/handlers/common/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from importer.api.exception import ImportException
from importer.celery_app import importer_app
from geonode.storage.manager import storage_manager
from geonode.assets.utils import copy_assets_and_links, get_default_asset

from importer.handlers.utils import create_alternate, should_be_imported
from importer.models import ResourceHandlerInfo
Expand Down Expand Up @@ -244,10 +245,12 @@ def perform_last_step(execution_id):
_exec.save()
if _exec and not _exec.input_params.get("store_spatial_file", False):
resources = ResourceHandlerInfo.objects.filter(execution_request=_exec)
# getting all files list
resources_files = list(set(chain(*[x.resource.files for x in resources])))
# better to delete each single file since it can be a remove storage service
list(map(storage_manager.delete, resources_files))
# getting all assets list
assets = [get_default_asset(x.resource) for x in resources]
# we need to loop and cancel one by one to activate the signal
# which delete the file from the filesystem
for asset in assets:
asset.delete()

def extract_resource_to_publish(
self, files, action, layer_name, alternate, **kwargs
Expand Down Expand Up @@ -622,6 +625,10 @@ def create_geonode_resource(
dirty_state=True,
title=layer_name,
owner=_exec.user,
data_title="Original",
data_type=self.supported_file_extension_config["label"],
extension=self.supported_file_extension_config["id"],
link_type="uploaded", # should be in geonode.base.enumerations.LINK_TYPES
files=list(
set(
list(_exec.input_params.get("files", {}).values())
Expand Down Expand Up @@ -756,14 +763,15 @@ def copy_geonode_resource(
new_alternate: str,
**kwargs,
):
resource = self.create_geonode_resource(
new_resource = self.create_geonode_resource(
layer_name=data_to_update.get("title"),
alternate=new_alternate,
execution_id=str(_exec.exec_id),
files=resource.files,
files=[],
)
resource.refresh_from_db()
return resource
copy_assets_and_links(resource, target=new_resource)
new_resource.refresh_from_db()
return new_resource

def get_ogr2ogr_task_group(
self,
Expand Down
17 changes: 10 additions & 7 deletions importer/tests/unit/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from dynamic_models.models import ModelSchema, FieldSchema
from dynamic_models.exceptions import DynamicModelError, InvalidFieldNameError
from importer.models import ResourceHandlerInfo
from importer import project_dir

from importer.tests.utils import (
ImporterBaseTestSupport,
Expand All @@ -39,13 +40,14 @@
class TestCeleryTasks(ImporterBaseTestSupport):
def setUp(self):
self.user = get_user_model().objects.first()
self.existing_file = f"{project_dir}/tests/fixture/valid.gpkg"
self.exec_id = orchestrator.create_execution_request(
user=get_user_model().objects.get(username=self.user),
func_name="dummy_func",
step="dummy_step",
legacy_upload_name="dummy",
input_params={
"files": {"base_file": "/filepath"},
"files": {"base_file": self.existing_file},
# "overwrite_existing_layer": True,
"store_spatial_files": True,
},
Expand Down Expand Up @@ -82,7 +84,7 @@ def test_import_resource_should_rase_exp_if_is_invalid(
func_name="dummy_func",
step="dummy_step",
legacy_upload_name="dummy",
input_params={"files": "/filepath", "store_spatial_files": True},
input_params={"files": self.existing_file, "store_spatial_files": True},
)

is_valid.side_effect = Exception("Invalid format type")
Expand Down Expand Up @@ -116,7 +118,7 @@ def test_import_resource_should_work(
func_name="dummy_func",
step="dummy_step",
legacy_upload_name="dummy",
input_params={"files": "/filepath", "store_spatial_files": True},
input_params={"files": self.existing_file, "store_spatial_files": True},
)

import_resource(
Expand Down Expand Up @@ -189,7 +191,7 @@ def test_publish_resource_if_overwrite_should_call_the_publishing(
step="dummy_step",
legacy_upload_name="dummy",
input_params={
"files": {"base_file": "/filepath"},
"files": {"base_file": self.existing_file},
"overwrite_existing_layer": True,
"store_spatial_files": True,
},
Expand Down Expand Up @@ -244,7 +246,7 @@ def test_publish_resource_if_overwrite_should_not_call_the_publishing(
step="dummy_step",
legacy_upload_name="dummy",
input_params={
"files": {"base_file": "/filepath"},
"files": {"base_file": self.existing_file},
"overwrite_existing_layer": True,
"store_spatial_files": True,
},
Expand Down Expand Up @@ -388,7 +390,7 @@ def test_rollback_works_as_expected_vector_step(
step=conf[0], # step name
action="import",
input_params={
"files": {"base_file": "/filepath"},
"files": {"base_file": self.existing_file},
"overwrite_existing_layer": True,
"store_spatial_files": True,
"handler_module_path": "importer.handlers.gpkg.handler.GPKGFileHandler",
Expand Down Expand Up @@ -509,13 +511,14 @@ class TestDynamicModelSchema(TransactionImporterBaseTestSupport):

def setUp(self):
self.user = get_user_model().objects.first()
self.existing_file = f"{project_dir}/tests/fixture/valid.gpkg"
self.exec_id = orchestrator.create_execution_request(
user=get_user_model().objects.get(username=self.user),
func_name="dummy_func",
step="dummy_step",
legacy_upload_name="dummy",
input_params={
"files": {"base_file": "/filepath"},
"files": {"base_file": self.existing_file},
# "overwrite_existing_layer": True,
"store_spatial_files": True,
},
Expand Down