Skip to content

Commit

Permalink
Refactor traversal/adapter to return @@JSON if the view was called wi…
Browse files Browse the repository at this point in the history
…th @@JSON.
  • Loading branch information
tisto committed Mar 27, 2015
1 parent b0bc7d0 commit 1c639bd
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 29 deletions.
4 changes: 2 additions & 2 deletions docs/source/_json/folder.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
"language": "",
"member": [
{
"@id": "http://localhost:55001/plone/folder/doc1/@@json",
"@id": "http://localhost:55001/plone/folder/doc1",
"description": "",
"title": "A document within a folder"
},
{
"@id": "http://localhost:55001/plone/folder/doc2/@@json",
"@id": "http://localhost:55001/plone/folder/doc2",
"description": "",
"title": "A document within a folder"
}
Expand Down
4 changes: 2 additions & 2 deletions docs/source/_json/siteroot.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
"@type": "Collection",
"member": [
{
"@id": "http://localhost:55001/plone/robot-test-folder/@@json",
"@id": "http://localhost:55001/plone/robot-test-folder",
"description": "",
"title": "Test Folder"
},
{
"@id": "http://localhost:55001/plone/front-page/@@json",
"@id": "http://localhost:55001/plone/front-page",
"description": "Congratulations! You have successfully installed Plone.",
"title": "Welcome to Plone"
}
Expand Down
11 changes: 9 additions & 2 deletions src/plone/restapi/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from plone.app.contenttypes.interfaces import ICollection
from plone.app.contenttypes.interfaces import IFile
from plone.app.contenttypes.interfaces import IImage
from plone.restapi.utils import append_json_to_links
from plone.restapi.utils import get_object_schema
from plone.restapi.interfaces import ISerializeToJson

Expand All @@ -34,13 +35,16 @@ def SerializeSiteRootToJson(context):
}
result['member'] = [
{
'@id': member.absolute_url() + '/@@json',
'@id': member.absolute_url(),
'title': member.title,
'description': member.description
}
for member_id, member in context.objectItems()
if IContentish.providedBy(member)
]
if getattr(context, 'request', False):
if context.request.get('append_json_to_hyperlinks', False):
result = append_json_to_links(result)
return json.dumps(result, indent=2, sort_keys=True)


Expand All @@ -56,7 +60,7 @@ def SerializeToJson(context):
result['@type'] = 'Collection'
result['member'] = [
{
'@id': member.absolute_url() + '/@@json',
'@id': member.absolute_url(),
'title': member.title,
'description': member.description
}
Expand Down Expand Up @@ -105,6 +109,9 @@ def SerializeToJson(context):
result[title] = value
else:
result[title] = str(value)
if getattr(context, 'request', False):
if context.request.get('append_json_to_hyperlinks', False):
result = append_json_to_links(result)
return json.dumps(result, indent=2, sort_keys=True)


Expand Down
1 change: 1 addition & 0 deletions src/plone/restapi/browser/traversal.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class MarkAsApiRequest(SimpleItem):
implements(IPublishTraverse)

def publishTraverse(self, request, name):
self.request.set('append_json_to_hyperlinks', True)
alsoProvides(self.request, IAPIRequest)
return self.context

Expand Down
2 changes: 1 addition & 1 deletion src/plone/restapi/tests/test_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def test_serialize_to_json_adapter_on_folder_returns_member_attr(self):
json.loads(ISerializeToJson(self.portal.folder1))['member'],
[
{
u'@id': u'http://nohost/plone/folder1/doc1/@@json',
u'@id': u'http://nohost/plone/folder1/doc1',
u'description': u'This is a document',
u'title': u'Document 1'
}
Expand Down
6 changes: 3 additions & 3 deletions src/plone/restapi/tests/test_browser_traversal.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,23 @@ def test_json_view_document_traversal(self):
self.assertTrue(json.loads(self.browser.contents))
self.assertEqual(
json.loads(self.browser.contents).get('@id'),
self.document_url
self.document_url + '/@@json'
)

def test_json_view_folder_traversal(self):
self.browser.open(self.folder_url + '/@@json')
self.assertTrue(json.loads(self.browser.contents))
self.assertEqual(
json.loads(self.browser.contents).get('@id'),
self.folder_url
self.folder_url + '/@@json'
)

def test_json_view_site_root_traversal(self):
self.browser.open(self.portal_url + '/@@json')
self.assertTrue(json.loads(self.browser.contents))
self.assertEqual(
json.loads(self.browser.contents).get('@id'),
self.portal_url
self.portal_url + '/@@json'
)

def test_document_traversal(self):
Expand Down
14 changes: 7 additions & 7 deletions src/plone/restapi/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,22 @@ def test_two_simple_terms(self):
class AppendJsonToLinksUnitTest(unittest.TestCase):

def test_empty(self):
self.assertEqual([], append_json_to_links([]))
self.assertEqual({}, append_json_to_links({}))

def test_append_json_to_id(self):
self.assertEqual(
[{'@id': 'http://foo.com/@@json'}],
append_json_to_links([{'@id': 'http://foo.com'}])
{'@id': 'http://foo.com/@@json'},
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'}])
{'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'}]}])
{'member': [{'@id': 'http://foo.com/@@json'}]},
append_json_to_links({'member': [{'@id': 'http://foo.com'}]})
)
23 changes: 11 additions & 12 deletions src/plone/restapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,15 @@ def underscore_to_camelcase(underscore_string):
def append_json_to_links(result):
"""
"""
for index, item in enumerate(result):
if '@id' in item:
result[index]['@id'] = '{0}/@@json'.format(item['@id'])
if 'parent' in item:
result[index]['parent'] = '{0}/@@json'.format(item['parent'])
if 'member' in item:
for mem_index, mem in enumerate(item['member']):
if '@id' in mem:
result[index]['member'][mem_index]['@id'] = \
'{0}/@@json'.format(
result[index]['member'][mem_index]['@id']
)
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']
)
return result

0 comments on commit 1c639bd

Please sign in to comment.