[Zope3-checkins] SVN: Zope3/trunk/src/ Modified 'Permission Ids' vocabulary:

Garrett Smith garrett at mojave-corp.com
Wed Jul 7 14:42:54 EDT 2004


Log message for revision 26177:
Modified 'Permission Ids' vocabulary:

- CheckerPublic is the value for permission ID 'zope.Public'
- 'Public' is the display name (title) instead of 'zope.Public'
- Vocabulary list is sorted alphabetically except that Public appears as first item

Change also includes modifications to test code to properly reset the vocabulary registry per test setup.

This fixes http://collector.zope.org/Zope3-dev/219


-=-
Modified: Zope3/trunk/src/bugtracker/tests/placelesssetup.py
===================================================================
--- Zope3/trunk/src/bugtracker/tests/placelesssetup.py	2004-07-07 18:34:31 UTC (rev 26176)
+++ Zope3/trunk/src/bugtracker/tests/placelesssetup.py	2004-07-07 18:42:54 UTC (rev 26177)
@@ -23,17 +23,16 @@
 
 from zope.component.interfaces import IFactory
 from zope.component.service import defineService, serviceManager
-from zope.component.tests.placelesssetup import PlacelessSetup as ComponentSetup
 from zope.interface import classImplements, implements
 from zope.schema.vocabulary import getVocabularyRegistry
 
 from zope.app import zapi
 from zope.app.tests import ztapi
+from zope.app.tests.placelesssetup import PlacelessSetup as SetupBase
 from zope.app.annotation.attribute import AttributeAnnotations
 from zope.app.file import File
 from zope.app.container.interfaces import INameChooser
 from zope.app.dublincore.annotatableadapter import ZDCAnnotatableAdapter
-from zope.app.event.tests.placelesssetup import PlacelessSetup as EventSetup
 from zope.app.annotation.interfaces import IAnnotations, IAttributeAnnotatable
 from zope.app.dublincore.interfaces import IWriteZopeDublinCore
 from zope.app.dublincore.interfaces import IZopeDublinCore
@@ -68,11 +67,10 @@
     __parent__ = None
     __name__ = ''
 
-class PlacelessSetup(ComponentSetup, EventSetup):
+class PlacelessSetup(SetupBase):
 
     def setUp(self):
-        ComponentSetup.setUp(self)
-        EventSetup.setUp(self)
+        SetupBase.setUp(self)
         classImplements(Bug, IAttributeAnnotatable)
         classImplements(BugTracker, IAttributeAnnotatable)
         classImplements(Comment, IAttributeAnnotatable)

Modified: Zope3/trunk/src/bugtracker/tests/test_vocabularies.py
===================================================================
--- Zope3/trunk/src/bugtracker/tests/test_vocabularies.py	2004-07-07 18:34:31 UTC (rev 26176)
+++ Zope3/trunk/src/bugtracker/tests/test_vocabularies.py	2004-07-07 18:42:54 UTC (rev 26177)
@@ -18,13 +18,13 @@
 import unittest
 
 from zope.component.service import defineService, serviceManager
-from zope.component.tests.placelesssetup import PlacelessSetup
 from zope.interface import classImplements, implements
 from zope.schema.interfaces import ITokenizedTerm
 from zope.schema.vocabulary import getVocabularyRegistry
 
 from zope.app import zapi
 from zope.app.tests import ztapi
+from zope.app.tests.placelesssetup import PlacelessSetup
 from zope.app.annotation.attribute import AttributeAnnotations
 from zope.app.annotation.interfaces import IAnnotations, IAttributeAnnotatable
 from zope.app.container.contained import contained, Contained

Modified: Zope3/trunk/src/zope/app/security/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/app/security/configure.zcml	2004-07-07 18:34:31 UTC (rev 26176)
+++ Zope3/trunk/src/zope/app/security/configure.zcml	2004-07-07 18:42:54 UTC (rev 26177)
@@ -15,11 +15,9 @@
       factory="zope.app.utility.vocabulary.UtilityVocabulary"
       interface="zope.app.security.interfaces.IPermission" />
 
-  <vocabulary 
+  <vocabulary
       name="Permission Ids"
-      factory="zope.app.utility.vocabulary.UtilityVocabulary"
-      interface="zope.app.security.interfaces.IPermission" 
-      nameOnly="True" />
+      factory=".vocabulary.PermissionIdsVocabulary" />
 
   <include file="globalmodules.zcml" />
   <include file="_protections.zcml" />

Added: Zope3/trunk/src/zope/app/security/tests/test_vocabulary.py
===================================================================
--- Zope3/trunk/src/zope/app/security/tests/test_vocabulary.py	2004-07-07 18:34:31 UTC (rev 26176)
+++ Zope3/trunk/src/zope/app/security/tests/test_vocabulary.py	2004-07-07 18:42:54 UTC (rev 26177)
@@ -0,0 +1,27 @@
+##############################################################################
+#
+# Copyright (c) 2003 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.security.vocabulary'),
+        ))
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')

