[z3-checkins] r5259 - z3/Five/trunk

philikon at codespeak.net philikon at codespeak.net
Wed Jun 23 17:59:52 MEST 2004


Author: philikon
Date: Wed Jun 23 17:59:52 2004
New Revision: 5259

Modified:
   z3/Five/trunk/browser.py
   z3/Five/trunk/fiveconfigure.py
Log:
Refactor view class mixins. We now have two very simple mixins:

  - one for attributes: a __browser_default__ tells the publisher where to
    go to and a __call__ makes them still callable from python/ZPT

  - one for views involving templates: a __call__ makes them callable and
    calls the template. a __getitem__ allows easy macro access


Modified: z3/Five/trunk/browser.py
==============================================================================
--- z3/Five/trunk/browser.py	(original)
+++ z3/Five/trunk/browser.py	Wed Jun 23 17:59:52 2004
@@ -11,8 +11,7 @@
 $Id$
 """
 import Acquisition
-from AccessControl import ClassSecurityInfo, getSecurityManager
-from zExceptions import Unauthorized
+from AccessControl import ClassSecurityInfo
 from Globals import InitializeClass
 
 class BrowserView(Acquisition.Explicit):
@@ -25,28 +24,4 @@
     # XXX do not create any methods on the subclass called index_html,
     # as this makes Zope 2 traverse into that first!
 
-    def __call__(self, *args, **kw):
-        # XXX this is definitely not the way Zope 3 does it..
-
-        # XXX and it is only needed for tests so that they can all
-        # views directly. ZPublisher will either find a __call__ on a
-        # view class or use __browswer_default__ from
-        # ViewMixinForAttributes
-        if hasattr(self, 'index'):
-            attr = 'index'
-        else:
-            attr = self.__page_attribute__
-        meth = getattr(self, attr)
-        if attr == '__call__':
-            raise AttributeError("__call__")
-        elif attr == 'index':
-            return meth(self, *args, **kw)
-        # XXX for some reason, validating 'index' doesn't work
-        # as expected. I suspect that its because it's a
-        # ViewPageTemplateFile.
-        security_manager = getSecurityManager()
-        if not security_manager.validate(meth, self, attr, meth):
-            raise Unauthorized
-        return meth(*args, **kw)
-
 InitializeClass(BrowserView)

Modified: z3/Five/trunk/fiveconfigure.py
==============================================================================
--- z3/Five/trunk/fiveconfigure.py	(original)
+++ z3/Five/trunk/fiveconfigure.py	Wed Jun 23 17:59:52 2004
@@ -18,6 +18,10 @@
 from zope.component.servicenames import Adapters, Presentation
 from zope.publisher.interfaces.browser import IBrowserRequest
 from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile
+
+from AccessControl import getSecurityManager
+from zExceptions import Unauthorized
+
 from provideinterface import provideInterface
 from viewable import Viewable
 from api import BrowserView
@@ -72,7 +76,7 @@
         if template:
             attribute = 'index'
             # class and template
-            new_class = SimpleViewClass(
+            new_class = makeClassForTemplate(
                 template, bases=(class_, ),
                 cdict=cdict)
         elif attribute != "__call__":
@@ -86,7 +90,7 @@
     else:
         # template
         attribute = 'index'
-        new_class = SimpleViewClass(template, bases=(BrowserView,))
+        new_class = makeClassForTemplate(template)
 
     _handle_for(_context, for_)
 
@@ -198,18 +202,38 @@
 
 class ViewMixinForAttributes(BrowserView):
 
+    # we have an attribute that we can simply tell ZPublisher to go to
     def __browser_default__(self, request):
-        if hasattr(self, 'index'):
-            attr = 'index'
-        else:
-            attr = self.__page_attribute__
-        return self, (attr,)
+        return self, (self.__page_attribute__,)
+
+    def __call__(self, *args, **kw):
+	attr = self.__page_attribute__
+        meth = getattr(self, attr)
+	#XXX not sure if this is really necessary; we need tests for this
+        #security_manager = getSecurityManager()
+        #if not security_manager.validate(meth, self, attr, meth):
+        #    raise Unauthorized
+        return meth(*args, **kw)
+
+class ViewMixinForTemplates(BrowserView):
+
+    # short cut to get to macros more easily
+    def __getitem__(self, name):
+        return self.index.macros[name]
+
+    # make the template publishable
+    def __call__(self, *args, **kw):
+	# we technically would have to validate here, but it doesn't
+	# seem to work because index is a ViewPageTemplateFile
+	#XXX not sure if we really need to validate here anyway
+        return self.index(self, *args, **kw)
 
-def SimpleViewClass(src, template=None, used_for=None, bases=(), cdict=None):
+def makeClassForTemplate(src, template=None, used_for=None, bases=(), cdict=None):
     # XXX needs to deal with security from the bases?
     if cdict is None:
         cdict = {}
     cdict.update({'index': ViewPageTemplateFile(src, template)})
+    bases += (ViewMixinForTemplates,)
     class_ = makeClass("SimpleViewClass from %s" % src, bases, cdict)
 
     if used_for is not None:


More information about the z3-checkins mailing list