Skip to content

Commit

Permalink
use dexterity utils for iterating fields.
Browse files Browse the repository at this point in the history
Use iterSchemata and getFields instead of the custom get_object_schema
implementation.
  • Loading branch information
jone committed Nov 12, 2015
1 parent 7fa221f commit 432ee38
Show file tree
Hide file tree
Showing 12 changed files with 6 additions and 149 deletions.
3 changes: 0 additions & 3 deletions docs/source/_json/collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ content-type: application/json
"effective": null,
"exclude_from_nav": false,
"expires": null,
"icon": "++resource++plone.dexterity.item.gif",
"isPrincipiaFolderish": 0,
"item_count": 30,
"language": "",
"limit": 1000,
Expand All @@ -46,7 +44,6 @@ content-type: application/json
"title": "Document 2"
}
],
"meta_type": "Dexterity Item",
"parent": {
"@id": "http://localhost:55001/plone",
"description": "",
Expand Down
3 changes: 0 additions & 3 deletions docs/source/_json/document.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ content-type: application/json
"effective": null,
"exclude_from_nav": false,
"expires": null,
"icon": "++resource++plone.dexterity.item.gif",
"isPrincipiaFolderish": 0,
"language": "",
"meta_type": "Dexterity Item",
"parent": {
"@id": "http://localhost:55001/plone",
"description": "",
Expand Down
3 changes: 0 additions & 3 deletions docs/source/_json/event.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,8 @@ content-type: application/json
"event_url": null,
"exclude_from_nav": false,
"expires": null,
"icon": "++resource++plone.dexterity.item.gif",
"isPrincipiaFolderish": 0,
"language": "",
"location": null,
"meta_type": "Dexterity Item",
"open_end": false,
"parent": {
"@id": "http://localhost:55001/plone",
Expand Down
3 changes: 0 additions & 3 deletions docs/source/_json/file.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ content-type: application/json
"exclude_from_nav": false,
"expires": null,
"file": "http://localhost:55001/plone/file/@@download/file",
"icon": "++resource++plone.dexterity.item.gif",
"isPrincipiaFolderish": 0,
"language": "",
"meta_type": "Dexterity Item",
"parent": {
"@id": "http://localhost:55001/plone",
"description": "",
Expand Down
5 changes: 0 additions & 5 deletions docs/source/_json/folder.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ content-type: application/json
"effective": null,
"exclude_from_nav": false,
"expires": null,
"icon": "++resource++plone.dexterity.item.gif",
"isAnObjectManager": 1,
"isPrincipiaFolderish": 1,
"language": "",
"member": [
{
Expand All @@ -34,8 +31,6 @@ content-type: application/json
"title": "A document within a folder"
}
],
"meta_type": "Dexterity Container",
"meta_types": [],
"nextPreviousEnabled": false,
"parent": {
"@id": "http://localhost:55001/plone",
Expand Down
3 changes: 0 additions & 3 deletions docs/source/_json/image.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ content-type: application/json
"effective": null,
"exclude_from_nav": false,
"expires": null,
"icon": "++resource++plone.dexterity.item.gif",
"image": {
"icon": "http://localhost:55001/plone/image/@@images/image/icon",
"large": "http://localhost:55001/plone/image/@@images/image/large",
Expand All @@ -29,9 +28,7 @@ content-type: application/json
"thumb": "http://localhost:55001/plone/image/@@images/image/thumb",
"tile": "http://localhost:55001/plone/image/@@images/image/tile"
},
"isPrincipiaFolderish": 0,
"language": "",
"meta_type": "Dexterity Item",
"parent": {
"@id": "http://localhost:55001/plone",
"description": "",
Expand Down
3 changes: 0 additions & 3 deletions docs/source/_json/link.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ content-type: application/json
"effective": null,
"exclude_from_nav": false,
"expires": null,
"icon": "++resource++plone.dexterity.item.gif",
"isPrincipiaFolderish": 0,
"language": "",
"meta_type": "Dexterity Item",
"parent": {
"@id": "http://localhost:55001/plone",
"description": "",
Expand Down
3 changes: 0 additions & 3 deletions docs/source/_json/newsitem.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ content-type: application/json
"effective": null,
"exclude_from_nav": false,
"expires": null,
"icon": "++resource++plone.dexterity.item.gif",
"image": {
"icon": "http://localhost:55001/plone/newsitem/@@images/image/icon",
"large": "http://localhost:55001/plone/newsitem/@@images/image/large",
Expand All @@ -31,9 +30,7 @@ content-type: application/json
"tile": "http://localhost:55001/plone/newsitem/@@images/image/tile"
},
"image_caption": "This is an image caption.",
"isPrincipiaFolderish": 0,
"language": "",
"meta_type": "Dexterity Item",
"parent": {
"@id": "http://localhost:55001/plone",
"description": "",
Expand Down
10 changes: 6 additions & 4 deletions src/plone/restapi/serializer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
from Products.CMFPlone.interfaces import IPloneSiteRoot

from plone.app.contenttypes.interfaces import ICollection
from plone.restapi.utils import get_object_schema
from plone.restapi.interfaces import ISerializeToJson
from plone.restapi.interfaces import IFieldSerializer
from plone.restapi.serializer.converters import json_compatible
from plone.dexterity.utils import iterSchemata

from zope.site.hooks import getSite
from zope.interface import implementer
from zope.component import adapter
from zope.component import getMultiAdapter
from zope.component import queryMultiAdapter
from zope.schema import getFields


def lookup_field_serializer(context, field):
Expand Down Expand Up @@ -92,8 +93,9 @@ def SerializeToJson(context):
for member in context.results()
]

for field_name, field in get_object_schema(context):
value = lookup_field_serializer(context, field)()
result[json_compatible(field_name)] = value
for schemata in iterSchemata(context):
for field_name, field in getFields(schemata).items():
value = lookup_field_serializer(context, field)()
result[json_compatible(field_name)] = value

return result
3 changes: 0 additions & 3 deletions src/plone/restapi/tests/test_serializer_dexterity.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ def test_document(self):
u'effective': None,
u'exclude_from_nav': False,
u'expires': None,
u'icon': u'++resource++plone.dexterity.item.gif',
u'isPrincipiaFolderish': 0,
u'language': u'',
u'meta_type': u'Dexterity Item',
u'relatedItems': [],
u'rights': u'',
u'subjects': [],
Expand Down
94 changes: 0 additions & 94 deletions src/plone/restapi/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,101 +1,7 @@
# -*- coding: utf-8 -*-
from plone.app.testing import setRoles
from plone.app.testing import TEST_USER_ID
from plone.restapi.utils import append_json_to_links
from plone.restapi.utils import get_object_schema
from plone.restapi.testing import PLONE_RESTAPI_INTEGRATION_TESTING

import unittest

try:
from Products.CMFPlone.factory import _IMREALLYPLONE5 # noqa
except ImportError:
ADDITIONAL_PLONE_5_FIELDS = [] # pragma: no cover
else:
ADDITIONAL_PLONE_5_FIELDS = ['id'] # pragma: no cover

BASE_SCHEMA = [
'title',
'allow_discussion',
'exclude_from_nav',
'relatedItems',
'meta_type',
'isPrincipiaFolderish',
'icon',
'rights',
'contributors',
'effective',
'expires',
'language',
'subjects',
'creators',
'description',
] + ADDITIONAL_PLONE_5_FIELDS


class GetObjectSchemaUnitTest(unittest.TestCase):

layer = PLONE_RESTAPI_INTEGRATION_TESTING

def setUp(self):
self.app = self.layer['app']
self.portal = self.layer['portal']
setRoles(self.portal, TEST_USER_ID, ['Manager'])

def test_get_object_schema_for_document(self):
self.portal.invokeFactory('Document', id='doc1', title='Doc 1')
schema = [x[0] for x in get_object_schema(self.portal.doc1)]
self.assertEqual(
set(schema),
set(BASE_SCHEMA + [
'table_of_contents',
'text',
'changeNote',
])
)

def test_get_object_schema_for_folder(self):
self.portal.invokeFactory('Folder', id='folder1', title='Folder 1')
schema = [x[0] for x in get_object_schema(self.portal.folder1)]
self.assertEqual(
set(schema),
set(BASE_SCHEMA + [
'nextPreviousEnabled',
'isAnObjectManager',
'meta_types'
])
)

def test_get_object_schema_for_news_item(self):
self.portal.invokeFactory(
'News Item',
id='newsitem1',
title='News Item 1'
)
schema = [x[0] for x in get_object_schema(self.portal.newsitem1)]
self.assertEqual(
set(schema),
set(BASE_SCHEMA + [
# 'table_of_contents', XXX: Why no toc?
'text',
'changeNote',
'image',
'image_caption',
])
)

def test_get_object_schema_for_image(self):
self.portal.invokeFactory(
'Image',
id='image1',
title='Image 1'
)
schema = [x[0] for x in get_object_schema(self.portal.image1)]
self.assertEqual(
set(schema),
set(BASE_SCHEMA + ['image'])
)


class AppendJsonToLinksUnitTest(unittest.TestCase):

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


def get_object_schema(obj):

# Iterate over all interfaces that are provided by the object and filter
# out all attributes that start with '_' or 'manage'.
for iface in providedBy(obj).flattened():
for name, field in getFields(iface).items():
no_underscore_method = not name.startswith('_')
no_manage_method = not name.startswith('manage')
if no_underscore_method and no_manage_method:
yield name, field

# Iterate over all behaviors that are assigned to the object.
assignable = IBehaviorAssignable(obj, None)
if assignable:
for behavior in assignable.enumerateBehaviors():
for name, field in getFields(behavior.interface).items():
yield name, field


def append_json_to_links(result):
Expand Down

0 comments on commit 432ee38

Please sign in to comment.