From 28721cc9154104554b18f1ed8b97007bec170cb0 Mon Sep 17 00:00:00 2001 From: ahmdthr Date: Tue, 30 Jan 2024 06:06:17 +0100 Subject: [PATCH 1/5] Fixes issue#11903. The metadata upload (either via forms or REST API) updates the resource even when the UUID is different. --- geonode/layers/api/tests.py | 2 +- geonode/layers/api/views.py | 9 ++++----- .../datasets/dataset_metadata_upload.html | 1 + geonode/layers/tests.py | 20 +++++++++---------- geonode/layers/views.py | 8 ++++---- geonode/locale/en/LC_MESSAGES/django.po | 3 +++ geonode/static/geonode/js/upload/LayerInfo.js | 12 +++++++++-- 7 files changed, 33 insertions(+), 22 deletions(-) diff --git a/geonode/layers/api/tests.py b/geonode/layers/api/tests.py index 79b4330977b..ed72c5053a5 100644 --- a/geonode/layers/api/tests.py +++ b/geonode/layers/api/tests.py @@ -660,7 +660,7 @@ def test_valid_metadata_file_with_different_uuid(self): f = open(self.exml_path, "r") put_data = {"metadata_file": f} response = self.client.put(url, data=put_data) - self.assertEqual(500, response.status_code) + self.assertEqual(200, response.status_code) def test_permissions_for_not_permitted_user(self): get_user_model().objects.create_user( diff --git a/geonode/layers/api/views.py b/geonode/layers/api/views.py index 218216a17bd..629367c3440 100644 --- a/geonode/layers/api/views.py +++ b/geonode/layers/api/views.py @@ -141,17 +141,16 @@ def metadata(self, request, pk=None, *args, **kwargs): dataset_uuid, vals, regions, keywords, _ = parse_metadata(open(metadata_file).read()) except Exception: raise InvalidMetadataException(detail="Unsupported metadata format") - if dataset_uuid and dataset.uuid != dataset_uuid: - raise InvalidMetadataException( - detail="The UUID identifier from the XML Metadata, is different from the one saved" - ) try: updated_dataset = update_resource(dataset, metadata_file, regions, keywords, vals) updated_dataset.save() # This also triggers the recreation of the XML metadata file according to the updated values except Exception: raise GeneralDatasetException(detail="Failed to update metadata") out["success"] = True - out["message"] = ["Metadata successfully updated"] + out_message = "Metadata successfully updated" + if dataset_uuid and dataset.uuid != dataset_uuid: + out_message += " The UUID identifier from the XML Metadata is different from the one saved" + out["message"] = [out_message] return Response(out) except Exception as e: raise e diff --git a/geonode/layers/templates/datasets/dataset_metadata_upload.html b/geonode/layers/templates/datasets/dataset_metadata_upload.html index 6ef4d3d2e55..a5a0168e9ef 100644 --- a/geonode/layers/templates/datasets/dataset_metadata_upload.html +++ b/geonode/layers/templates/datasets/dataset_metadata_upload.html @@ -63,6 +63,7 @@

{% trans "Files to be uploaded" %}

