Skip to content

Commit

Permalink
Use @type for portal_type. This fixes #15.
Browse files Browse the repository at this point in the history
  • Loading branch information
tisto committed Apr 17, 2015
1 parent a6b0b5b commit 48769d8
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 58 deletions.
1 change: 0 additions & 1 deletion docs/source/_json/collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"description": "",
"title": "Plone site"
},
"portal_type": "Collection",
"query": [
{
"i": "portal_type",
Expand Down
3 changes: 1 addition & 2 deletions docs/source/_json/document.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"@context": "http://www.w3.org/ns/hydra/context.jsonld",
"@id": "http://localhost:55001/plone/front-page",
"@type": "Resource",
"@type": "Document",
"contributors": [],
"creators": [
"test_user_1_"
Expand All @@ -19,7 +19,6 @@
"description": "",
"title": "Plone site"
},
"portal_type": "Document",
"relatedItems": [],
"rights": "",
"text": "<p>If you're seeing this instead of the web site you were expecting, the owner of this web site has just installed Plone. Do not contact the Plone Team or the Plone mailing lists about this.</p>",
Expand Down
3 changes: 1 addition & 2 deletions docs/source/_json/file.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
{
"@context": "http://www.w3.org/ns/hydra/context.jsonld",
"@id": "http://localhost:55001/plone/file",
"@type": "Resource",
"@type": "File",
"description": "This is a file",
"download": "http://localhost:55001/plone/file/@@download",
"parent": {
"@id": "http://localhost:55001/plone",
"description": "",
"title": "Plone site"
},
"portal_type": "File",
"title": "My File"
}
3 changes: 1 addition & 2 deletions docs/source/_json/folder.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"@context": "http://www.w3.org/ns/hydra/context.jsonld",
"@id": "http://localhost:55001/plone/folder",
"@type": "Collection",
"@type": "Folder",
"contributors": [],
"creators": [
"test_user_1_"
Expand Down Expand Up @@ -34,7 +34,6 @@
"description": "",
"title": "Plone site"
},
"portal_type": "Folder",
"relatedItems": [],
"rights": "",
"title": "My Folder"
Expand Down
3 changes: 1 addition & 2 deletions docs/source/_json/image.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
{
"@context": "http://www.w3.org/ns/hydra/context.jsonld",
"@id": "http://localhost:55001/plone/image",
"@type": "Resource",
"@type": "Image",
"description": "This is an image",
"download": "http://localhost:55001/plone/image/@@download",
"parent": {
"@id": "http://localhost:55001/plone",
"description": "",
"title": "Plone site"
},
"portal_type": "Image",
"scales": {
"icon": "http://localhost:55001/plone/image/@@images/image/icon",
"large": "http://localhost:55001/plone/image/@@images/image/large",
Expand Down
3 changes: 1 addition & 2 deletions docs/source/_json/link.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"@context": "http://www.w3.org/ns/hydra/context.jsonld",
"@id": "http://localhost:55001/plone/link",
"@type": "Resource",
"@type": "Link",
"contributors": [],
"creators": [
"test_user_1_"
Expand All @@ -19,7 +19,6 @@
"description": "",
"title": "Plone site"
},
"portal_type": "Link",
"remoteUrl": "http://",
"rights": "",
"title": "My Link"
Expand Down
3 changes: 1 addition & 2 deletions docs/source/_json/newsitem.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"@context": "http://www.w3.org/ns/hydra/context.jsonld",
"@id": "http://localhost:55001/plone/newsitem",
"@type": "Resource",
"@type": "News Item",
"contributors": [],
"creators": [
"test_user_1_"
Expand All @@ -21,7 +21,6 @@
"description": "",
"title": "Plone site"
},
"portal_type": "News Item",
"relatedItems": [],
"rights": "",
"text": "<p>Lorem ipsum</p>",
Expand Down
5 changes: 2 additions & 3 deletions docs/source/_json/siteroot.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"@context": "http://www.w3.org/ns/hydra/context.jsonld",
"@id": "http://localhost:55001/plone",
"@type": "Collection",
"@type": "SiteRoot",
"member": [
{
"@id": "http://localhost:55001/plone/robot-test-folder",
Expand All @@ -14,6 +14,5 @@
"title": "Welcome to Plone"
}
],
"parent": {},
"portal_type": "SiteRoot"
"parent": {}
}
16 changes: 5 additions & 11 deletions src/plone/restapi/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ def SerializeSiteRootToJson(context):
result = {
'@context': 'http://www.w3.org/ns/hydra/context.jsonld',
'@id': context.absolute_url(),
'@type': 'Collection',
'@type': 'SiteRoot',
'parent': {},
'portal_type': 'SiteRoot'
}
result['member'] = [
{
Expand All @@ -56,14 +55,14 @@ def SerializeToJson(context):
result = {
'@context': 'http://www.w3.org/ns/hydra/context.jsonld',
'@id': context.absolute_url(),
'@type': context.portal_type,
'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'
result['member'] = [
{
'@id': member.absolute_url(),
Expand All @@ -72,8 +71,7 @@ def SerializeToJson(context):
}
for member_id, member in context.objectItems()
]
elif ICollection.providedBy(context):
result['@type'] = 'Collection'
if ICollection.providedBy(context):
portal = getSite()
result['member'] = [
{
Expand All @@ -86,8 +84,6 @@ def SerializeToJson(context):
}
for member in context.results()
]
else:
result['@type'] = 'Resource'
for title, schema_object in get_object_schema(context):
value = getattr(context, title, None)
if value is not None:
Expand Down Expand Up @@ -139,13 +135,12 @@ def SerializeFileToJson(context):
result = {
'@context': 'http://www.w3.org/ns/hydra/context.jsonld',
'@id': context.absolute_url(),
'@type': 'Resource',
'@type': 'File',
'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,
'download': '{0}/@@download'.format(context.absolute_url()),
Expand All @@ -162,13 +157,12 @@ def SerializeImageToJson(context):
result = {
'@context': 'http://www.w3.org/ns/hydra/context.jsonld',
'@id': context.absolute_url(),
'@type': 'Resource',
'@type': 'Image',
'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,
'download': '{0}/@@download'.format(context.absolute_url()),
Expand Down
35 changes: 14 additions & 21 deletions src/plone/restapi/tests/test_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
from plone.restapi.interfaces import ISerializeToJson
from plone.app.textfield.value import RichTextValue

from plone.restapi.testing import \
PLONE_RESTAPI_INTEGRATION_TESTING
from plone.restapi.testing import PLONE_RESTAPI_INTEGRATION_TESTING
from DateTime import DateTime

import json
Expand All @@ -21,22 +20,26 @@ def setUp(self):
self.portal_url = self.portal.absolute_url()
self.portal.invokeFactory('Document', id='doc1', title='Document 1')

def test_serialize_to_json_adapter_returns_hydra_context(self):
def test_serialize_to_json_adapter_returns_context(self):
self.assertEqual(
json.loads(ISerializeToJson(self.portal.doc1))['@context'],
u'http://www.w3.org/ns/hydra/context.jsonld'
)

def test_serialize_to_json_adapter_returns_hydra_id(self):
def test_serialize_to_json_adapter_returns_id(self):
self.assertEqual(
json.loads(ISerializeToJson(self.portal.doc1))['@id'],
self.portal_url + '/doc1'
)

def test_serialize_to_json_adapter_returns_hydra_type(self):
def test_serialize_to_json_adapter_returns_type(self):
self.assertTrue(
json.loads(ISerializeToJson(self.portal.doc1)).get('@type'),
'The @type attribute should be present.'
)
self.assertEqual(
json.loads(ISerializeToJson(self.portal.doc1))['@type'],
u'Resource'
u'Document'
)

def test_serialize_to_json_adapter_returns_title(self):
Expand Down Expand Up @@ -123,23 +126,13 @@ def test_serialize_to_json_adapter_does_not_returns_parent_on_root(self):
'The parent attribute on portal root should be None'
)

def test_serialize_to_json_adapter_returns_portal_type(self):
self.assertTrue(
json.loads(ISerializeToJson(self.portal.doc1)).get('portal_type'),
'The portal_type attribute should be present.'
)
self.assertEqual(
json.loads(ISerializeToJson(self.portal.doc1))['portal_type'],
u'Document'
)

def test_serialize_to_json_adapter_returns_site_root_portal_type(self):
def test_serialize_to_json_adapter_returns_site_root_type(self):
self.assertTrue(
json.loads(ISerializeToJson(self.portal)).get('portal_type'),
'The portal_type attribute should be present.'
json.loads(ISerializeToJson(self.portal)).get('@type'),
'The @type attribute should be present.'
)
self.assertEqual(
json.loads(ISerializeToJson(self.portal))['portal_type'],
json.loads(ISerializeToJson(self.portal))['@type'],
u'SiteRoot'
)

Expand Down Expand Up @@ -194,7 +187,7 @@ def test_serialize_to_json_collection(self):
u'Collection',
json.loads(
ISerializeToJson(self.portal.collection1)
).get('portal_type')
).get('@type')
)
self.assertEqual(
[
Expand Down
6 changes: 2 additions & 4 deletions src/plone/restapi/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ def test_get_object_schema_for_document(self):
'subjects',
'creators',
'description',
'changeNote',
'portal_type'
'changeNote'
]
)

Expand All @@ -69,8 +68,7 @@ def test_get_object_schema_for_folder(self):
'language',
'subjects',
'creators',
'description',
'portal_type'
'description'
]
)

Expand Down
6 changes: 0 additions & 6 deletions src/plone/restapi/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
from zope.schema import getFields
from zope.schema import TextLine
from zope.interface import providedBy
from plone.behavior.interfaces import IBehaviorAssignable

Expand Down Expand Up @@ -28,11 +27,6 @@ def get_object_schema(obj):
object_schema.add(name)
yield name, field

# The portal_type is declared in IDexterityFactory and not included in the
# interfaces that are provided by the object.
# https://github.com/plone/plone.dexterity/blob/master/plone/dexterity/interfaces.py#L94 # noqa
yield 'portal_type', TextLine


def underscore_to_camelcase(underscore_string):
return ''.join(
Expand Down

0 comments on commit 48769d8

Please sign in to comment.