From d1b0c1dfaa3493171c3b964772d564f7c5d20b4b Mon Sep 17 00:00:00 2001 From: Ben Silverman Date: Mon, 11 Dec 2023 17:04:10 -0500 Subject: [PATCH] Serialize metadata according to IIIF spec --- apps/iiif/serializers/manifest.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/apps/iiif/serializers/manifest.py b/apps/iiif/serializers/manifest.py index cef2da43..3151b3e2 100644 --- a/apps/iiif/serializers/manifest.py +++ b/apps/iiif/serializers/manifest.py @@ -27,6 +27,17 @@ def start_serialization(self): def end_serialization(self): self.stream.write('') + def serialize_metadata(self, obj): + """Convert metadata on object into list of {label, value} dicts""" + if isinstance(obj.metadata, list): + # most common case: metadata is already a list of {label, value} dicts + return obj.metadata + elif isinstance(obj.metadata, dict): + # convert dict into list of label/value pair dicts + return [{"label": key, "value": val} for (key, val) in obj.metadata.items()] + else: + return [] + def get_dump_object(self, obj): # TODO: Raise error if version is not v2 or v3 if self.version == 'v2' or self.version is None: @@ -69,10 +80,6 @@ def get_dump_object(self, obj): "label": "Publication Date", "value": obj.published_date }, - { - "label": "Notes", - "value": obj.metadata - }, { "label": "Record Created", "value": obj.created_at @@ -92,7 +99,9 @@ def get_dump_object(self, obj): { "label": "Export Date", "value": self.exportdate - } + }, + # unpack serialized metadata (list of label, value pairs) + *self.serialize_metadata(obj), ], "description": obj.summary, "related": obj.related_links,