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

Add Recipe 0014 and add add_item() to AccompanyingCanvas #200

Merged
merged 5 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
10 changes: 10 additions & 0 deletions docs/recipes/0014-accompanyingcanvas.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# "Audio Presentation with Accompanying Image"
| | **Cookbook URLs** |
|--------------|-------------------|
| **Recipe:** | [https://iiif.io/api/cookbook/recipe/0014-accompanyingcanvas/](https://iiif.io/api/cookbook/recipe/0014-accompanyingcanvas/) |
| **JSON-LD:** | [https://iiif.io/api/cookbook/recipe/0014-accompanyingcanvas/manifest.json](https://iiif.io/api/cookbook/recipe/0014-accompanyingcanvas/manifest.json) |

### Method 1 - Use AccompanyingCanvas and add_item() helper
```python
--8<-- "docs/recipes/scripts/0014-accompanyingcanvas-method1.py"
```
60 changes: 60 additions & 0 deletions docs/recipes/scripts/0014-accompanyingcanvas-method1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from iiif_prezi3 import Manifest, ResourceItem, AnnotationPage, Annotation, config, AccompanyingCanvas

config.configs['helpers.auto_fields.AutoLang'].auto_lang = "en"
accompanying_canvas = AccompanyingCanvas(
id="https://iiif.io/api/cookbook/recipe/0014-accompanyingcanvas/canvas/accompanying",
label="First page of score for Gustav Mahler, Symphony No. 3",
height=998,
width=772,
)
manifest = Manifest(
id="https://iiif.io/api/cookbook/recipe/0014-accompanyingcanvas/manifest.json",
label="Partial audio recording of Gustav Mahler's _Symphony No. 3_",
)
ac_anno_body = ResourceItem(
id="https://iiif.io/api/image/3.0/example/reference/4b45bba3ea612ee46f5371ce84dbcd89-mahler-0/full/,998/0/default.jpg",
type="Image",
format="image/jpeg",
height=998,
width=772,
)
ac_anno_body.make_service(
id="https://iiif.io/api/image/3.0/example/reference/4b45bba3ea612ee46f5371ce84dbcd89-mahler-0",
type="ImageService3",
profile="level1"
)
ac_anno_page = AnnotationPage(
id="https://iiif.io/api/cookbook/recipe/0014-accompanyingcanvas/canvas/accompanying/annotation/page"
)
ac_anno = Annotation(
id="https://iiif.io/api/cookbook/recipe/0014-accompanyingcanvas/canvas/accompanying/annotation/image",
motivation="painting",
body=ac_anno_body,
target="https://iiif.io/api/cookbook/recipe/0014-accompanyingcanvas/canvas/accompanying"
)
ac_anno_page.add_item(ac_anno)
accompanying_canvas.add_item(ac_anno_page)
canvas = manifest.make_canvas(
id="https://iiif.io/api/cookbook/recipe/0014-accompanyingcanvas/canvas/p1",
label="Gustav Mahler, Symphony No. 3, CD 1",
duration=1985.024,
accompanyingCanvas=accompanying_canvas
)
anno_body = ResourceItem(
id="https://fixtures.iiif.io/audio/indiana/mahler-symphony-3/CD1/medium/128Kbps.mp4",
type="Sound",
format="video/mp4",
duration=1985.024,
)
anno_page = AnnotationPage(
id="https://iiif.io/api/cookbook/recipe/0014-accompanyingcanvas/canvas/page/p1"
)
anno = Annotation(
id="https://iiif.io/api/cookbook/recipe/0014-accompanyingcanvas/canvas/page/annotation/segment1-audio",
motivation="painting",
body=anno_body,
target=canvas.id
)
anno_page.add_item(anno)
canvas.add_item(anno_page)
print(manifest.json(indent=2))
8 changes: 4 additions & 4 deletions iiif_prezi3/helpers/add_item.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from ..loader import monkeypatch_schema
from ..skeleton import (AnnotationPage, Canvas, Collection, Manifest, Range,
Reference)
from ..skeleton import (AccompanyingCanvas, AnnotationPage, Canvas, Collection,
Manifest, Range, Reference)


class AddItem:
Expand Down Expand Up @@ -37,5 +37,5 @@ def add_item_by_reference(self, item):
self.items = self.items # Force Pydantic to validate?


monkeypatch_schema([Collection, Manifest, Canvas, Range, AnnotationPage, Reference], AddItem)
monkeypatch_schema([Collection, Range, Canvas, AnnotationPage], AddItemByReference)
monkeypatch_schema([Collection, Manifest, Canvas, Range, AnnotationPage, Reference, AccompanyingCanvas], AddItem)
monkeypatch_schema([Collection, Range, Canvas, AnnotationPage, AccompanyingCanvas], AddItemByReference)
11 changes: 9 additions & 2 deletions tests/test_add_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

from pydantic import ValidationError

from iiif_prezi3 import (Annotation, AnnotationPage, Canvas, Collection,
CollectionRef, Manifest, ManifestRef, ResourceItem)
from iiif_prezi3 import (AccompanyingCanvas, Annotation, AnnotationPage,
Canvas, Collection, CollectionRef, Manifest,
ManifestRef, ResourceItem)


class AddItemTests(unittest.TestCase):
Expand All @@ -16,6 +17,7 @@ def setUp(self):
self.ca2 = Canvas(label="second canvas", type="Canvas")
self.ap = AnnotationPage()
self.a = Annotation(target=self.c.id)
self.ac = AccompanyingCanvas()

def test_add_item(self):
"""Test that a Canvas added to an empty Manifest creates and populates items."""
Expand Down Expand Up @@ -74,3 +76,8 @@ def test_add_invalid_reference(self):
"""Test that adding an invalid reference type fails."""
with self.assertRaises(ValidationError):
self.c2.add_item_by_reference(self.ca)

def test_add_item_to_accompanying_canvas(self):
"""Test that an item can be added to an items list in Accompanying Canvas."""
self.ac.add_item(self.ap)
self.assertIsInstance(self.ac.items[0], AnnotationPage)