Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions geonode/base/fixtures/initial_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@
"model": "base.spatialrepresentationtype",
"fields": {
"is_choice": true,
"gn_description": "vector data is used to represent geographic data",
"gn_description": "Vector",
"identifier": "vector",
"description": "vector data is used to represent geographic data"
}
Expand All @@ -234,7 +234,7 @@
"model": "base.spatialrepresentationtype",
"fields": {
"is_choice": true,
"gn_description": "grid data is used to represent geographic data",
"gn_description": "Grid",
"identifier": "grid",
"description": "grid data is used to represent geographic data"
}
Expand All @@ -244,7 +244,7 @@
"model": "base.spatialrepresentationtype",
"fields": {
"is_choice": true,
"gn_description": "textual or tabular data is used to represent geographic data",
"gn_description": "Text table",
"identifier": "textTable",
"description": "textual or tabular data is used to represent geographic data"
}
Expand All @@ -254,7 +254,7 @@
"model": "base.spatialrepresentationtype",
"fields": {
"is_choice": true,
"gn_description": "triangulated irregular network",
"gn_description": "TIN / Triangulated irregular network",
"identifier": "tin",
"description": "triangulated irregular network"
}
Expand All @@ -264,7 +264,7 @@
"model": "base.spatialrepresentationtype",
"fields": {
"is_choice": true,
"gn_description": "three-dimensional view formed by the intersecting homologous rays of an overlapping pair of images",
"gn_description": "Stereo model",
"identifier": "stereoModel",
"description": "three-dimensional view formed by the intersecting homologous rays of an overlapping pair of images"
}
Expand All @@ -274,7 +274,7 @@
"model": "base.spatialrepresentationtype",
"fields": {
"is_choice": true,
"gn_description": "scene from a video recording",
"gn_description": "Video",
"identifier": "video",
"description": "scene from a video recording"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from django.db import migrations

# identifier -> (original fixture gn_description, new short label)
LABELS = {
"vector": ("vector data is used to represent geographic data", "Vector"),
"grid": ("grid data is used to represent geographic data", "Grid"),
"textTable": ("textual or tabular data is used to represent geographic data", "Text table"),
"tin": ("triangulated irregular network", "TIN / Triangulated irregular network"),
"stereoModel": (
"three-dimensional view formed by the intersecting homologous rays of an overlapping pair of images",
"Stereo model",
),
"video": ("scene from a video recording", "Video"),
}


def set_short_labels(apps, schema_editor):
"""
Replaces gn_description with a short, human-readable label for each
SpatialRepresentationType, but only for rows still holding the original
fixture description, so any value already customized by a user is left alone.
"""
SpatialRepresentationType = apps.get_model("base", "SpatialRepresentationType")
for identifier, (original, label) in LABELS.items():
SpatialRepresentationType.objects.filter(identifier=identifier, gn_description=original).update(
gn_description=label
)


def restore_original_descriptions(apps, schema_editor):
SpatialRepresentationType = apps.get_model("base", "SpatialRepresentationType")
for identifier, (original, label) in LABELS.items():
SpatialRepresentationType.objects.filter(identifier=identifier, gn_description=label).update(
gn_description=original
)


class Migration(migrations.Migration):

dependencies = [
("base", "0102_alter_resourcebase_options"),
]

operations = [
migrations.RunPython(set_short_labels, restore_original_descriptions),
]
5 changes: 5 additions & 0 deletions geonode/metadata/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,10 @@
views.licenses_autocomplete,
name="metadata_autocomplete_licenses",
),
path(
r"autocomplete/spatial_representation_types",
views.spatial_representation_types_autocomplete,
name="metadata_autocomplete_spatial_representation_types",
),
# path(r"autocomplete/users", login_required(ProfileAutocomplete.as_view()), name="metadata_autocomplete_users"),
]
27 changes: 26 additions & 1 deletion geonode/metadata/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@
from django.db.models import Q

from geonode.base.api.permissions import UserHasPerms
from geonode.base.models import ResourceBase, ThesaurusKeyword, ThesaurusKeywordLabel, TopicCategory, License
from geonode.base.models import (
ResourceBase,
ThesaurusKeyword,
ThesaurusKeywordLabel,
TopicCategory,
License,
SpatialRepresentationType,
)
from geonode.base.utils import remove_country_from_languagecode
from geonode.base.views import LinkedResourcesAutocomplete, RegionAutocomplete, HierarchicalKeywordAutocomplete
from geonode.catalogue.models import update_csw_metadata
Expand Down Expand Up @@ -295,6 +302,24 @@ def categories_autocomplete(request: WSGIRequest):
return JsonResponse({"results": ret})


