Skip to content

Commit

Permalink
[Fixres GeoNode#8690] port to master
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiagiupponi committed Feb 4, 2022
1 parent 8f07103 commit 1ba8793
Show file tree
Hide file tree
Showing 18 changed files with 334 additions and 20 deletions.
2 changes: 1 addition & 1 deletion geonode/api/resourcebase_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def build_filters(self, filters=None, ignore_bad_filters=False, **kwargs):
_metadata = {f"metadata__{_k}": _v for _k, _v in filters.items() if _k.startswith('metadata__')}
if _metadata:
orm_filters.update({"metadata_filters": _metadata})

if 'extent' in filters:
orm_filters.update({'extent': filters['extent']})
orm_filters['f_method'] = filters['f_method'] if 'f_method' in filters else 'and'
Expand Down
1 change: 1 addition & 0 deletions geonode/base/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1664,6 +1664,7 @@ def test_manager_can_edit_map(self):
}
)


class TestExtraMetadataBaseApi(GeoNodeBaseTestSupport):
def setUp(self):
self.layer = create_single_layer('single_layer')
Expand Down
2 changes: 1 addition & 1 deletion geonode/base/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1241,7 +1241,7 @@ def set_thumbnail(self, request, pk=None):
'Unable to set thumbnail',
status=status.HTTP_400_BAD_REQUEST
)

@extend_schema(
methods=["get", "put", "delete", "post"], description="Get/Update/Delete/Add extra metadata for resource"
)
Expand Down
4 changes: 3 additions & 1 deletion geonode/base/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,8 +495,10 @@ class ResourceBaseForm(TranslationModelForm):
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user', None)
super().__init__(*args, **kwargs)

if self.instance and self.instance.id and self.instance.metadata.exists():
self.fields['extra_metadata'].initial = [x.metadata for x in self.instance.metadata.all()]
self.fields['extra_metadata'].initial = [x.metadata for x in self.instance.metadata.all()]

for field in self.fields:
if field == 'featured' and self.user and not self.user.is_superuser:
self.fields[field].disabled = True
Expand Down
14 changes: 14 additions & 0 deletions geonode/base/migrations/0077_merge_20220204_1347.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Generated by Django 3.2.12 on 2022-02-04 13:47

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('base', '0076_auto_20220128_0919'),
('base', '0076_merge_20220204_1339'),
]

operations = [
]
6 changes: 4 additions & 2 deletions geonode/documents/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
TKeywordForm,
ThesaurusAvailableForm)
from geonode.base.models import (
ExtraMetadata,
Thesaurus,
TopicCategory)

Expand Down Expand Up @@ -444,7 +443,10 @@ def document_metadata(
metadata_author=new_author or document.metadata_author,
category=new_category
),
notify=True)
notify=True,
extra_metadata=json.loads(document_form.cleaned_data['extra_metadata'])
)

resource_manager.set_thumbnail(document.uuid, instance=document, overwrite=False)
document_form.save_many2many()

Expand Down
6 changes: 6 additions & 0 deletions geonode/geoapps/templates/layouts/app_panels.html
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,12 @@
<!--<p class="xxs-font-size">(Method used to represent geographic information in the dataset)</p>-->
{{ geoapp_form.spatial_representation_type }}
</div>
{% block geoapp_extra_metadata %}
<div style="margin-top: 5px">
<span><label for="{{ geoapp_form.extra_metadata|id }}">{{ geoapp_form.extra_metadata.label }}</label></span>
{{ geoapp_form.extra_metadata }}
</div>
{% endblock geoapp_extra_metadata %}
</div>
</div>
<div class="col-xs-12 col-lg-3">
Expand Down
75 changes: 74 additions & 1 deletion geonode/geoapps/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#########################################################################
from audioop import reverse
from django.contrib.auth import get_user_model

from django.test import override_settings
from geonode.geoapps.forms import GeoAppForm
from geonode.geoapps.models import GeoApp
from geonode.resource.manager import resource_manager
from geonode.tests.base import GeoNodeBaseTestSupport
Expand Down Expand Up @@ -62,3 +64,74 @@ def setUp(self):
blob='{"test_data": {"test": ["test_1","test_2","test_3"]}}'
)
)
self.user = get_user_model().objects.get(username='admin')
self.geoapp = GeoApp.objects.create(
name="name",
title="geoapp_titlte",
thumbnail_url='initial',
owner=self.user
)
self.sut = GeoAppForm

def test_resource_form_is_invalid_extra_metadata_not_json_format(self):
self.client.login(username="admin", password="admin")
url = reverse("geoapp_metadata", args=(self.geoapp.id,))
response = self.client.post(url, data={
"resource-owner": self.geoapp.owner.id,
"resource-title": "geoapp_title",
"resource-date": "2022-01-24 16:38 pm",
"resource-date_type": "creation",
"resource-language": "eng",
"resource-extra_metadata": "not-a-json"
})
expected = {"success": False, "errors": ["extra_metadata: The value provided for the Extra metadata field is not a valid JSON"]}
self.assertDictEqual(expected, response.json())

