From 374f4190eb49da999ac71be4aa6c01d853f3cab3 Mon Sep 17 00:00:00 2001 From: Ben Silverman Date: Tue, 5 Dec 2023 11:20:37 -0500 Subject: [PATCH] Update serialization: separate seeAlso and related (#958) --- apps/iiif/manifests/models.py | 33 ++++++++++++++++++++++++++++-- apps/iiif/manifests/tests/tests.py | 2 +- apps/iiif/serializers/manifest.py | 5 ++--- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/apps/iiif/manifests/models.py b/apps/iiif/manifests/models.py index 426f0f31b..614efbf53 100644 --- a/apps/iiif/manifests/models.py +++ b/apps/iiif/manifests/models.py @@ -259,8 +259,37 @@ def related_links(self): :return: List of links related to Manifest :rtype: list """ - links = [link.link for link in self.relatedlink_set.all()] - links.append(self.get_volume_url()) + links = [ + { + "@id": link.link, + "format": link.format, + } if link.format else link.link + for link in self.relatedlink_set.all() + ] + links.append({ + "@id": self.get_volume_url(), + "format": "text/html" + }) + return links + + @property + def see_also_links(self): + """List of links for IIIF v2 'seeAlso' field (structured data). + + :return: List of links to structured data describing Manifest + :rtype: list + """ + links = [] + for link in self.relatedlink_set.all(): + if link.data_type.lower() == "dataset": + links.append( + { + "@id": link.link, + "format": link.format, + } + if link.format + else link.link + ) return links # TODO: Is this needed? It doesn't seem to be called anywhere. diff --git a/apps/iiif/manifests/tests/tests.py b/apps/iiif/manifests/tests/tests.py index e30228bc2..16be42f34 100644 --- a/apps/iiif/manifests/tests/tests.py +++ b/apps/iiif/manifests/tests/tests.py @@ -175,7 +175,7 @@ def test_serialized_related_links(self): [manifest] ) ) - assert 'seeAlso' not in no_links.keys() + assert not no_links['seeAlso'] link = RelatedLink(link='images.org', manifest=manifest) link.save() diff --git a/apps/iiif/serializers/manifest.py b/apps/iiif/serializers/manifest.py index bd55ebd42..cef2da43e 100644 --- a/apps/iiif/serializers/manifest.py +++ b/apps/iiif/serializers/manifest.py @@ -125,10 +125,9 @@ def get_dump_object(self, obj): ) ) } - ] + ], + "seeAlso": obj.see_also_links, } - if obj.relatedlink_set.exists(): - data["seeAlso"] = [related.link for related in obj.relatedlink_set.all()] return data return None