[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/securitypolicy/
Define a Role Ids vocabulary
Julien Anguenot
ja at nuxeo.com
Wed Oct 13 13:14:35 EDT 2004
Log message for revision 28114:
Define a Role Ids vocabulary
Changed:
U Zope3/trunk/src/zope/app/securitypolicy/configure.zcml
A Zope3/trunk/src/zope/app/securitypolicy/tests/test_vocabulary.py
A Zope3/trunk/src/zope/app/securitypolicy/vocabulary.py
-=-
Modified: Zope3/trunk/src/zope/app/securitypolicy/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/app/securitypolicy/configure.zcml 2004-10-13 17:07:58 UTC (rev 28113)
+++ Zope3/trunk/src/zope/app/securitypolicy/configure.zcml 2004-10-13 17:14:34 UTC (rev 28114)
@@ -98,4 +98,10 @@
<grantAll role="zope.Manager" />
+ <!-- Vocabularies -->
+
+ <vocabulary
+ name="Role Ids"
+ factory=".vocabulary.RoleIdsVocabulary" />
+
</configure>
Added: Zope3/trunk/src/zope/app/securitypolicy/tests/test_vocabulary.py
===================================================================
--- Zope3/trunk/src/zope/app/securitypolicy/tests/test_vocabulary.py 2004-10-13 17:07:58 UTC (rev 28113)
+++ Zope3/trunk/src/zope/app/securitypolicy/tests/test_vocabulary.py 2004-10-13 17:14:34 UTC (rev 28114)
@@ -0,0 +1,27 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Permission vocabluary doc tests.
+
+$Id:$
+"""
+import unittest
+from zope.testing.doctestunit import DocTestSuite
+
+def test_suite():
+ return unittest.TestSuite((
+ DocTestSuite('zope.app.securitypolicy.vocabulary'),
+ ))
+
+if __name__ == '__main__':
+ unittest.main(defaultTest='test_suite')
Added: Zope3/trunk/src/zope/app/securitypolicy/vocabulary.py
===================================================================
--- Zope3/trunk/src/zope/app/securitypolicy/vocabulary.py 2004-10-13 17:07:58 UTC (rev 28113)
+++ Zope3/trunk/src/zope/app/securitypolicy/vocabulary.py 2004-10-13 17:14:34 UTC (rev 28114)
@@ -0,0 +1,66 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Role Id Vocabulary.
+
+This vocabulary provides role IDs.
+
+$Id:$
+"""
+from zope.app import zapi
+from zope.schema.vocabulary import SimpleTerm, SimpleVocabulary
+from zope.app.securitypolicy.interfaces import IRole
+
+class RoleIdsVocabulary(SimpleVocabulary):
+ """A vocabular of role IDs.
+
+ Term values are the role ID strings
+ Term are stored by title
+
+ To illustrate, we need to register the role IDs vocab:
+
+ >>> from zope.app.tests.placelesssetup import setUp, tearDown
+ >>> setUp()
+ >>> from zope.schema.vocabulary import getVocabularyRegistry
+ >>> registry = getVocabularyRegistry()
+ >>> registry.register('Role Ids', RoleIdsVocabulary)
+
+ Let's register some sample roles to test against them
+
+ >>> from zope.app.securitypolicy.interfaces import IRole
+ >>> from zope.app.securitypolicy.role import Role
+ >>> from zope.app.tests import ztapi
+ >>> ztapi.provideUtility(IRole, Role('a_id','a_title'), 'a_id')
+ >>> ztapi.provideUtility(IRole, Role('b_id','b_title'), 'b_id')
+
+ Let's lookup the roles using the vocabulary
+
+ >>> vocab = registry.get(None, 'Role Ids')
+
+ >>> vocab.getTermByToken('a_id').value
+ u'a_id'
+ >>> vocab.getTermByToken('b_id').value
+ u'b_id'
+
+ >>> tearDown()
+
+ """
+
+ def __init__(self, context):
+ terms = []
+ roles = zapi.getUtilitiesFor(IRole, context)
+ for name, role in roles:
+ terms.append(SimpleTerm(name, name, name))
+ super(RoleIdsVocabulary, self).__init__(terms)
+
+
More information about the Zope3-Checkins
mailing list