[Zope3-checkins] SVN: Zope3/trunk/ - added 'permission' attribute to browser:menuItems directive

Christian Theune ct at gocept.com
Mon Sep 11 16:47:16 EDT 2006


Log message for revision 70102:
   - added 'permission' attribute to browser:menuItems directive
  

Changed:
  U   Zope3/trunk/doc/CHANGES.txt
  U   Zope3/trunk/src/zope/app/publisher/browser/menumeta.py
  U   Zope3/trunk/src/zope/app/publisher/browser/metadirectives.py
  A   Zope3/trunk/src/zope/app/publisher/browser/tests/menus-permissions.zcml
  U   Zope3/trunk/src/zope/app/publisher/browser/tests/test_menudirectives.py

-=-
Modified: Zope3/trunk/doc/CHANGES.txt
===================================================================
--- Zope3/trunk/doc/CHANGES.txt	2006-09-11 19:53:08 UTC (rev 70101)
+++ Zope3/trunk/doc/CHANGES.txt	2006-09-11 20:47:15 UTC (rev 70102)
@@ -10,6 +10,10 @@
 
     New features
 
+      - Added attribute `permission` to the `browser:menuItems`-directive to
+        allow setting a default value for all menu items defined in this
+        directive.
+
       - Added a new interface decorator to zope.interface that allows the
         setting tagged values on an interface at definition time (see
         zope.interface.taggedValue).

Modified: Zope3/trunk/src/zope/app/publisher/browser/menumeta.py
===================================================================
--- Zope3/trunk/src/zope/app/publisher/browser/menumeta.py	2006-09-11 19:53:08 UTC (rev 70101)
+++ Zope3/trunk/src/zope/app/publisher/browser/menumeta.py	2006-09-11 20:47:15 UTC (rev 70102)
@@ -144,10 +144,11 @@
 class menuItemsDirective(object):
     """Register several menu items for a particular menu."""
 
-    def __init__(self, _context, menu, for_, layer=IDefaultBrowserLayer):
+    def __init__(self, _context, menu, for_, layer=IDefaultBrowserLayer, permission=None):
         self.for_ = for_
         self.menuItemType = menu
         self.layer = layer
+        self.permission = permission
 
     def menuItem(self, _context, action, title, description=u'',
                  icon=None, filter=None, permission=None, extra=None, order=0):
@@ -155,6 +156,9 @@
         if filter is not None:
             filter = Engine.compile(filter)
 
+        if permission is None:
+            permission = self.permission
+
         if order == 0:
             order = _order_counter.get(self.for_, 1)
             _order_counter[self.for_] = order + 1
@@ -174,6 +178,9 @@
         if filter is not None:
             filter = Engine.compile(filter)
 
+        if permission is None:
+            permission = self.permission
+
         if order == 0:
             order = _order_counter.get(self.for_, 1)
             _order_counter[self.for_] = order + 1
@@ -271,6 +278,6 @@
     if menu == None:
         menu = AddMenu
 
-    return menuItemsDirective(_context, menu, for_, layer).menuItem(
+    return menuItemsDirective(_context, menu, for_, layer=layer).menuItem(
         _context, action, title, description, icon, filter,
         permission, extra, order)

Modified: Zope3/trunk/src/zope/app/publisher/browser/metadirectives.py
===================================================================
--- Zope3/trunk/src/zope/app/publisher/browser/metadirectives.py	2006-09-11 19:53:08 UTC (rev 70101)
+++ Zope3/trunk/src/zope/app/publisher/browser/metadirectives.py	2006-09-11 20:47:15 UTC (rev 70102)
@@ -424,6 +424,18 @@
         required=False
         )
 
+    permission = Permission(
+        title=u"The permission needed access the item",
+        description=u"""
+        This can usually be inferred by the system, however, doing so
+        may be expensive. When displaying a menu, the system tries to
+        traverse to the URLs given in each action to determine whether
+        the url is accessible to the current user. This can be
+        avoided if the permission is given explicitly.""",
+        required=False
+        )
+
+
 class IMenuItem(Interface):
     """Common menu item configuration
     """

