Skip to content

Commit

Permalink
Links to the parent object should use @id to mark links. Otherwise th…
Browse files Browse the repository at this point in the history
…e client can not figure out that this is a hyperlink.
  • Loading branch information
tisto committed Mar 28, 2015
1 parent c092743 commit aab5117
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 24 deletions.
6 changes: 5 additions & 1 deletion docs/source/_json/collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@
}
],
"meta_type": "Dexterity Item",
"parent": "http://localhost:55001/plone",
"parent": {
"@id": "http://localhost:55001/plone",
"description": "",
"title": "Plone site"
},
"portal_type": "Collection",
"query": [
{
Expand Down
6 changes: 5 additions & 1 deletion docs/source/_json/document.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
"isPrincipiaFolderish": "0",
"language": "",
"meta_type": "Dexterity Item",
"parent": "http://localhost:55001/plone",
"parent": {
"@id": "http://localhost:55001/plone",
"description": "",
"title": "Plone site"
},
"portal_type": "Document",
"relatedItems": [],
"rights": "",
Expand Down
6 changes: 5 additions & 1 deletion docs/source/_json/file.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
"@type": "Resource",
"description": "This is a file",
"download": "http://localhost:55001/plone/file/@@download",
"parent": "http://localhost:55001/plone",
"parent": {
"@id": "http://localhost:55001/plone",
"description": "",
"title": "Plone site"
},
"portal_type": "File",
"title": "My File"
}
6 changes: 5 additions & 1 deletion docs/source/_json/folder.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@
"meta_type": "Dexterity Container",
"meta_types": [],
"nextPreviousEnabled": "False",
"parent": "http://localhost:55001/plone",
"parent": {
"@id": "http://localhost:55001/plone",
"description": "",
"title": "Plone site"
},
"portal_type": "Folder",
"relatedItems": [],
"rights": "",
Expand Down
6 changes: 5 additions & 1 deletion docs/source/_json/image.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
"@type": "Resource",
"description": "This is an image",
"download": "http://localhost:55001/plone/image/@@download",
"parent": "http://localhost:55001/plone",
"parent": {
"@id": "http://localhost:55001/plone",
"description": "",
"title": "Plone site"
},
"portal_type": "Image",
"title": "My Image",
"versions": [
Expand Down
6 changes: 5 additions & 1 deletion docs/source/_json/link.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
"isPrincipiaFolderish": "0",
"language": "",
"meta_type": "Dexterity Item",
"parent": "http://localhost:55001/plone",
"parent": {
"@id": "http://localhost:55001/plone",
"description": "",
"title": "Plone site"
},
"portal_type": "Link",
"remoteUrl": "http://",
"rights": "",
Expand Down
6 changes: 5 additions & 1 deletion docs/source/_json/newsitem.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
"isPrincipiaFolderish": "0",
"language": "",
"meta_type": "Dexterity Item",
"parent": "http://localhost:55001/plone",
"parent": {
"@id": "http://localhost:55001/plone",
"description": "",
"title": "Plone site"
},
"portal_type": "News Item",
"relatedItems": [],
"rights": "",
Expand Down
2 changes: 1 addition & 1 deletion docs/source/_json/siteroot.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
"title": "Welcome to Plone"
}
],
"parent": null,
"parent": {},
"portal_type": "SiteRoot"
}
20 changes: 16 additions & 4 deletions src/plone/restapi/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def SerializeSiteRootToJson(context):
'@context': 'http://www.w3.org/ns/hydra/context.jsonld',
'@id': context.absolute_url(),
'@type': 'Collection',
'parent': None,
'parent': {},
'portal_type': 'SiteRoot'
}
result['member'] = [
Expand All @@ -54,7 +54,11 @@ def SerializeToJson(context):
result = {
'@context': 'http://www.w3.org/ns/hydra/context.jsonld',
'@id': context.absolute_url(),
'parent': aq_parent(aq_inner(context)).absolute_url(),
'parent': {
'@id': aq_parent(aq_inner(context)).absolute_url(),
'title': aq_parent(aq_inner(context)).title,
'description': aq_parent(aq_inner(context)).description
}
}
if IFolderish.providedBy(context):
result['@type'] = 'Collection'
Expand Down Expand Up @@ -122,7 +126,11 @@ def SerializeFileToJson(context):
'@context': 'http://www.w3.org/ns/hydra/context.jsonld',
'@id': context.absolute_url(),
'@type': 'Resource',
'parent': aq_parent(aq_inner(context)).absolute_url(),
'parent': {
'@id': aq_parent(aq_inner(context)).absolute_url(),
'title': aq_parent(aq_inner(context)).title,
'description': aq_parent(aq_inner(context)).description
},
'portal_type': 'File',
'title': context.title,
'description': context.description,
Expand All @@ -141,7 +149,11 @@ def SerializeImageToJson(context):
'@context': 'http://www.w3.org/ns/hydra/context.jsonld',
'@id': context.absolute_url(),
'@type': 'Resource',
'parent': aq_parent(aq_inner(context)).absolute_url(),
'parent': {
'@id': aq_parent(aq_inner(context)).absolute_url(),
'title': aq_parent(aq_inner(context)).title,
'description': aq_parent(aq_inner(context)).description
},
'portal_type': 'Image',
'title': context.title,
'description': context.description,
Expand Down
16 changes: 12 additions & 4 deletions src/plone/restapi/tests/test_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,26 @@ def test_serialize_to_json_adapter_returns_parent(self):
'The parent attribute should be present.'
)
self.assertEqual(
json.loads(ISerializeToJson(self.portal.doc1))['parent'],
self.portal_url
{
'@id': self.portal.absolute_url(),
'title': self.portal.title,
'description': self.portal.description
},
json.loads(ISerializeToJson(self.portal.doc1))['parent']
)