def spatial_representation_types_autocomplete(request: WSGIRequest):
qs = SpatialRepresentationType.objects.order_by("identifier")

if q := request.GET.get("q", None):
qs = qs.filter(gn_description__icontains=q)

ret = []
for record in qs.all():
ret.append(
{
"id": record.identifier,
"label": _(record.gn_description),
}
)

return JsonResponse({"results": ret})


def licenses_autocomplete(request: WSGIRequest):
qs = License.objects.order_by("name")

Expand Down
20 changes: 11 additions & 9 deletions geonode/metadata/handlers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,21 +149,23 @@ def deserialize(cls, field_value):
class SpatialRepresentationTypeSubHandler(SubHandler):
@classmethod
def update_subschema(cls, subschema, lang=None):
subschema["oneOf"] = [
{"const": tc.identifier, "title": tc.identifier, "description": tc.description}
for tc in SpatialRepresentationType.objects.order_by("identifier")
]
MetadataHandler._add_oneof_null_option(subschema)
subschema["ui:options"] = {
"geonode-ui:autocomplete": reverse("metadata_autocomplete_spatial_representation_types"),
}

@classmethod
def serialize(cls, db_value):
if isinstance(db_value, SpatialRepresentationType):
return db_value.identifier
return db_value
if db_value is None:
return None
elif isinstance(db_value, SpatialRepresentationType):
return {"id": db_value.identifier, "label": _(db_value.gn_description)}
else:
logger.warning(f"SpatialRepresentationType: can't decode <{type(db_value)}>'{db_value}'")
return None

@classmethod
def deserialize(cls, field_value):
return SpatialRepresentationType.objects.get(identifier=field_value) if field_value else None
return SpatialRepresentationType.objects.get(identifier=field_value["id"]) if field_value else None


SUBHANDLERS = {
Expand Down
12 changes: 10 additions & 2 deletions geonode/metadata/schemas/base.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,18 @@
"maxLength": 255
},
"spatial_representation_type": {
"type": ["string", "null"],
"type": "object",
"title": "spatial representation type",
"description": "method used to represent geographic information in the dataset.",
"maxLength": 255
"properties": {
"id": {
"type": "string"
},
"label": {
"type": "string"
}
},
"required": ["id"]
}

}
44 changes: 20 additions & 24 deletions geonode/metadata/tests/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def setUp(self):
identifier="fake_restrictions", description="a detailed description"
)
self.spatial_repr = SpatialRepresentationType.objects.create(
identifier="fake_spatial_repr", description="a detailed description"
identifier="fake_spatial_repr", gn_description="a fake label", description="a detailed description"
)

# Create instances for thesaurus
Expand Down Expand Up @@ -613,34 +613,29 @@ def test_restrictions_subhandler_deserialize_with_invalid_data(self):
# Assert that the deserialized value is the correct model instance
self.assertIsNone(deserialized_value)

def test_spatial_repr_type_subhandler_update_subschema(self):
@patch("geonode.metadata.handlers.base.reverse")
def test_spatial_repr_type_subhandler_update_subschema(self, mocked_endpoint):
"""
Test for the update_subschema of the SpatialRepresentationTypeSubHandler.
An instance of the SpatialRepresentationType model has been created
"""

mocked_endpoint.return_value = "/mocked_url"

subschema = {
"type": "string",
"type": "object",
"title": "spatial representation type",
"description": "method used to represent geographic information in the dataset.",
"maxLength": 255,
"properties": {"id": {"type": "string"}, "label": {"type": "string"}},
"required": ["id"],
}

# Delete all the SpatialRepresentationType models except the "fake_spatial_repr"
fake_spatial_repr = SpatialRepresentationType.objects.get(identifier="fake_spatial_repr")
SpatialRepresentationType.objects.exclude(identifier=fake_spatial_repr.identifier).delete()

# Call the update_subschema method with the real database data
# Call the update_subschema method
SpatialRepresentationTypeSubHandler.update_subschema(subschema, lang="en")

# Assertions
self.assertIn("oneOf", subschema)
self.assertEqual(len(subschema["oneOf"]), 1)

# Check that each entry in "oneOf" contains the expected "const", "title", and "description"
self.assertEqual(subschema["oneOf"][0]["const"], "fake_spatial_repr")
self.assertEqual(subschema["oneOf"][0]["title"], "fake_spatial_repr")
self.assertEqual(subschema["oneOf"][0]["description"], "a detailed description")
# Assert changes to the subschema
self.assertIn("ui:options", subschema)
self.assertIn("geonode-ui:autocomplete", subschema["ui:options"])
self.assertEqual(subschema["ui:options"]["geonode-ui:autocomplete"], mocked_endpoint.return_value)

