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

Optional context argument #184

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
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()