Skip to content

Commit

Permalink
Merge pull request #184 from iiif-prezi/optional_context
Browse files Browse the repository at this point in the history
Optional context argument
  • Loading branch information
glenrobson authored Dec 4, 2024
2 parents 76d4f40 + e987faf commit 5168000
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
27 changes: 16 additions & 11 deletions iiif_prezi3/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


class Base(BaseModel):

class Config:
validate_assignment = True
validate_all = True
Expand Down Expand Up @@ -59,26 +58,32 @@ def __setattr__(self, key, value):
# and now pass upwards for pydantic to validate and set
super().__setattr__(key, value)

def json(self, **kwargs):
return self.jsonld(**kwargs)

def jsonld(self, **kwargs):
def json(self, exclude_context=False, **kwargs):
# approach 6- use the pydantic .dict() function to get the dict with pydantic options, add the context at the top and dump to json with modified kwargs
excluded_args = ["exclude_unset", "exclude_defaults", "exclude_none", "by_alias", "ensure_ascii", "default"]
pydantic_args = ["include", "exclude", "encoder"]
dict_kwargs = dict([(arg, kwargs[arg]) for arg in kwargs.keys() if arg in pydantic_args])

json_kwargs = dict([(arg, kwargs[arg]) for arg in kwargs.keys() if arg not in pydantic_args + excluded_args])
return json.dumps({"@context": "http://iiif.io/api/presentation/3/context.json",
**self.dict(exclude_unset=False,
exclude_defaults=False,
exclude_none=True,
by_alias=True,
**dict_kwargs)},

dict_out = self.dict(exclude_unset=False,
exclude_defaults=False,
exclude_none=True,
by_alias=True,
**dict_kwargs)

if not exclude_context:
dict_out = {"@context": "http://iiif.io/api/presentation/3/context.json",
**dict_out}

return json.dumps(dict_out,
ensure_ascii=False,
default=pydantic_encoder,
**json_kwargs)

def jsonld(self, **kwargs):
return self.json(exclude_context=False, **kwargs)

def jsonld_dict(self, **kwargs):
pydantic_args = ["include", "exclude", "encoder"]
dict_kwargs = dict([(arg, kwargs[arg]) for arg in kwargs.keys() if arg in pydantic_args])
Expand Down
28 changes: 28 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,34 @@ def testLabel(self):
self.assertTrue('en' in manifest.label, 'Manifest seems to be missing English label')
self.assertEqual(manifest.label['en'][0], 'default label', 'Unexpected label for manifest')

def test_context_default(self):
"""Test that @context is included by default for .json() calls."""
manifest = Manifest(id='http://iiif.example.org/prezi/Manifest/0', type='Manifest', label={'en': ['default label']})
manifest_json = manifest.json()

self.assertEqual(manifest_json[:61], '{"@context": "http://iiif.io/api/presentation/3/context.json"')

def test_context_jsonld(self):
"""Test that @context is included by default for .jsonld() calls."""
manifest = Manifest(id='http://iiif.example.org/prezi/Manifest/0', type='Manifest', label={'en': ['default label']})
manifest_json = manifest.jsonld()

self.assertEqual(manifest_json[:61], '{"@context": "http://iiif.io/api/presentation/3/context.json"')

def test_context_excluded(self):
"""Test that @context is excluded when requested."""
manifest = Manifest(id='http://iiif.example.org/prezi/Manifest/0', type='Manifest', label={'en': ['default label']})
manifest_json = manifest.json(exclude_context=True)

self.assertEqual(manifest_json[:49], '{"id": "http://iiif.example.org/prezi/Manifest/0"')

def text_jsonld_context_excluded(self):
"""Test that @context is not excluded from .jsonld() calls, even when requested."""
manifest = Manifest(id='http://iiif.example.org/prezi/Manifest/0', type='Manifest', label={'en': ['default label']})
manifest_json = manifest.jsonld(exclude_context=True)

self.assertEqual(manifest_json[:61], '{"@context": "http://iiif.io/api/presentation/3/context.json"')


if __name__ == '__main__':
unittest.main()

0 comments on commit 5168000

Please sign in to comment.