+
{% trans "WARNING" %}: {% trans "This will most probably overwrite the current metadata!" %}
{% trans "Clear" %} {% trans "Upload files" %}
diff --git a/geonode/layers/tests.py b/geonode/layers/tests.py index c4d0bf88f0b..dab60c4397a 100644 --- a/geonode/layers/tests.py +++ b/geonode/layers/tests.py @@ -1003,9 +1003,10 @@ def test_xml_should_update_the_dataset_with_the_expected_values(self): prev_dataset = Dataset.objects.get(typename="geonode:single_point") self.assertEqual(0, prev_dataset.keywords.count()) resp = self.client.post(reverse("dataset_upload"), params) - self.assertEqual(404, resp.status_code) + self.assertEqual(200, resp.status_code) self.assertEqual( - resp.json()["errors"], "The UUID identifier from the XML Metadata, is different from the one saved" + resp.json()["warning"], + "WARNING: The XML's UUID was ignored while updating this dataset's metadata because that UUID is already present in this system. The rest of the XML's metadata was applied.", ) def test_sld_should_raise_500_if_is_invalid(self): @@ -1053,10 +1054,10 @@ def test_sld_should_update_the_dataset_with_the_expected_values(self): self.assertIsNotNone(updated_dataset.styles.first()) self.assertEqual(layer.styles.first().sld_title, updated_dataset.styles.first().sld_title) - def test_xml_should_raise_an_error_if_the_uuid_is_changed(self): + def test_xml_should_not_raise_an_error_if_the_uuid_is_changed(self): """ If the UUID coming from the XML and the one saved in the DB are different - The system should raise an error + The system should not raise an error, instead it should simply update the values """ params = { "permissions": '{ "users": {"AnonymousUser": ["view_resourcebase"]} , "groups":{}}', @@ -1072,12 +1073,11 @@ def test_xml_should_raise_an_error_if_the_uuid_is_changed(self): prev_dataset = Dataset.objects.get(typename="geonode:single_point") self.assertEqual(0, prev_dataset.keywords.count()) resp = self.client.post(reverse("dataset_upload"), params) - self.assertEqual(404, resp.status_code) - expected = { - "success": False, - "errors": "The UUID identifier from the XML Metadata, is different from the one saved", - } - self.assertDictEqual(expected, resp.json()) + self.assertEqual(200, resp.status_code) + self.assertEqual( + resp.json()["warning"], + "WARNING: The XML's UUID was ignored while updating this dataset's metadata because that UUID is already present in this system. The rest of the XML's metadata was applied.", + ) def test_will_raise_exception_for_replace_vector_dataset_with_raster(self): layer = Dataset.objects.get(name="single_point") diff --git a/geonode/layers/views.py b/geonode/layers/views.py index f7729d2ce09..1dce2c5b9e8 100644 --- a/geonode/layers/views.py +++ b/geonode/layers/views.py @@ -171,10 +171,6 @@ def dataset_upload_metadata(request): ) if layer: dataset_uuid, vals, regions, keywords, _ = parse_metadata(open(base_file).read()) - if dataset_uuid and layer.uuid != dataset_uuid: - out["success"] = False - out["errors"] = "The UUID identifier from the XML Metadata, is different from the one saved" - return HttpResponse(json.dumps(out), content_type="application/json", status=404) updated_dataset = update_resource(layer, base_file, regions, keywords, vals) updated_dataset.save() out["status"] = ["finished"] @@ -188,6 +184,10 @@ def dataset_upload_metadata(request): upload_session.save() status_code = 200 out["success"] = True + if dataset_uuid and layer.uuid != dataset_uuid: + out[ + "warning" + ] = "WARNING: The XML's UUID was ignored while updating this dataset's metadata because that UUID is already present in this system. The rest of the XML's metadata was applied." return HttpResponse(json.dumps(out), content_type="application/json", status=status_code) else: out["success"] = False diff --git a/geonode/locale/en/LC_MESSAGES/django.po b/geonode/locale/en/LC_MESSAGES/django.po index 1e094b47828..8b75867f235 100644 --- a/geonode/locale/en/LC_MESSAGES/django.po +++ b/geonode/locale/en/LC_MESSAGES/django.po @@ -1168,6 +1168,9 @@ msgstr "Files to be uploaded" msgid "Is Upload Metadata XML Form" msgstr "Is Upload Metadata XML Form" +msgid "This will most probably overwrite the current metadata!" +msgstr "This will most probably overwrite the current metadata!" + msgid "Upload files" msgstr "Upload files" diff --git a/geonode/static/geonode/js/upload/LayerInfo.js b/geonode/static/geonode/js/upload/LayerInfo.js index 4d4c4385172..574776a8977 100644 --- a/geonode/static/geonode/js/upload/LayerInfo.js +++ b/geonode/static/geonode/js/upload/LayerInfo.js @@ -458,7 +458,15 @@ define(function (require, exports) { } catch (err) { // pass } - var info_message = gettext('Your ' + resourceType +' was successfully created.'); + var info_message = '' + var level = 'alert-success' + if (resp.warning){ + info_message = resp.warning + level = 'alert-warning' + } + else{ + info_message = gettext('Your ' + resourceType +' was successfully created.'); + } var a = '' + gettext(resourceType.capitalize() + ' Info') + '   '; if(resourceType == 'dataset') { // Only Layers have Metadata and SLD Upload features for the moment @@ -474,7 +482,7 @@ define(function (require, exports) { } self.logStatus({ msg: '

' + info_message + '
' + msg_col + '
' + a + '

', - level: 'alert-success', + level: level, empty: 'true' }); }; From 935de96e36a4d1b2e46395d618f23c04d93015ef Mon Sep 17 00:00:00 2001 From: ahmdthr Date: Thu, 15 Feb 2024 11:18:19 +0100 Subject: [PATCH 2/5] Fixed linting issues. --- geonode/layers/views.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/geonode/layers/views.py b/geonode/layers/views.py index 149dae3f770..f7c54f48799 100644 --- a/geonode/layers/views.py +++ b/geonode/layers/views.py @@ -187,7 +187,8 @@ def dataset_upload_metadata(request): if dataset_uuid and layer.uuid != dataset_uuid: out[ "warning" - ] = "WARNING: The XML's UUID was ignored while updating this dataset's metadata because that UUID is already present in this system. The rest of the XML's metadata was applied." + ] = """WARNING: The XML's UUID was ignored while updating this dataset's metadata because + that UUID is already present in this system. The rest of the XML's metadata was applied.""" return HttpResponse(json.dumps(out), content_type="application/json", status=status_code) else: out["success"] = False From d964f557babfa8e40930a4aa1d06b8950273e6d1 Mon Sep 17 00:00:00 2001 From: ahmdthr Date: Thu, 15 Feb 2024 12:28:26 +0100 Subject: [PATCH 3/5] Fixed continuation of the warning message when the UUID is not same. Previously the message was split into two lines which was causing two tests to fail. --- geonode/layers/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/geonode/layers/views.py b/geonode/layers/views.py index f7c54f48799..4f015bd9824 100644 --- a/geonode/layers/views.py +++ b/geonode/layers/views.py @@ -187,7 +187,7 @@ def dataset_upload_metadata(request): if dataset_uuid and layer.uuid != dataset_uuid: out[ "warning" - ] = """WARNING: The XML's UUID was ignored while updating this dataset's metadata because + ] = """WARNING: The XML's UUID was ignored while updating this dataset's metadata because \ that UUID is already present in this system. The rest of the XML's metadata was applied.""" return HttpResponse(json.dumps(out), content_type="application/json", status=status_code) else: From 1c6b0cbc46f6c59812df42e9ba14798e3fd35e32 Mon Sep 17 00:00:00 2001 From: ahmdthr Date: Thu, 15 Feb 2024 13:13:07 +0100 Subject: [PATCH 4/5] Fixed string indentation for the warning message. --- geonode/layers/views.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/geonode/layers/views.py b/geonode/layers/views.py index 4f015bd9824..50ba0ca7c45 100644 --- a/geonode/layers/views.py +++ b/geonode/layers/views.py @@ -187,8 +187,8 @@ def dataset_upload_metadata(request): if dataset_uuid and layer.uuid != dataset_uuid: out[ "warning" - ] = """WARNING: The XML's UUID was ignored while updating this dataset's metadata because \ - that UUID is already present in this system. The rest of the XML's metadata was applied.""" + ] = (" ").join("WARNING: The XML's UUID was ignored while updating this dataset's metadata because \ + that UUID is already present in this system. The rest of the XML's metadata was applied.".split()) return HttpResponse(json.dumps(out), content_type="application/json", status=status_code) else: out["success"] = False From 60a8ecab9d85506c86cb79263ae6e5a86357130e Mon Sep 17 00:00:00 2001 From: ahmdthr Date: Thu, 15 Feb 2024 13:14:54 +0100 Subject: [PATCH 5/5] Fixed linting issues. --- geonode/layers/views.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/geonode/layers/views.py b/geonode/layers/views.py index 50ba0ca7c45..1b73d95cba3 100644 --- a/geonode/layers/views.py +++ b/geonode/layers/views.py @@ -185,10 +185,10 @@ def dataset_upload_metadata(request): status_code = 200 out["success"] = True if dataset_uuid and layer.uuid != dataset_uuid: - out[ - "warning" - ] = (" ").join("WARNING: The XML's UUID was ignored while updating this dataset's metadata because \ - that UUID is already present in this system. The rest of the XML's metadata was applied.".split()) + out["warning"] = (" ").join( + "WARNING: The XML's UUID was ignored while updating this dataset's metadata because \ + that UUID is already present in this system. The rest of the XML's metadata was applied.".split() + ) return HttpResponse(json.dumps(out), content_type="application/json", status=status_code) else: out["success"] = False