diff --git a/iiif_prezi3/base.py b/iiif_prezi3/base.py index 1399b51..c032d70 100644 --- a/iiif_prezi3/base.py +++ b/iiif_prezi3/base.py @@ -5,7 +5,6 @@ class Base(BaseModel): - class Config: validate_assignment = True validate_all = True @@ -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]) diff --git a/tests/test_basic.py b/tests/test_basic.py index 57cdbcc..1aa1f54 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -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()