Skip to content

Commit

Permalink
UISerializer: add polygon locations to serializer in addition to poin…
Browse files Browse the repository at this point in the history
…ts (#1924)

* Allow UISerializer to serialize Polygon locations in addition to Points.

* Consolidate test cases and fixtures
  • Loading branch information
spilth authored Jan 27, 2025
1 parent fc77aa6 commit ee29707
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
14 changes: 13 additions & 1 deletion invenio_rdm_records/resources/serializers/ui/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@ def mask_removed_by(obj):
return return_value


def get_coordinates(obj):
"""Coordinates determined by geometry type."""
geometry_type = obj.get("type", None)

if geometry_type == "Point":
return obj.get("coordinates", [])
elif geometry_type == "Polygon":
return obj.get("coordinates", [[[]]])
else:
return None


class RelatedIdentifiersSchema(Schema):
"""Localization of language titles."""

Expand Down Expand Up @@ -168,7 +180,7 @@ class GeometrySchema(Schema):
"""Schema for geometry in the UI."""

type = fields.Str()
coordinates = fields.List(fields.Float())
coordinates = fields.Function(get_coordinates)


class IdentifierSchema(Schema):
Expand Down
18 changes: 18 additions & 0 deletions tests/resources/serializers/test_ui_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ def full_to_dict_record(full_record_to_dict):
"place": "CERN",
"description": "Invenio birth place.",
},
{
"geometry": {
"type": "Polygon",
"coordinates": [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]],
},
"identifiers": [{"scheme": "geonames", "identifier": "123456"}],
"place": "Rectangle",
"description": "Example Polygon",
},
]
}

Expand Down Expand Up @@ -256,6 +265,15 @@ def test_ui_serializer(app, full_to_dict_record):
"place": "CERN",
"description": "Invenio birth place.",
},
{
"geometry": {
"type": "Polygon",
"coordinates": [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]],
},
"identifiers": [{"scheme": "geonames", "identifier": "123456"}],
"place": "Rectangle",
"description": "Example Polygon",
},
]
},
}
Expand Down

0 comments on commit ee29707

Please sign in to comment.