[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/Publisher/Browser - ResourceMeta.py:1.2 ViewMeta.py:1.2

Jim Fulton jim@zope.com
Wed, 19 Jun 2002 17:32:27 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/App/Publisher/Browser
In directory cvs.zope.org:/tmp/cvs-serv6230/lib/python/Zope/App/Publisher/Browser

Modified Files:
	ResourceMeta.py ViewMeta.py 
Log Message:
implemented 
http://dev.zope.org/Wikis/DevSite/Projects/ComponentArchitecture/NamedBrowserViewsAndPages

I think

Well, the tests pass. :)



=== Zope3/lib/python/Zope/App/Publisher/Browser/ResourceMeta.py 1.1 => 1.2 ===
         return pageView
 
-    def __call__(self):
+    def __call__(self, require = None):
         if self.name is None:
             return ()
 
@@ -143,6 +143,9 @@
         factory = self.factory
 
         if permission:
+            if require is None:
+                require = {}
+                
             if permission == 'Zope.Public':
                 permission = CheckerPublic
             
@@ -150,13 +153,14 @@
                 and (not self.pages)):
                 allowed_attributes = self.default_allowed_attributes
 
-            require={}
             for name in (allowed_attributes or '').split():
                 require[name] = permission
+
             if allowed_interface:
                 for name in allowed_interface.names(1):
                     require[name] = permission
 
+        if require:
             checker = Checker(require.get)
 
             factory = self._proxyFactory(factory, checker)


=== Zope3/lib/python/Zope/App/Publisher/Browser/ViewMeta.py 1.1 => 1.2 ===
 """
 
+# XXX this will need to be refactored soon. :)
+
 from Zope.Security.Proxy import Proxy
 from Zope.Security.Checker \
      import CheckerPublic, NamesChecker, Checker
@@ -24,6 +26,8 @@
 from Zope.Configuration.Exceptions import ConfigurationError
 
 from Zope.Publisher.Browser.IBrowserPresentation import IBrowserPresentation
+from Zope.Publisher.Browser.IBrowserView import IBrowserView
+from Zope.Publisher.Browser.IBrowserPublisher import IBrowserPublisher
 
 from Zope.App.ComponentArchitecture.metaConfigure \
      import defaultView as _defaultView, handler
@@ -35,6 +39,9 @@
 
 class view(resource):
 
+    __pages = None
+    __default = None
+
     def __init__(self, _context, factory=None, name=None, for_=None,
                  layer='default',
                  permission=None,
@@ -66,16 +73,37 @@
         resource.__init__(self, _context, factory, name, layer,
                           permission, allowed_interface, allowed_attributes)
 
+        if name:
+            self.__pages = {}
+
 
     def page(self, _context, name, attribute=None, permission=None,
              layer=None, template=None):
 
+
         if self.template:
             raise ConfigurationError(
                 "Can't use page or defaultPage subdirectives for simple "
                 "template views")
 
 
+        if self.name:
+            # Named view with pages.
+
+            if layer is not None:
+                raise ConfigurationError(
+                    "Can't specify a separate layer for pages of named "
+                    "templates.")
+            
+            if template is not None:
+                template = _context.path(template)
+
+            self.__pages[name] = attribute, permission, template
+            if self.__default is None:
+                self.__default = name
+
+            return ()
+
         factory = self.factory
 
         if template is not None:
@@ -92,6 +120,18 @@
             _context, name, attribute, permission, layer,
             factory=factory)
 
+    def defaultPage(self, _context, name):
+        if self.name:
+            self.__default = name
+            return ()
+
+        return [Action(
+            discriminator = ('defaultViewName', self.for_, self.type, name),
+            callable = handler,
+            args = ('Views','setDefaultViewName', self.for_, self.type, name),
+            )]
+        
+
     def _factory(self, _context, factory):
 
         if self.template:
@@ -165,6 +205,57 @@
 
         return factory
 
+    def __call__(self):
+        if not self.__pages:
+            return super(view, self).__call__()
+
+        # OK, we have named pages on a named view.
+        # We'l lreplace the original class with a new subclass that
+        # can traverse to the necessary pages. 
+
+        require = {}
+        klassdict = {'_PageTraverser__pages': {},
+                     '_PageTraverser__default': self.__default,
+                     }
+        for name in self.__pages:
+            attribute, permission, template = self.__pages[name] 
+            if permission == 'Zope.Public':
+                permission = CheckerPublic
+
+            if attribute:
+                require[attribute] = permission
+            else:
+                attribute = name
+                require[attribute] = permission
+
+            if template:
+                klassdict[attribute] = ViewPageTemplateFile(template)
+
+            klassdict['_PageTraverser__pages'][name] = attribute
+
+        factory = self.factory[:]
+        klass = factory[-1]
+        klass = type(klass.__name__,
+                     (PageTraverser, klass, object),
+                     klassdict)
+        factory[-1] = klass
+        self.factory = factory
+
+        return super(view, self).__call__(require=require)
+
+
+class PageTraverser:
+
+    __implements__ = IBrowserPublisher
+
+    def publishTraverse(self, request, name):
+        return getattr(self, self._PageTraverser__pages[name])
+
+    def browserDefault(self, request):
+        return self, (self._PageTraverser__default, )
+
+
+
 def defaultView(_context, name, for_=None, **__kw):
 
     if __kw:
@@ -184,3 +275,6 @@
         )]
 
     return actions
+
+
+