Added: Zope3/trunk/src/zope/app/publisher/browser/tests/menus-permissions.zcml
===================================================================
--- Zope3/trunk/src/zope/app/publisher/browser/tests/menus-permissions.zcml	2006-09-11 19:53:08 UTC (rev 70101)
+++ Zope3/trunk/src/zope/app/publisher/browser/tests/menus-permissions.zcml	2006-09-11 20:47:15 UTC (rev 70102)
@@ -0,0 +1,19 @@
+<configure
+    xmlns="http://namespaces.zope.org/zope"
+    xmlns:browser="http://namespaces.zope.org/browser"
+    i18n_domain="zope">
+
+  <permission id="zope.View" title="Test permission"/>
+
+  <browser:menu 
+      id="test_id" 
+      title="test menu" />
+
+  <browser:menuItems 
+      menu="test_id"
+      for=".tests.test_menudirectives.I2"
+      permission="zope.View">
+    <browser:menuItem action="b1" title="b1" />
+  </browser:menuItems>
+
+</configure>


Property changes on: Zope3/trunk/src/zope/app/publisher/browser/tests/menus-permissions.zcml
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: Zope3/trunk/src/zope/app/publisher/browser/tests/test_menudirectives.py
===================================================================
--- Zope3/trunk/src/zope/app/publisher/browser/tests/test_menudirectives.py	2006-09-11 19:53:08 UTC (rev 70101)
+++ Zope3/trunk/src/zope/app/publisher/browser/tests/test_menudirectives.py	2006-09-11 20:47:15 UTC (rev 70102)
@@ -22,12 +22,17 @@
 from zope.publisher.browser import TestRequest
 from zope.publisher.interfaces.browser import IBrowserPublisher
 from zope.publisher.interfaces.browser import IDefaultBrowserLayer
+from zope.app.publisher.interfaces.browser import IBrowserMenu
 from zope.security.interfaces import Unauthorized, Forbidden
+import zope.component
 
+import zope.security
+
 from zope.app.testing.placelesssetup import PlacelessSetup
 
 import zope.app.publisher.browser
 
+
 template = """<configure
    xmlns='http://namespaces.zope.org/zope'
    xmlns:browser='http://namespaces.zope.org/browser'
@@ -40,9 +45,15 @@
 class I12(I1): pass
 class I111(I11): pass
 
+
 class C1(object):
     implements(I1)
-            
+
+class I2(Interface): pass
+
+class C2(object):
+    implements(I2)
+
 class TestObject(object):
     implements(IBrowserPublisher, I111)
 
@@ -65,6 +76,25 @@
 class IMySkin(IMyLayer, IDefaultBrowserLayer):
     pass
 
+
+class TestPermissions(PlacelessSetup, unittest.TestCase):
+
+    def setUp(self):
+        super(TestPermissions, self).setUp()
+        XMLConfig('meta.zcml', zope.app.publisher.browser)()
+        XMLConfig('meta.zcml', zope.security)()
+
+    def testMenuItemsPermission(self):
+        XMLConfig('tests/menus-permissions.zcml', zope.app.publisher.browser)()
+
+        menu = zope.component.getUtility(IBrowserMenu, 'test_id')
+        # This is a bit icky, but the menu hides too much stuff from us.
+        items = zope.component.getAdapters((C2(), TestRequest()),
+                                           menu.getMenuItemType())
+        item = list(items)[0][1]
+        self.assertEquals("zope.View", item.permission)
+
+
 class Test(PlacelessSetup, unittest.TestCase):
 
     def setUp(self):
@@ -110,7 +140,7 @@
 
     def testMenuItemWithLayer(self):
         XMLConfig('tests/menus.zcml', zope.app.publisher.browser)()
-        
+
         menu = zope.app.publisher.browser.menu.getMenu(
             'test_id', TestObject(), TestRequest())
         self.assertEqual(len(menu), 6)
@@ -123,10 +153,10 @@
             'test_id', TestObject(), TestRequest(skin=IMySkin))
         self.assertEqual(len(menu), 8)
 
-
 def test_suite():
     return unittest.TestSuite((
         unittest.makeSuite(Test),
+        unittest.makeSuite(TestPermissions),
         ))
 
 if __name__=='__main__':



More information about the Zope3-Checkins mailing list