Merge pull request #7 from headnet/master
FTI can now get additional schemata using behavior utility lookup
This commit is contained in:
commit
dd5a96917d
|
@ -4,8 +4,12 @@ Changelog
|
|||
2.0.1 (unreleased)
|
||||
------------------
|
||||
|
||||
- Nothing changed yet.
|
||||
- Added support in the FTI to look up behaviors by utility name when
|
||||
getting additional schemata (i.e. fields provided by behaviors).
|
||||
|
||||
This functionality makes it possible to create a behavior where the
|
||||
interface is dynamically generated.
|
||||
[malthe]
|
||||
|
||||
2.0 (2012-08-30)
|
||||
----------------
|
||||
|
|
|
@ -40,5 +40,50 @@ class TestUtils(MockTestCase):
|
|||
self.assertEquals(('prefix', 'type one.two', '',),
|
||||
utils.splitSchemaName('prefix_0_type_1_one_2_two'))
|
||||
|
||||
def test_getAdditionalSchemata(self):
|
||||
from plone.dexterity.interfaces import IDexterityFTI
|
||||
from plone.behavior.interfaces import IBehavior
|
||||
from plone.autoform.interfaces import IFormFieldProvider
|
||||
|
||||
from zope.interface import Interface
|
||||
from zope.interface import providedBy
|
||||
|
||||
class IBehaviorInterface(Interface):
|
||||
pass
|
||||
|
||||
class IBehaviorSchema(Interface):
|
||||
pass
|
||||
|
||||
behavior_mock = self.mocker.mock()
|
||||
fti_mock = self.mocker.mock()
|
||||
provider_mock = self.mocker.mock()
|
||||
|
||||
portal_type = 'prefix_0_type_0_schema'
|
||||
behavior_name = 'behavior_0'
|
||||
|
||||
fti_mock.behaviors
|
||||
self.mocker.result((behavior_name, ))
|
||||
|
||||
behavior_mock.interface
|
||||
self.mocker.result(IBehaviorInterface)
|
||||
|
||||
provider_mock(IBehaviorInterface)
|
||||
self.mocker.result(IBehaviorSchema)
|
||||
|
||||
self.mock_utility(behavior_mock, IBehavior, behavior_name)
|
||||
self.mock_utility(fti_mock, IDexterityFTI, portal_type)
|
||||
self.mock_adapter(provider_mock, IFormFieldProvider,
|
||||
(providedBy(IBehaviorInterface), ))
|
||||
|
||||
self.replay()
|
||||
|
||||
generator = utils.getAdditionalSchemata(None, portal_type)
|
||||
schematas = tuple(generator)
|
||||
|
||||
self.assertEqual(len(schematas), 1)
|
||||
schemata = schematas[0]
|
||||
self.assertTrue(schemata is IBehaviorSchema)
|
||||
|
||||
|
||||
def test_suite():
|
||||
return unittest.defaultTestLoader.loadTestsFromName(__name__)
|
||||
|
|
|
@ -11,6 +11,7 @@ from zope.dottedname.resolve import resolve
|
|||
from zope.event import notify
|
||||
from zope.lifecycleevent import ObjectCreatedEvent
|
||||
|
||||
from plone.behavior.interfaces import IBehavior
|
||||
from plone.autoform.interfaces import IFormFieldProvider
|
||||
from plone.behavior.interfaces import IBehaviorAssignable
|
||||
from plone.dexterity.interfaces import IDexterityFTI
|
||||
|
@ -208,11 +209,17 @@ def getAdditionalSchemata(context=None, portal_type=None):
|
|||
portal_type = context.portal_type
|
||||
fti = getUtility(IDexterityFTI, name=portal_type)
|
||||
for behavior_name in fti.behaviors:
|
||||
try:
|
||||
behavior_interface = resolveDottedName(behavior_name)
|
||||
except (ValueError, ImportError):
|
||||
log.warning("Error resolving behaviour %s", behavior_name)
|
||||
continue
|
||||
behavior_interface = None
|
||||
behavior_instance = queryUtility(IBehavior, name=behavior_name)
|
||||
if not behavior_instance:
|
||||
try:
|
||||
behavior_interface = resolveDottedName(behavior_name)
|
||||
except (ValueError, ImportError):
|
||||
log.warning("Error resolving behaviour %s", behavior_name)
|
||||
continue
|
||||
else:
|
||||
behavior_interface = behavior_instance.interface
|
||||
|
||||
if behavior_interface is not None:
|
||||
behavior_schema = IFormFieldProvider(behavior_interface, None)
|
||||
if behavior_schema is not None:
|
||||
|
|
Reference in New Issue