@override_settings(EXTRA_METADATA_SCHEMA={"key": "value"})
def test_resource_form_is_invalid_extra_metadata_not_schema_in_settings(self):
self.client.login(username="admin", password="admin")
url = reverse("geoapp_metadata", args=(self.geoapp.id,))
response = self.client.post(url, data={
"resource-owner": self.geoapp.owner.id,
"resource-title": "geoapp_title",
"resource-date": "2022-01-24 16:38 pm",
"resource-date_type": "creation",
"resource-language": "eng",
"resource-extra_metadata": "[{'key': 'value'}]"
})
expected = {"success": False, "errors": ["extra_metadata: EXTRA_METADATA_SCHEMA validation schema is not available for resource geoapp"]}
self.assertDictEqual(expected, response.json())

def test_resource_form_is_invalid_extra_metadata_invalids_schema_entry(self):
self.client.login(username="admin", password="admin")
url = reverse("geoapp_metadata", args=(self.geoapp.id,))
response = self.client.post(url, data={
"resource-owner": self.geoapp.owner.id,
"resource-title": "geoapp_title",
"resource-date": "2022-01-24 16:38 pm",
"resource-date_type": "creation",
"resource-language": "eng",
"resource-extra_metadata": '[{"key": "value"},{"id": "int", "filter_header": "object", "field_name": "object", "field_label": "object", "field_value": "object"}]'
})
expected = "extra_metadata: Missing keys: \'field_label\', \'field_name\', \'field_value\', \'filter_header\' at index 0 "
self.assertIn(expected, response.json()['errors'][0])

@override_settings(EXTRA_METADATA_SCHEMA={
"geoapp": {
"id": int,
"filter_header": object,
"field_name": object,
"field_label": object,
"field_value": object
}
})
def test_resource_form_is_valid_extra_metadata(self):
form = self.sut(data={
"owner": self.geoapp.owner.id,
"title": "geoapp_title",
"date": "2022-01-24 16:38 pm",
"date_type": "creation",
"language": "eng",
"extra_metadata": '[{"id": 1, "filter_header": "object", "field_name": "object", "field_label": "object", "field_value": "object"}]'
})
self.assertTrue(form.is_valid())
17 changes: 15 additions & 2 deletions geonode/geoapps/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,9 @@ def geoapp_metadata(request, geoappid, template='apps/app_metadata.html', ajax=T
vals=dict(
**geoapp_form.cleaned_data, **additional_vals
),
notify=True)
notify=True,
extra_metadata=json.loads(geoapp_form.cleaned_data['extra_metadata'])
)
resource_manager.set_thumbnail(geoapp_obj.uuid, instance=geoapp_obj, overwrite=False)

