From f845a246c5a6e093f89d35b467b1155940b31106 Mon Sep 17 00:00:00 2001 From: alejandromumo Date: Fri, 8 Sep 2023 15:23:37 +0200 Subject: [PATCH] serializers: added locations to ui serializer. --- .../resources/serializers/schemas.py | 3 +- .../resources/serializers/ui/schema.py | 31 +++++++++++++++++++ .../serializers/test_ui_serializer.py | 21 +++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/invenio_rdm_records/resources/serializers/schemas.py b/invenio_rdm_records/resources/serializers/schemas.py index de3e905b9..6d6b661b4 100644 --- a/invenio_rdm_records/resources/serializers/schemas.py +++ b/invenio_rdm_records/resources/serializers/schemas.py @@ -28,7 +28,8 @@ def get_locations(self, obj): """Get locations.""" locations = [] - access_location = obj["metadata"].get("locations", []) + access_location = obj["metadata"].get("locations", {}) + if not access_location: return missing diff --git a/invenio_rdm_records/resources/serializers/ui/schema.py b/invenio_rdm_records/resources/serializers/ui/schema.py index 2208c41ab..74d72f338 100644 --- a/invenio_rdm_records/resources/serializers/ui/schema.py +++ b/invenio_rdm_records/resources/serializers/ui/schema.py @@ -160,6 +160,35 @@ class FundingSchema(Schema): funder = fields.Nested(FunderL10NItemSchema) +class GeometrySchema(Schema): + """Schema for geometry in the UI.""" + + type = fields.Str() + coordinates = fields.List(fields.Float()) + + +class IdentifierSchema(Schema): + """Schema for dumping identifier in the UI.""" + + scheme = fields.Str() + identifier = fields.Str() + + +class FeatureSchema(Schema): + """Schema for dumping locations in the UI.""" + + place = SanitizedUnicode() + description = SanitizedUnicode() + geometry = fields.Nested(GeometrySchema) + identifiers = fields.List(fields.Nested(IdentifierSchema)) + + +class LocationSchema(Schema): + """Schema for dumping locations in the UI.""" + + features = fields.List(fields.Nested(FeatureSchema)) + + class MeetingSchema(Schema): """Schema for dumping 'meeting' custom field in the UI.""" @@ -325,6 +354,8 @@ class UIRecordSchema(BaseObjectSchema): tombstone = fields.Nested(TombstoneSchema, attribute="tombstone") + locations = fields.Nested(LocationSchema, attribute="metadata.locations") + @pre_dump def add_communities_permissions_and_roles(self, obj, **kwargs): """Inject current user's permission to community receiver.""" diff --git a/tests/resources/serializers/test_ui_serializer.py b/tests/resources/serializers/test_ui_serializer.py index 7d7e90b78..5475be367 100644 --- a/tests/resources/serializers/test_ui_serializer.py +++ b/tests/resources/serializers/test_ui_serializer.py @@ -90,6 +90,17 @@ def full_to_dict_record(full_record): } ] + to_dict_record["metadata"]["locations"] = { + "features": [ + { + "geometry": {"type": "Point", "coordinates": [6.05, 46.23333]}, + "identifiers": [{"scheme": "geonames", "identifier": "2661235"}], + "place": "CERN", + "description": "Invenio birth place.", + }, + ] + } + _add_affiliation_name(to_dict_record["metadata"]["creators"]) _add_affiliation_name(to_dict_record["metadata"]["contributors"]) @@ -206,6 +217,16 @@ def test_ui_serializer(app, full_to_dict_record): }, } ], + "locations": { + "features": [ + { + "geometry": {"type": "Point", "coordinates": [6.05, 46.23333]}, + "identifiers": [{"scheme": "geonames", "identifier": "2661235"}], + "place": "CERN", + "description": "Invenio birth place.", + }, + ] + }, } serialized_record = UIJSONSerializer().dump_obj(full_to_dict_record)