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

[Fixes #11903] The metadata upload (either via forms or REST API) updates the resource even when the UUID is different. #11904

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion geonode/layers/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
9 changes: 4 additions & 5 deletions geonode/layers/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ <h4>{% trans "Files to be uploaded" %}</h4>
</section>

<section>
<div class="btn-danger"><strong>{% trans "WARNING" %}: </strong>{% trans "This will most probably overwrite the current metadata!" %}</div>
<a href="{% url "dataset_metadata_upload" resource.alternate %}" id="clear-button" class="btn btn-default">{% trans "Clear" %}</a>
<a href="#" id="upload-button" class="btn btn-primary">{% trans "Upload files" %}</a>
</section>
Expand Down
20 changes: 10 additions & 10 deletions geonode/layers/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -984,9 +984,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):
Expand Down Expand Up @@ -1034,10 +1035,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":{}}',
Expand All @@ -1053,12 +1054,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")
Expand Down
9 changes: 5 additions & 4 deletions geonode/layers/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -188,6 +184,11 @@ def dataset_upload_metadata(request):
upload_session.save()
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()
)
return HttpResponse(json.dumps(out), content_type="application/json", status=status_code)
else:
out["success"] = False
Expand Down
3 changes: 3 additions & 0 deletions geonode/locale/en/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
12 changes: 10 additions & 2 deletions geonode/static/geonode/js/upload/LayerInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<a href="' + resp.url + '" class="btn btn-success">' + gettext(resourceType.capitalize() + ' Info') + '</a>&nbsp;&nbsp;&nbsp;';
if(resourceType == 'dataset') {
// Only Layers have Metadata and SLD Upload features for the moment
Expand All @@ -474,7 +482,7 @@ define(function (require, exports) {
}
self.logStatus({
msg: '<p>' + info_message + '<br/>' + msg_col + '<br/>' + a + '</p>',
level: 'alert-success',
level: level,
empty: 'true'
});
};
Expand Down