[Zope3-checkins] CVS: Zope3/src/zope/app/publisher/browser - globalbrowsermenuservice.py:1.13 meta.zcml:1.7 viewmeta.py:1.20

Godefroid Chapelle gotcha@swing.be
Fri, 11 Apr 2003 18:16:17 -0400


Update of /cvs-repository/Zope3/src/zope/app/publisher/browser
In directory cvs.zope.org:/tmp/cvs-serv9040/app/publisher/browser

Modified Files:
	globalbrowsermenuservice.py meta.zcml viewmeta.py 
Log Message:
- added usage argument to ZCML menu directive
  The menu usage is used by the configuration to initialize 
  the page view usage in case a page is registered as a
  menu item included in a menu declared with a usage argument.

- added corresponding tests

- various ZCML refactorings to improve usage initialization

- added tests for menu and usage on page directive



=== Zope3/src/zope/app/publisher/browser/globalbrowsermenuservice.py 1.12 => 1.13 ===
--- Zope3/src/zope/app/publisher/browser/globalbrowsermenuservice.py:1.12	Wed Apr  9 16:51:32 2003
+++ Zope3/src/zope/app/publisher/browser/globalbrowsermenuservice.py	Fri Apr 11 18:15:47 2003
@@ -33,7 +33,18 @@
 from zope.app.pagetemplate.engine import Engine
 from zope.app.publication.browser import PublicationTraverser
 
-class GlobalBrowserMenuService:
+class Menu(object):
+    '''Browser menu
+    '''
+    
+    def __init__(self, title, description=u'', usage=u''):
+        self.title = title
+        self.description = description
+        self.usage = usage
+        self.registry = TypeRegistry()
+        
+
+class GlobalBrowserMenuService(object):
     """Global Browser Menu Service
     """
 
@@ -44,19 +55,19 @@
 
     _clear = __init__
 
-    def menu(self, menu_id, title, description=''):
+    def menu(self, menu_id, title, description=u'', usage=u''):
         # XXX we have nothing to do with the title and description. ;)
 
         if menu_id in self._registry:
             raise DuplicationError("Menu %s is already defined." % menu_id)
 
-        self._registry[menu_id] = TypeRegistry()
+        self._registry[menu_id] = Menu(title, description, usage)
 
     def menuItem(self, menu_id, interface, action, title,
                  description='', filter_string=None, permission=None,
                  ):
 
-        registry = self._registry[menu_id]
+        registry = self._registry[menu_id].registry
 
         if filter_string:
             filter = Engine.compile(filter_string)
@@ -74,7 +85,7 @@
         registry.register(interface, data)
 
     def getMenu(self, menu_id, object, request, max=999999):
-        registry = self._registry[menu_id]
+        registry = self._registry[menu_id].registry
         traverser = PublicationTraverser()
 
         result = []
@@ -139,17 +150,21 @@
 
         return result
 
+    def getMenuUsage(self, menu_id):
+        return self._registry[menu_id].usage
+        
+
     def getFirstMenuItem(self, menu_id, object, request):
         r = self.getMenu(menu_id, object, request, max=1)
         if r:
             return r[0]
         return None
 
-def menuDirective(_context, id, title, description=''):
+def menuDirective(_context, id, title, description='', usage=u''):
     return [Action(
         discriminator = ('browser:menu', id),
         callable = globalBrowserMenuService.menu,
-        args = (id, title, description),
+        args = (id, title, description, usage),
         )]
 
 def menuItemDirective(_context, menu, for_,


=== Zope3/src/zope/app/publisher/browser/meta.zcml 1.6 => 1.7 ===
--- Zope3/src/zope/app/publisher/browser/meta.zcml:1.6	Tue Dec 31 13:26:58 2002
+++ Zope3/src/zope/app/publisher/browser/meta.zcml	Fri Apr 11 18:15:47 2003
@@ -130,6 +130,20 @@
           </description>
       </attribute>
 
+      <attribute name="usage" required="no">
+        <description>
+          The template usage top-level variable
+
+          See the usage documentation in the README.txt in the 
+          zope/app/browser/skins directory.
+          If this view is associated with a menu item, this attribute should
+          not be supplied as the view will get its usage from the menu the
+          menu item is registered to.
+          This attribute is available for views not associated with a menu
+          item.
+        </description>
+      </attribute>
+
     </directive>
 
     <directive name="pages" handler=".viewmeta.pages">
@@ -681,16 +695,30 @@
               The name of the menu.
 
               This is, effectively, an id.     
-      </description>
-   </attribute>
+        </description>
+      </attribute>
 
 
       <attribute
           name="title"
           description="A descriptive title for documentation purposes"
           />
-      </directive>
 
+      <attribute name="usage">
+        <description>
+          The templates usage top-level variable
+
+          See the usage documentation in the README.txt in the 
+          zope/app/browser/skins directory.
+          If a view is associated with a menu item, the view will get its 
+          usage from the menu the menu item is registered to.
+        </description>
+      </attribute>
+
+    </directive>
+
+
+    
     <directive
         name="menuItems"
         attributes="menu for" 


=== Zope3/src/zope/app/publisher/browser/viewmeta.py 1.19 => 1.20 ===
--- Zope3/src/zope/app/publisher/browser/viewmeta.py:1.19	Wed Apr  9 16:51:32 2003
+++ Zope3/src/zope/app/publisher/browser/viewmeta.py	Fri Apr 11 18:15:47 2003
@@ -50,7 +50,7 @@
 from zope.proxy.context import ContextMethod
 
 from zope.app.publisher.browser.globalbrowsermenuservice \
-     import menuItemDirective
+     import menuItemDirective, globalBrowserMenuService
 
 # There are three cases we want to suport:
 #
@@ -143,6 +143,7 @@
                     "The provided class doesn't have the specified attribute "
                     )
         if template:
+            # class and template
             template = str(_context.path(template))
             new_class = SimpleViewClass(
                 template, bases=(original_class, ), usage=usage
@@ -162,12 +163,14 @@
             new_class = type(original_class.__name__,
                           (original_class, simple,),
                           cdict)
+            new_class.usage = usage              
 
         if hasattr(original_class, '__implements__'):
             implements(new_class, IBrowserPublisher)
             implements(new_class, IBrowserPresentation, check=False)
 
     else:
+        # template
         new_class = SimpleViewClass(template, usage=usage)
 
     for n in (attribute, 'browserDefault', '__call__', 'publishTraverse'):
@@ -190,6 +193,14 @@
           )
         )
 
+    if not usage and menu:
+        actions.append(
+            Action(discriminator = None,
+            callable = _handle_usage_from_menu,
+            args = (new_class, menu, ),
+            )
+        )
+
     return actions
 
 
@@ -260,6 +271,7 @@
         self.pages = []
         # default usage is u''
         self.usage = usage
+        self.menu = menu
 
     def page(self, _context, name, attribute=None, template=None, usage=None):
         if template:
@@ -463,6 +475,10 @@
     if allowed_attributes.strip():
         for name in allowed_attributes.strip().split():
             required[name] = permission
+
+def _handle_usage_from_menu(view, menu_id):
+    usage = globalBrowserMenuService.getMenuUsage(menu_id)
+    view.usage = usage
 
 def _handle_for(_context, for_, actions):
     if for_ == '*':