[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/publisher/browser/menumeta.py Create a new action to check the view existence within the addMenuItem directive (XXX not finished)

Julien Anguenot ja at nuxeo.com
Fri Jul 29 18:23:03 EDT 2005


Log message for revision 37578:
  Create a new action to check the view existence within the addMenuItem directive (XXX not finished)

Changed:
  U   Zope3/trunk/src/zope/app/publisher/browser/menumeta.py

-=-
Modified: Zope3/trunk/src/zope/app/publisher/browser/menumeta.py
===================================================================
--- Zope3/trunk/src/zope/app/publisher/browser/menumeta.py	2005-07-29 21:52:56 UTC (rev 37577)
+++ Zope3/trunk/src/zope/app/publisher/browser/menumeta.py	2005-07-29 22:23:02 UTC (rev 37578)
@@ -17,6 +17,7 @@
 """
 from zope.configuration.exceptions import ConfigurationError
 from zope.interface.interface import InterfaceClass
+from zope.interface import Interface
 from zope.publisher.interfaces.browser import IDefaultBrowserLayer
 from zope.security.checker import InterfaceChecker, CheckerPublic
 
@@ -50,7 +51,7 @@
 def menuDirective(_context, id=None, class_=BrowserMenu, interface=None,
                   title=u'', description=u''):
     """Registers a new browser menu."""
-    if id is None and interface is None: 
+    if id is None and interface is None:
         raise ConfigurationError(
             "You must specify the 'id' or 'interface' attribute.")
 
@@ -129,7 +130,7 @@
         if 'permission' in kwargs and kwargs['permission'] == 'zope.Public':
             kwargs['permission'] = CheckerPublic
         self.kwargs = kwargs
-    
+
     def __call__(self, context, request):
         item = self.factory(context, request)
 
@@ -187,15 +188,35 @@
             _for=self.for_, submenuId=submenu)
         adapter(_context, (factory,), self.menuItemType,
                 (self.for_, self.layer), name=title)
-        
+
     def __call__(self, _context):
         # Nothing to do.
         pass
 
+def _checkViewFor(class_=None, factory=None, for_=None, view=''):
 
-def addMenuItem(_context, title, description='', menu=None, for_=None, 
-                class_=None, factory=None, view=None, icon=None, filter=None, 
-                permission=None, layer=IDefaultBrowserLayer, extra=None, 
+    if not view:
+        # XXX verbose error
+        raise ConfigurationError(
+            "Within a addMenuItem directive the view attribut"
+            " is optional but can\'t be empty")
+    
+    # Check if there is a view of that name registered for IAdding and
+    # IBrowserRequest. If not raise a ConfigurationError
+
+    gsm = zapi.getGlobalSiteManager()
+
+    # XXX returns None all the time
+    ##if gsm.adapters.lookup((IBrowserRequest, IAdding),
+    ##                       Interface, view) is None:
+    ##    print "view name %s not found " %view
+    ##    raise ConfigurationError(
+    ##        "view name %s not found " %view
+    ##        )
+        
+def addMenuItem(_context, title, description='', menu=None, for_=None,
+                class_=None, factory=None, view=None, icon=None, filter=None,
+                permission=None, layer=IDefaultBrowserLayer, extra=None,
                 order=0):
     """Create an add menu item for a given class or factory
 
@@ -221,7 +242,7 @@
             menu = zapi.getUtility(IMenuItemType, menu)
             if menu is None:
                 raise ValueError("Missing menu id '%s'" % menu)
-    
+
     if class_ is None:
         if factory is None:
             raise ValueError("Must specify either class or factory")
@@ -232,14 +253,20 @@
             raise ValueError(
                 "A permission must be specified when a class is used")
         factory = "BrowserAdd%s__%s.%s" % (
-            forname, class_.__module__, class_.__name__) 
+            forname, class_.__module__, class_.__name__)
         ContentDirective(_context, class_).factory(_context, id=factory)
 
     extra = {'factory': factory}
 
     if view:
-        # XXX : (#307) Here we don't know if it's a valid view or not
         action = view
+        # This action will check if the view exists
+        _context.action(
+            discriminator = None,
+            callable = _checkViewFor,
+            args = (class_, factory, for_, view),
+            order=999999
+            )
     else:
         action = factory
 



More information about the Zope3-Checkins mailing list