Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IIIF: set startCanvas from default_preview #1275

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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: 11 additions & 1 deletion invenio_rdm_records/resources/serializers/iiif/schema.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2021-2023 CERN.
# Copyright (C) 2021 data-futures.
# Copyright (C) 2021-2023 data-futures.
# Copyright (C) 2022 Universität Hamburg.
#
# Invenio-RDM-Records is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -153,11 +153,21 @@ class Meta:
label = fields.Constant(_("Current Page Order"))
viewingDirection = fields.Constant("left-to-right")
viewingHint = fields.Constant("paged")
startCanvas = fields.Method("get_start_canvas")

canvases = ListIIIFFilesAttribute(
fields.Nested(IIIFCanvasV2Schema), attribute="files.entries"
)

def get_start_canvas(self, obj):
"""Set startCanvas - use default_preview if set."""
try:
file = obj["files"]["default_preview"]
entry = next((e for e in obj.files.entries if e["key"] == file), None)
return entry["links"]["iiif_canvas"]
except (AttributeError, KeyError):
return missing


class IIIFManifestV2Schema(Schema):
"""IIIF manifest schema."""
Expand Down
73 changes: 43 additions & 30 deletions tests/resources/test_iiif_presentation_api.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2022 Universität Hamburg.
# Copyright (C) 2023 Data Futures GmbH.
#
# Invenio-RDM-Records is free software; you can redistribute it and/or modify
# it under the terms of the MIT License; see LICENSE file for more details.

"""Tests for the handlers."""

import random
from io import BytesIO

import pytest
Expand All @@ -15,8 +17,11 @@


def publish_record_with_images(
client, file_id, record, headers, restricted_files=False
client, file_ids, record, headers, restricted_files=False
):
# generate a random RGBA value tuple
random_color = lambda: tuple(random.randint(0, 255) for _ in range(4))

"""A record with files."""
record["files"]["enabled"] = True
if restricted_files:
Expand All @@ -26,24 +31,29 @@ def publish_record_with_images(
res = client.post("/records", headers=headers, json=record)
id_ = res.json["id"]

# create a new image
res = client.post(
f"/records/{id_}/draft/files", headers=headers, json=[{"key": file_id}]
)

# Upload a file
image_file = BytesIO()
image = Image.new("RGBA", (1280, 1024), (255, 0, 0, 0))
image.save(image_file, "png")
image_file.seek(0)
res = client.put(
f"/records/{id_}/draft/files/{file_id}/content",
headers={"content-type": "application/octet-stream"},
data=image_file,
)

# Commit the file
res = client.post(f"/records/{id_}/draft/files/{file_id}/commit", headers=headers)
for f in file_ids:
# create a new image
res = client.post(
f"/records/{id_}/draft/files", headers=headers, json=[{"key": f}]
)

# Upload a file
image_file = BytesIO()
image = Image.new("RGBA", (1280, 1024), random_color())
image.save(image_file, "png")
image_file.seek(0)
res = client.put(
f"/records/{id_}/draft/files/{f}/content",
headers={"content-type": "application/octet-stream"},
data=image_file,
)

# Commit the file
res = client.post(f"/records/{id_}/draft/files/{f}/commit", headers=headers)

# set a default_preview image
record["files"]["default_preview"] = file_ids[-1]
res = client.put(f"/records/{id_}/draft", headers=headers, json=record)

# Publish the record
res = client.post(f"/records/{id_}/draft/actions/publish", headers=headers)
Expand All @@ -56,8 +66,8 @@ def test_iiif_manifest_schema(
running_app, search_clear, client, uploader, headers, minimal_record
):
client = uploader.login(client)
file_id = "test_image.png"
recid = publish_record_with_images(client, file_id, minimal_record, headers)
file_ids = ["test_image_001.png", "test_image_002.png"]
recid = publish_record_with_images(client, file_ids, minimal_record, headers)
response = client.get(f"/iiif/record:{recid}/manifest")
manifest = response.json
validator = IIIFValidator(fail_fast=False)
Expand All @@ -70,26 +80,29 @@ def test_iiif_manifest(
running_app, search_clear, client, uploader, headers, minimal_record
):
client = uploader.login(client)
file_id = "test_image.png"
recid = publish_record_with_images(client, file_id, minimal_record, headers)
file_ids = ["test_image_001.png", "test_image_002.png"]
recid = publish_record_with_images(client, file_ids, minimal_record, headers)
response = client.get(f"/iiif/record:{recid}/manifest")
assert response.status_code == 200

manifest = response.json
assert manifest["@id"] == f"https://127.0.0.1:5000/api/iiif/record:{recid}/manifest"
assert manifest["label"] == "A Romans story"
assert "sequences" in manifest
assert len(manifest["sequences"]) == 1

sequence = manifest["sequences"][0]
assert (
sequence["@id"]
== f"https://127.0.0.1:5000/api/iiif/record:{recid}/sequence/default"
)
assert (
sequence["startCanvas"]
== f"https://127.0.0.1:5000/api/iiif/record:{recid}/canvas/{file_ids[-1]}"
)
assert "canvases" in sequence
assert len(sequence["canvases"]) == 1

assert len(sequence["canvases"]) == len(file_ids)
canvas = sequence["canvases"][0]

assert (
canvas["@id"]
== f"https://127.0.0.1:5000/api/iiif/record:{recid}/canvas/test_image.png"
Expand All @@ -105,20 +118,20 @@ def test_iiif_manifest(
assert image["resource"]["width"] == 1280
assert (
image["resource"]["@id"] == f"https://127.0.0.1:5000/api/iiif/"
f"record:{recid}:{file_id}/full/full/0/default.png"
f"record:{recid}:{file_ids[0]}/full/full/0/default.png"
)
assert (
image["resource"]["service"]["@id"]
== f"https://127.0.0.1:5000/api/iiif/record:{recid}:{file_id}"
== f"https://127.0.0.1:5000/api/iiif/record:{recid}:{file_ids[0]}"
)


def test_empty_iiif_manifest(
running_app, search_clear, client, uploader, headers, minimal_record
):
client = uploader.login(client)
file_id = "test_image.zip"
recid = publish_record_with_images(client, file_id, minimal_record, headers)
file_ids = ["test_file.zip"]
recid = publish_record_with_images(client, file_ids, minimal_record, headers)
response = client.get(f"/iiif/record:{recid}/manifest")
assert response.status_code == 200
manifest = response.json
Expand Down
Loading