Added: Zope3/trunk/src/zope/app/security/vocabulary.py
===================================================================
--- Zope3/trunk/src/zope/app/security/vocabulary.py	2004-07-07 18:34:31 UTC (rev 26176)
+++ Zope3/trunk/src/zope/app/security/vocabulary.py	2004-07-07 18:42:54 UTC (rev 26177)
@@ -0,0 +1,97 @@
+##############################################################################
+#
+# 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 Id Vocabulary.
+
+This vocabulary provides permission IDs.
+
+$Id: $
+"""
+from zope.security.checker import CheckerPublic
+from zope.app import zapi
+from zope.schema.vocabulary import SimpleTerm, SimpleVocabulary
+from zope.app.security.interfaces import IPermission
+
+
+class PermissionIdsVocabulary(SimpleVocabulary):
+    """A vocabular of permission IDs.
+    
+    Term values are the permission ID strings except for 'zope.Public', which
+    is the global permission CheckerPublic.
+    
+    Term titles are the permission ID strings except for 'zope.Public', which
+    is shortened to 'Public'.
+    
+    Terms are sorted by title except for 'Public', which always appears as
+    the first term.
+
+    To illustrate, we need to register the permission IDs vocab:
+
+        >>> from zope.app.tests.placelesssetup import setUp, tearDown
+        >>> setUp()
+        >>> from zope.schema.vocabulary import getVocabularyRegistry
+        >>> registry = getVocabularyRegistry()
+        >>> registry.register('Permission Ids', PermissionIdsVocabulary)
+
+    We also need to register some sample permission utilities, including
+    the special permission 'zope.Public':
+    
+        >>> from zope.app.security.interfaces import IPermission
+        >>> from zope.app.security.permission import Permission
+        >>> from zope.app.tests import ztapi
+        >>> ztapi.provideUtility(IPermission, Permission('zope.Public'),
+        ...     'zope.Public')
+        >>> ztapi.provideUtility(IPermission, Permission('b'), 'b')
+        >>> ztapi.provideUtility(IPermission, Permission('a'), 'a')
+
+    We can now lookup these permissions using the vocabulary:
+    
+        >>> vocab = registry.get(None, 'Permission Ids')
+
+	The non-public permissions 'a' and 'b' are string values:
+	
+	    >>> vocab.getTermByToken('a').value
+	    u'a'
+	    >>> vocab.getTermByToken('b').value
+	    u'b'
+
+    However, the public permission value is CheckerPublic:
+    
+        >>> vocab.getTermByToken('zope.Public').value is CheckerPublic
+        True
+
+    and its title is shortened:
+
+        >>> vocab.getTermByToken('zope.Public').title
+        u'Public'
+
+    The terms are sorted by title except for the public permission, which is
+    listed first:
+    
+        >>> [term.title for term in vocab]
+        [u'Public', u'a', u'b']
+        
+        >>> tearDown()
+    """
+    def __init__(self, context):
+        terms = []
+        permissions = zapi.getUtilitiesFor(IPermission, context)
+        for name, permission in permissions:
+            if name == 'zope.Public':
+                terms.append(SimpleTerm(
+                    CheckerPublic, 'zope.Public', u'Public'))
+            else:
+                terms.append(SimpleTerm(name, name, name))
+        terms.sort(lambda lhs, rhs: \
+            lhs.title == u'Public' and -1 or cmp(lhs.title, rhs.title))
+        super(PermissionIdsVocabulary, self).__init__(terms)

Modified: Zope3/trunk/src/zope/app/tests/placelesssetup.py
===================================================================
--- Zope3/trunk/src/zope/app/tests/placelesssetup.py	2004-07-07 18:34:31 UTC (rev 26176)
+++ Zope3/trunk/src/zope/app/tests/placelesssetup.py	2004-07-07 18:42:54 UTC (rev 26177)
@@ -16,6 +16,7 @@
 $Id$
 """
 from zope.app.tests import ztapi
+from zope.schema.vocabulary import setVocabularyRegistry
 from zope.component.tests.placelesssetup \
     import PlacelessSetup as CAPlacelessSetup
 from zope.app.event.tests.placelesssetup \
@@ -48,7 +49,9 @@
         from zope.app.security.tests import addCheckerPublic
         addCheckerPublic()
 
+        setVocabularyRegistry(None)
 
+
 ps = PlacelessSetup()
 setUp = ps.setUp
 tearDown = ps.tearDown

Modified: Zope3/trunk/src/zope/schema/vocabulary.py
===================================================================
--- Zope3/trunk/src/zope/schema/vocabulary.py	2004-07-07 18:34:31 UTC (rev 26176)
+++ Zope3/trunk/src/zope/schema/vocabulary.py	2004-07-07 18:42:54 UTC (rev 26177)
@@ -186,8 +186,6 @@
 def setVocabularyRegistry(registry):
     """Set the vocabulary registry."""
     global _vocabularies
-    if _vocabularies is not None:
-        raise ValueError("vocabulary registry has already been set")
     _vocabularies = registry
 
 def _clear():



More information about the Zope3-Checkins mailing list