def test_spatial_repr_type_subhandler_serialize_with_existed_db_value(self):
"""
Expand All @@ -651,8 +646,9 @@ def test_spatial_repr_type_subhandler_serialize_with_existed_db_value(self):
# Test the case where the db_value is a model instance
serialized_value = SpatialRepresentationTypeSubHandler.serialize(self.spatial_repr)

# Assert that the serialize method returns the identifier
self.assertEqual(serialized_value, self.spatial_repr.identifier)
expected_value = {"id": self.spatial_repr.identifier, "label": _(self.spatial_repr.gn_description)}

self.assertEqual(serialized_value, expected_value)

def test_spatial_repr_type_subhandler_serialize_with_non_existed_db_value(self):
"""
Expand All @@ -665,23 +661,23 @@ def test_spatial_repr_type_subhandler_serialize_with_non_existed_db_value(self):

serialized_value = SpatialRepresentationTypeSubHandler.serialize(non_spatial_repr_value)

# Assert that the serialize method returns the input value unchanged
self.assertEqual(serialized_value, non_spatial_repr_value)
# Assert that the serialize method returns None for an undecodable value
self.assertIsNone(serialized_value)

def test_spatial_repr_type_subhandler_deserialize(self):
"""
Test the deserialize method.
An instance of this model has been created initial setup
"""

field_value = "fake_spatial_repr"
field_value = {"id": "fake_spatial_repr", "label": "a fake label"}

# Call the method using the "fake_category" identifier from the created instance
deserialized_value = SpatialRepresentationTypeSubHandler.deserialize(field_value)

# Assert that the deserialized value is the correct model instance
self.assertEqual(deserialized_value, self.spatial_repr)
self.assertEqual(deserialized_value.identifier, field_value)
self.assertEqual(deserialized_value.identifier, field_value["id"])

def test_spatial_repr_type_subhandler_deserialize_with_invalid_data(self):
"""
Expand Down
43 changes: 43 additions & 0 deletions geonode/metadata/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
ThesaurusKeyword,
ThesaurusKeywordLabel,
Thesaurus,
SpatialRepresentationType,
)
from geonode.groups.models import GroupProfile, GroupMember
from geonode.metadata.models import SparseField
Expand Down Expand Up @@ -103,6 +104,8 @@ def setUp(self):
TopicCategory.objects.create(identifier="cat2", gn_description="fake category 2")
self.license1 = License.objects.create(identifier="license1", name="fake license 1")
self.license2 = License.objects.create(identifier="license2", name="fake license 2")
SpatialRepresentationType.objects.create(identifier="spatial1", gn_description="fake spatial type 1")
SpatialRepresentationType.objects.create(identifier="spatial2", gn_description="fake spatial type 2")
Region.objects.create(code="fake_code_1", name="fake name 1")
Region.objects.create(code="fake_code_2", name="fake name 2")
HierarchicalKeyword.objects.create(name="fake_keyword_1", slug="fake keyword 1")
Expand Down Expand Up @@ -350,6 +353,46 @@ def test_categories_autocomplete_with_query_no_match(self):
results = response.json()["results"]
self.assertEqual(len(results), 0)

# Tests for spatial representation types autocomplete
def test_spatial_representation_types_autocomplete_no_query(self):
"""
Test the response when no query parameter is provided
"""

self.url = reverse("metadata_autocomplete_spatial_representation_types")
response = self.client.get(self.url)

self.assertEqual(response.status_code, 200)
results = response.json()["results"]
self.assertEqual(len(results), 2)
self.assertIn({"id": "spatial1", "label": _("fake spatial type 1")}, results)
self.assertIn({"id": "spatial2", "label": _("fake spatial type 2")}, results)

def test_spatial_representation_types_autocomplete_with_query_one_match(self):
"""
Test partial matches for the query parameter
"""

self.url = reverse("metadata_autocomplete_spatial_representation_types")
response = self.client.get(self.url, {"q": "fake spatial type 2"})

self.assertEqual(response.status_code, 200)
results = response.json()["results"]
self.assertEqual(len(results), 1)
self.assertIn({"id": "spatial2", "label": _("fake spatial type 2")}, results)

def test_spatial_representation_types_autocomplete_with_query_no_match(self):
"""
Test when no results match the query parameter
"""

self.url = reverse("metadata_autocomplete_spatial_representation_types")
response = self.client.get(self.url, {"q": "A missing spatial type"})

self.assertEqual(response.status_code, 200)
results = response.json()["results"]
self.assertEqual(len(results), 0)

# Tests for License autocomplete
def test_license_autocomplete_no_query(self):
"""
Expand Down
Loading