Skip to content

Commit 139b86e

Browse files
[Fixes #14042] deprecate overwrite_existing_layers in favor of action… (#14043)
* [Fixes #14042] deprecate overwrite_existing_layers in favor of action usage
1 parent 2e61721 commit 139b86e

15 files changed

Lines changed: 84 additions & 67 deletions

File tree

geonode/upload/api/serializer.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,8 @@ class Meta:
5151
ref_name = "OverwriteImporterSerializer"
5252
model = ResourceBase
5353
view_name = "importer_upload"
54-
fields = ImporterSerializer.Meta.fields + (
55-
"overwrite_existing_layer",
56-
"resource_pk",
57-
)
54+
fields = ImporterSerializer.Meta.fields + ("resource_pk",)
5855

59-
overwrite_existing_layer = serializers.BooleanField(required=True)
6056
resource_pk = serializers.IntegerField(required=True)
6157

6258

geonode/upload/celery_tasks.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ def publish_resource(
399399
)
400400
_exec = orchestrator.get_execution_object(execution_id)
401401
_files = _exec.input_params.get("files")
402-
_overwrite = _exec.input_params.get("overwrite_existing_layer")
402+
_overwrite = action == ira.REPLACE.value
403403

404404
_publisher = DataPublisher(handler_module_path)
405405
kwargs.update({"exec_id": execution_id})
@@ -509,7 +509,7 @@ def create_geonode_resource(
509509
handler_module_path = handler_module_path or _exec.input_params.get("handler_module_path")
510510

511511
handler = import_string(handler_module_path)()
512-
_overwrite = _exec.input_params.get("overwrite_existing_layer")
512+
_overwrite = action == ira.REPLACE.value
513513

514514
if _overwrite:
515515
resource = handler.overwrite_geonode_resource(
@@ -975,7 +975,7 @@ def rollback(self, *args, **kwargs):
975975
)
976976

977977
handler = import_string(handler_module_path)()
978-
if exec_object.input_params.get("overwrite_existing_layer"):
978+
if exec_object.action == ira.REPLACE.value:
979979
logger.warning("Rollback is skipped for the overwrite")
980980
else:
981981
handler.rollback(exec_id, rollback_from_step, action_to_rollback, *args, **kwargs)

geonode/upload/handlers/base.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,25 @@ def perform_last_step(execution_id):
184184

185185
return _exec
186186

187+
def evaluate_exec_prev_status(self, action, resource_pk=None):
188+
"""
189+
Required to evaluate if the resource can be replaced/upsert
190+
based on it's previous status. this is required for 3dtiles for example
191+
but the handler can deny some operations on some resources
192+
"""
193+
return True, None
194+
187195
def pre_processing(self, files, execution_id, **kwargs):
188196
from geonode.upload.orchestrator import orchestrator
189197

190198
_exec_obj = orchestrator.get_execution_object(execution_id)
191199
_data = _exec_obj.input_params.copy()
200+
201+
if _exec_obj.action in (ira.REPLACE.value, ira.UPSERT.value):
202+
if resource_pk := _data.get("resource_pk"):
203+
can_proceed, reason = self.evaluate_exec_prev_status(_exec_obj.action, resource_pk)
204+
if not can_proceed:
205+
raise ImportException(reason)
192206
# unzipping the file
193207
if "zip_file" in files or "kmz_file" in files:
194208
# if a zipfile is provided, we need to unzip it before searching for an handler

geonode/upload/handlers/common/metadata.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ def extract_params_from_data(_data, action=None):
5555
return {
5656
"dataset_title": _data.pop("dataset_title", None),
5757
"skip_existing_layers": _data.pop("skip_existing_layers", "False"),
58-
"overwrite_existing_layer": _data.pop("overwrite_existing_layer", False),
5958
"resource_pk": _data.pop("resource_pk", None),
6059
"store_spatial_file": _data.pop("store_spatial_files", "True"),
6160
"action": _data.pop("action"),

geonode/upload/handlers/common/raster.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
from geonode.upload.handlers.utils import create_alternate, should_be_imported
3939
from geonode.upload.models import ResourceHandlerInfo
4040
from geonode.upload.orchestrator import orchestrator
41-
from geonode.upload.utils import find_key_recursively
41+
from geonode.upload.utils import find_key_recursively, ImporterRequestAction as ira
4242
from osgeo import gdal
4343
from geonode.upload.celery_app import importer_app
4444
from geonode.storage.manager import storage_manager
@@ -123,7 +123,6 @@ def extract_params_from_data(_data, action=None):
123123

124124
return {
125125
"skip_existing_layers": _data.pop("skip_existing_layers", "False"),
126-
"overwrite_existing_layer": _data.pop("overwrite_existing_layer", False),
127126
"resource_pk": _data.pop("resource_pk", None),
128127
"store_spatial_file": _data.pop("store_spatial_files", "True"),
129128
"action": _data.pop("action", "upload"),
@@ -276,7 +275,7 @@ def import_resource(self, files: dict, execution_id: str, **kwargs) -> str:
276275
# start looping on the layers available
277276
layer_name = self.fixup_name(filename)
278277

279-
should_be_overwritten = _exec.input_params.get("overwrite_existing_layer")
278+
should_be_overwritten = _exec.action == ira.REPLACE.value
280279
# should_be_imported check if the user+layername already exists or not
281280
if should_be_imported(
282281
layer_name,
@@ -318,7 +317,7 @@ def import_resource(self, files: dict, execution_id: str, **kwargs) -> str:
318317
"geonode.upload.import_resource",
319318
layer_name,
320319
alternate,
321-
exa.UPLOAD.value,
320+
_input.get("action", exa.UPLOAD.value),
322321
)
323322
)
324323
return layer_name, alternate, execution_id
@@ -350,7 +349,7 @@ def create_geonode_resource(
350349
getattr(settings, "CASCADE_WORKSPACE", "geonode"),
351350
)
352351

353-
_overwrite = _exec.input_params.get("overwrite_existing_layer", False)
352+
_overwrite = _exec.action == ira.REPLACE.value
354353
# if the layer exists, we just update the information of the dataset by
355354
# let it recreate the catalogue
356355
if not saved_dataset.exists() and _overwrite:
@@ -398,7 +397,7 @@ def overwrite_geonode_resource(
398397

399398
dataset = resource_type.objects.filter(alternate__icontains=alternate, owner=_exec.user)
400399

401-
_overwrite = _exec.input_params.get("overwrite_existing_layer", False)
400+
_overwrite = _exec.action == ira.REPLACE.value
402401
# if the layer exists, we just update the information of the dataset by
403402
# let it recreate the catalogue
404403

geonode/upload/handlers/common/remote.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ def extract_params_from_data(_data, action=None):
115115
"title": _data.pop("title", None),
116116
"url": _data.pop("url", None),
117117
"type": _data.pop("type", None),
118-
"overwrite_existing_layer": _data.pop("overwrite_existing_layer", False),
119118
}, _data
120119

121120
def pre_validation(self, files, execution_id, **kwargs):
@@ -144,7 +143,7 @@ def import_resource(self, files: dict, execution_id: str, **kwargs) -> str:
144143
# start looping on the layers available
145144
layer_name = self.fixup_name(title)
146145

147-
should_be_overwritten = _exec.input_params.get("overwrite_existing_layer")
146+
should_be_overwritten = _exec.action == ira.REPLACE.value
148147

149148
payload_alternate = params.get("remote_resource_id", None)
150149

@@ -281,7 +280,7 @@ def overwrite_geonode_resource(
281280
_exec = self._get_execution_request_object(execution_id)
282281
resource = resource_type.objects.filter(alternate__icontains=alternate, owner=_exec.user)
283282

284-
_overwrite = _exec.input_params.get("overwrite_existing_layer", False)
283+
_overwrite = _exec.action == ira.REPLACE.value
285284
# if the layer exists, we just update the information of the dataset by
286285
# let it recreate the catalogue
287286
if resource.exists() and _overwrite:

geonode/upload/handlers/common/serializer.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Meta:
2727
ref_name = "RemoteResourceSerializer"
2828
model = ResourceBase
2929
view_name = "importer_upload"
30-
fields = ("url", "title", "type", "action", "overwrite_existing_layer")
30+
fields = ("url", "title", "type", "action")
3131

3232
url = serializers.URLField(required=True, help_text="URL of the remote service / resource")
3333
title = serializers.CharField(required=True, help_text="Title of the resource. Can be None or Empty")
@@ -36,5 +36,3 @@ class Meta:
3636
help_text="Remote resource type, for example wms or 3dtiles. Is used by the handler to understand if can handle the resource",
3737
)
3838
action = serializers.CharField(required=False, default=exa.UPLOAD.value)
39-
40-
overwrite_existing_layer = serializers.BooleanField(required=False, default=False)

geonode/upload/handlers/common/vector.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ def extract_params_from_data(_data, action=None):
193193

194194
return {
195195
"skip_existing_layers": _data.pop("skip_existing_layers", "False"),
196-
"overwrite_existing_layer": _data.pop("overwrite_existing_layer", False),
197196
"resource_pk": _data.pop("resource_pk", None),
198197
"store_spatial_file": _data.pop("store_spatial_files", "True"),
199198
"action": _data.pop("action", "upload"),
@@ -229,6 +228,17 @@ def pre_validation(self, files, execution_id, **kwargs):
229228
Maybe a file rename, assign the resource to the execution_id
230229
"""
231230

231+
def evaluate_exec_prev_status(self, action, resource_pk):
232+
if not resource_pk:
233+
return True, None
234+
res = ResourceBase.objects.filter(pk=resource_pk).first()
235+
if not res:
236+
return True, None
237+
if res.subtype in ["3dtiles"] and action in (ira.REPLACE.value, ira.UPSERT.value):
238+
reason = "Replace or Upsert are not possible on an existing 3Dtile"
239+
return False, reason
240+
return True, None
241+
232242
def overwrite_geoserver_resource(self, resource, catalog, store, workspace):
233243
"""
234244
We dont need to do anything for now.
@@ -456,7 +466,7 @@ def import_resource(self, files: dict, execution_id: str, **kwargs) -> str:
456466
for index, layer in enumerate(layers, start=1):
457467
layer_name = self.fixup_name(layer.GetName())
458468

459-
should_be_overwritten = _exec.input_params.get("overwrite_existing_layer")
469+
should_be_overwritten = _exec.action == ira.REPLACE.value
460470
# should_be_imported check if the user+layername already exists or not
461471
if (
462472
should_be_imported(
@@ -790,7 +800,7 @@ def create_geonode_resource(
790800
getattr(settings, "CASCADE_WORKSPACE", "geonode"),
791801
)
792802

793-
_overwrite = _exec.input_params.get("overwrite_existing_layer", False)
803+
_overwrite = _exec.action == ira.REPLACE.value
794804
# if the layer exists, we just update the information of the dataset by
795805
# let it recreate the catalogue
796806
if not saved_dataset.exists() and _overwrite:
@@ -843,7 +853,7 @@ def overwrite_geonode_resource(
843853

844854
dataset = resource_type.objects.filter(pk=_exec.input_params.get("resource_pk"), owner=_exec.user)
845855

846-
_overwrite = _exec.input_params.get("overwrite_existing_layer", False)
856+
_overwrite = _exec.action == ira.REPLACE.value
847857
# if the layer exists, we just update the information of the dataset by
848858
# let it recreate the catalogue
849859
if dataset.exists() and _overwrite:

geonode/upload/handlers/shapefile/handler.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
#
1818
#########################################################################
19-
import ast
2019
import json
2120
import logging
2221
import codecs
@@ -85,10 +84,7 @@ def has_serializer(data) -> bool:
8584
if data.get("action") == ira.UPSERT.value:
8685
return False
8786
if _base.endswith("shp") if isinstance(_base, str) else _base.name.endswith("shp"):
88-
is_overwrite_flow = data.get("overwrite_existing_layer", False)
89-
if isinstance(is_overwrite_flow, str):
90-
is_overwrite_flow = ast.literal_eval(is_overwrite_flow.title())
91-
return OverwriteShapeFileSerializer if is_overwrite_flow else ShapeFileSerializer
87+
return OverwriteShapeFileSerializer if data.get("action") == ira.REPLACE.value else ShapeFileSerializer
9288
return False
9389

9490
@staticmethod
@@ -103,7 +99,6 @@ def extract_params_from_data(_data, action=None):
10399

104100
additional_params = {
105101
"skip_existing_layers": _data.pop("skip_existing_layers", "False"),
106-
"overwrite_existing_layer": _data.pop("overwrite_existing_layer", False),
107102
"resource_pk": _data.pop("resource_pk", None),
108103
"store_spatial_file": _data.pop("store_spatial_files", "True"),
109104
"action": _data.pop("action", "upload"),

geonode/upload/handlers/shapefile/serializer.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ class Meta:
3535
"xml_file",
3636
"sld_file",
3737
"store_spatial_files",
38-
"overwrite_existing_layer",
3938
"skip_existing_layers",
4039
"action",
4140
)
@@ -47,7 +46,6 @@ class Meta:
4746
xml_file = serializers.FileField(required=False)
4847
sld_file = serializers.FileField(required=False)
4948
store_spatial_files = serializers.BooleanField(required=False, default=True)
50-
overwrite_existing_layer = serializers.BooleanField(required=False, default=False)
5149
skip_existing_layers = serializers.BooleanField(required=False, default=False)
5250
action = serializers.CharField(required=False, default=exa.UPLOAD.value)
5351

@@ -57,10 +55,6 @@ class Meta:
5755
ref_name = "ShapeFileSerializer"
5856
model = ResourceBase
5957
view_name = "importer_upload"
60-
fields = ShapeFileSerializer.Meta.fields + (
61-
"overwrite_existing_layer",
62-
"resource_pk",
63-
)
58+
fields = ShapeFileSerializer.Meta.fields + ("resource_pk",)
6459

65-
overwrite_existing_layer = serializers.BooleanField(required=True)
6660
resource_pk = serializers.IntegerField(required=True)

0 commit comments

Comments
 (0)