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 AnnotationPageRef to the .to_reference() function #185

Open
wants to merge 3 commits into
base: main
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
13 changes: 9 additions & 4 deletions iiif_prezi3/helpers/to_reference.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from ..loader import monkeypatch_schema
from ..skeleton import (Annotation, AnnotationCollection, AnnotationPage,
Canvas, CanvasRef, Collection, CollectionRef, Manifest,
ManifestRef, Range, RangeRef, Reference)
AnnotationPageRef, Canvas, CanvasRef, Collection,
CollectionRef, Manifest, ManifestRef, Range, RangeRef,
Reference)


class ToReference:
Expand All @@ -17,7 +18,9 @@ def to_reference(self):
# Currently the skeleton Reference requires a label, but some Referenceable objects may not have one (e.g AnnotationPage)
# TODO: Remove this when the Schema is updated to have different reference types
if not self.label:
self.label = ""
label = None
else:
label = self.label

# Ensure that we use a specific Reference type if it exists
if isinstance(self, Manifest):
Expand All @@ -28,10 +31,12 @@ def to_reference(self):
target_type = CanvasRef
elif isinstance(self, Range):
target_type = RangeRef
elif isinstance(self, AnnotationPage):
target_type = AnnotationPageRef
else:
target_type = Reference

return target_type(id=self.id, label=self.label, type=self.type, thumbnail=thumbnail)
return target_type(id=self.id, label=label, type=self.type, thumbnail=thumbnail)


monkeypatch_schema([Manifest, AnnotationPage, Collection, AnnotationCollection, Canvas, Range], ToReference)
26 changes: 13 additions & 13 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 (Annotation, AnnotationPage, AnnotationPageRef, Canvas,
Collection, CollectionRef, Manifest, ManifestRef,
Reference, ResourceItem)


class AddItemTests(unittest.TestCase):
Expand Down Expand Up @@ -36,18 +37,17 @@ def test_add_manifest_to_collection(self):
self.c.add_item(self.m)
self.assertIsInstance(self.c.items[0], ManifestRef)

# THIS TEST DISABLED UNTIL AnnotationPageRef added to the schema + skeleton
# def test_add_by_reference(self):
# """Test that an item can be added by Reference."""
# self.ap.add_item(self.a)
# self.assertEqual(len(self.ap.items), 1)
# self.c.add_item_by_reference(self.ap)
# self.assertNotEqual(self.ca.items[-1], self.ap)
# self.assertIsInstance(self.ca.items[-1], Reference)
# self.assertEqual(self.ca.items[-1].id, self.ap.id)

def test_add_by_reference(self):
"""Test that an item can be added by Reference."""
"""Test that an AnnotationPage can be added to a Canvas by Reference."""
self.ap.add_item(self.a)
self.assertEqual(len(self.ap.items), 1)
self.ca.add_item_by_reference(self.ap)
self.assertNotEqual(self.ca.items[-1], self.ap)
self.assertIsInstance(self.ca.items[-1], AnnotationPageRef)
self.assertEqual(self.ca.items[-1].id, self.ap.id)

def test_add_collection_by_reference(self):
"""Test that a Collection can be added to another Collection by Reference."""
self.c.add_item_by_reference(self.c2)
self.assertNotEqual(self.c.items[-1], self.c2)
self.assertIsInstance(self.c.items[-1], CollectionRef)
Expand Down
Loading