Skip to content

Commit

Permalink
Allows metadata upload with different UUID
Browse files Browse the repository at this point in the history
Co-authored-by: ahmdthr <[email protected]>
  • Loading branch information
ridoo and ahmdthr authored Dec 18, 2023
1 parent fb7ced3 commit 785fbf8
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 23 deletions.
2 changes: 1 addition & 1 deletion geonode/layers/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,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):
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 @@ -63,6 +63,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 @@ -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):
Expand Down Expand Up @@ -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":{}}',
Expand All @@ -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")
Expand Down
11 changes: 6 additions & 5 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 @@ -186,9 +182,14 @@ def dataset_upload_metadata(request):
upload_session = updated_dataset.upload_session
upload_session.processed = True
upload_session.save()
status_code = 200
out["success"] = True
status_code = 200
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
out["errors"] = "Dataset selected does not exists"
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

0 comments on commit 785fbf8

Please sign in to comment.