register_event(request, EventType.EVENT_CHANGE_METADATA, geoapp_obj)
Expand Down Expand Up @@ -382,7 +384,18 @@ def geoapp_metadata(request, geoappid, template='apps/app_metadata.html', ajax=T
logger.error(tb)

return HttpResponse(json.dumps({'message': message}))

elif request.method == "POST" and (not geoapp_form.is_valid(
) or not category_form.is_valid() or not tkeywords_form.is_valid()):
errors_list = {**geoapp_form.errors.as_data(), **category_form.errors.as_data(), **tkeywords_form.errors.as_data()}
logger.error(f"GeoApp Metadata form is not valid: {errors_list}")
out = {
'success': False,
"errors": [f"{x}: {y[0].messages[0]}" for x, y in errors_list.items()]
}
return HttpResponse(
json.dumps(out),
content_type='application/json',
status=400)
# - POST Request Ends here -

# Request.GET
Expand Down
6 changes: 6 additions & 0 deletions geonode/layers/templates/layouts/panels.html
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,12 @@
{{ dataset_form.spatial_representation_type }}
</div>
{% endblock dataset_spatial_representation_type %}
{% block layer_extra_metadata %}
<div style="margin-top: 5px">
<span><label for="{{ dataset_form.extra_metadata|id }}">{{ dataset_form.extra_metadata.label }}</label></span>
{{ dataset_form.extra_metadata }}
</div>
{% endblock layer_extra_metadata %}
</div>
</div>
<div class="col-xs-12 col-lg-3">
Expand Down
62 changes: 61 additions & 1 deletion geonode/layers/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
from geonode.resource.manager import resource_manager
from geonode.tests.utils import NotificationsTestsHelper
from geonode.layers.models import Dataset, Style, Attribute
from geonode.layers.forms import JSONField, LayerUploadForm
from geonode.layers.forms import DatasetForm, JSONField, LayerUploadForm
from geonode.layers.populate_datasets_data import create_dataset_data
from geonode.base.models import TopicCategory, License, Region, Link
from geonode.utils import check_ogc_backend, set_resource_default_links
Expand Down Expand Up @@ -1679,3 +1679,63 @@ def test_give_single_file_should_return_False(self):
request.FILES['base_file'] = f
actual = is_sld_upload_only(request)
self.assertFalse(actual)

class TestDatasetForm(GeoNodeBaseTestSupport):
def setUp(self) -> None:
self.user = get_user_model().objects.get(username='admin')
self.dataset = create_single_dataset("my_single_layer", owner=self.user)
self.sut = DatasetForm

def test_resource_form_is_invalid_extra_metadata_not_json_format(self):
self.client.login(username="admin", password="admin")
url = reverse("layer_metadata", args=(self.dataset.alternate,))
response = self.client.post(url, data={
"resource-owner": self.dataset.owner.id,
"resource-title": "layer_title",
"resource-date": "2022-01-24 16:38 pm",
"resource-date_type": "creation",
"resource-language": "eng",
"resource-extra_metadata": "not-a-json"
})
expected = {"success": False, "errors": ["extra_metadata: The value provided for the Extra metadata field is not a valid JSON"]}
self.assertDictEqual(expected, response.json())

@override_settings(EXTRA_METADATA_SCHEMA={"key": "value"})
def test_resource_form_is_invalid_extra_metadata_not_schema_in_settings(self):
self.client.login(username="admin", password="admin")
url = reverse("layer_metadata", args=(self.dataset.alternate,))
response = self.client.post(url, data={
"resource-owner": self.dataset.owner.id,
"resource-title": "layer_title",
"resource-date": "2022-01-24 16:38 pm",
"resource-date_type": "creation",
"resource-language": "eng",
"resource-extra_metadata": "[{'key': 'value'}]"
})
expected = {"success": False, "errors": ["extra_metadata: EXTRA_METADATA_SCHEMA validation schema is not available for resource layer"]}
self.assertDictEqual(expected, response.json())

def test_resource_form_is_invalid_extra_metadata_invalids_schema_entry(self):
self.client.login(username="admin", password="admin")
url = reverse("layer_metadata", args=(self.dataset.alternate,))
response = self.client.post(url, data={
"resource-owner": self.dataset.owner.id,
"resource-title": "layer_title",
"resource-date": "2022-01-24 16:38 pm",
"resource-date_type": "creation",
"resource-language": "eng",
"resource-extra_metadata": '[{"key": "value"},{"id": "int", "filter_header": "object", "field_name": "object", "field_label": "object", "field_value": "object"}]'
})
expected = "extra_metadata: Missing keys: \'field_label\', \'field_name\', \'field_value\', \'filter_header\' at index 0 "
self.assertIn(expected, response.json()['errors'][0])

def test_resource_form_is_valid_extra_metadata(self):
form = self.sut(instance=self.dataset, data={
"owner": self.dataset.owner.id,
"title": "layer_title",
"date": "2022-01-24 16:38 pm",
"date_type": "creation",
"language": "eng",
"extra_metadata": '[{"id": 1, "filter_header": "object", "field_name": "object", "field_label": "object", "field_value": "object"}]'
})
self.assertTrue(form.is_valid())
11 changes: 8 additions & 3 deletions geonode/layers/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,8 +551,7 @@ def dataset_metadata(
logger.error(f"Dataset Attributes form is not valid: {attribute_form.errors}")
out = {
'success': False,
'errors': [
re.sub(re.compile('<.*?>'), '', str(err)) for err in attribute_form.errors]
"errors": [f"{x}: {y[0].messages[0]}" for x, y in attribute_form.errors.as_data().items()]
}
return HttpResponse(
json.dumps(out),
Expand Down Expand Up @@ -736,7 +735,13 @@ def dataset_metadata(
tb = traceback.format_exc()
logger.error(tb)

resource_manager.update(layer.uuid, instance=layer, notify=True)
resource_manager.update(
layer.uuid,
instance=layer,
notify=True,
extra_metadata=json.loads(dataset_form.cleaned_data['extra_metadata'])

)
return HttpResponse(json.dumps({'message': message}))

if settings.ADMIN_MODERATE_UPLOADS:
Expand Down
6 changes: 6 additions & 0 deletions geonode/maps/templates/layouts/map_panels.html
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,12 @@
<!--<p class="xxs-font-size">(Method used to represent geographic information in the dataset)</p>-->
{{ map_form.spatial_representation_type }}
</div>
{% block map_extra_metadata %}
<div style="margin-top: 5px">
<span><label for="{{ map_form.extra_metadata|id }}">{{ map_form.extra_metadata.label }}</label></span>
{{ map_form.extra_metadata }}
</div>
{% endblock map_extra_metadata %}
</div>
</div>
<div class="col-xs-12 col-lg-3">
Expand Down
Loading

0 comments on commit 1ba8793

Please sign in to comment.