def test_serialize_to_json_adapter_does_not_returns_parent_on_root(self):
self.assertEqual(
{},
json.loads(ISerializeToJson(self.portal)).get('parent'),
None,
'The parent attribute should be present, even on portal root.'
)
self.assertEqual(
self.portal_url,
{
'@id': self.portal.absolute_url(),
'title': self.portal.title,
'description': self.portal.description
},
json.loads(ISerializeToJson(self.portal.doc1))['parent'],
'The parent attribute on portal root should be None'
)
Expand Down
12 changes: 6 additions & 6 deletions src/plone/restapi/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@ def test_append_json_to_id(self):
append_json_to_links({'@id': 'http://foo.com'})
)

def test_append_json_to_parent(self):
self.assertEqual(
{'parent': 'http://foo.com/@@json'},
append_json_to_links({'parent': 'http://foo.com'})
)

def test_append_json_to_member_ids(self):
self.assertEqual(
{'member': [{'@id': 'http://foo.com/@@json'}]},
append_json_to_links({'member': [{'@id': 'http://foo.com'}]})
)

def test_append_json_to_parent_ids(self):
self.assertEqual(
{'parent': {'@id': 'http://foo.com/@@json'}},
append_json_to_links({'parent': {'@id': 'http://foo.com'}})
)
6 changes: 4 additions & 2 deletions src/plone/restapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ def append_json_to_links(result):
"""
if '@id' in result:
result['@id'] = '{0}/@@json'.format(result['@id'])
if 'parent' in result:
result['parent'] = '{0}/@@json'.format(result['parent'])
if 'member' in result:
for index, item in enumerate(result['member']):
if '@id' in item:
result['member'][index]['@id'] = \
'{0}/@@json'.format(
result['member'][index]['@id']
)
if 'parent' in result and result != {} and '@id' in result['parent']:
result['parent']['@id'] = '{0}/@@json'.format(
result['parent']['@id']
)
return result

0 comments on commit aab5117

Please sign in to comment.