[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/ The default skin wasn't being set for calls to runurl. Change includes a function for setting the default skin with proper tests.

Garrett Smith garrett at mojave-corp.com
Mon Apr 11 18:20:42 EDT 2005


Log message for revision 29945:
  The default skin wasn't being set for calls to runurl. Change includes a function for setting the default skin with proper tests.

Changed:
  U   Zope3/trunk/src/zope/app/debug/debug.py
  U   Zope3/trunk/src/zope/app/publication/browser.py
  U   Zope3/trunk/src/zope/app/publication/httpfactory.py
  U   Zope3/trunk/src/zope/app/publication/tests/test_browserpublication.py

-=-
Modified: Zope3/trunk/src/zope/app/debug/debug.py
===================================================================
--- Zope3/trunk/src/zope/app/debug/debug.py	2005-04-11 18:41:07 UTC (rev 29944)
+++ Zope3/trunk/src/zope/app/debug/debug.py	2005-04-11 22:20:41 UTC (rev 29945)
@@ -21,7 +21,7 @@
 from StringIO import StringIO
 from zope.publisher.publish import publish as _publish, debug_call
 from zope.publisher.browser import TestRequest
-from zope.app.publication.browser import BrowserPublication
+from zope.app.publication.browser import BrowserPublication, setDefaultSkin
 from zope.app.appsetup import config, database
 
 class Debugger(object):
@@ -53,7 +53,7 @@
     def _request(self,
                  path='/', stdin='', stdout=None, basic=None,
                  environment = None, form=None,
-                 request=TestRequest, publication=BrowserPublication):
+                 request=None, publication=BrowserPublication):
         """Create a request
         """
 
@@ -82,7 +82,11 @@
 
         pub = publication(self.db)
 
-        request = request(stdin, stdout, env)
+        if request is not None:
+            request = request(stdin, stdout, env)
+        else:
+            request = TestRequest(stdin, stdout, env)
+            setDefaultSkin(request)
         request.setPublication(pub)
         if form:
             # This requires that request class has an attribute 'form'

Modified: Zope3/trunk/src/zope/app/publication/browser.py
===================================================================
--- Zope3/trunk/src/zope/app/publication/browser.py	2005-04-11 18:41:07 UTC (rev 29944)
+++ Zope3/trunk/src/zope/app/publication/browser.py	2005-04-11 22:20:41 UTC (rev 29945)
@@ -20,11 +20,17 @@
 """
 __docformat__ = 'restructuredtext'
 
+from zope.interface import providedBy
+from zope.interface import directlyProvides
+
+from zope.publisher.interfaces.browser import IBrowserPublisher
+from zope.publisher.interfaces.browser import IDefaultSkin
+from zope.publisher.interfaces.browser import IDefaultBrowserLayer
+
 from zope.app import zapi
 from zope.app.publication.publicationtraverse \
      import PublicationTraverser as PublicationTraverser_
 from zope.app.publication.http import BaseHTTPPublication
-from zope.publisher.interfaces.browser import IBrowserPublisher
 from zope.security.checker import ProxyFactory
 
 class PublicationTraverser(PublicationTraverser_):
@@ -73,3 +79,72 @@
 
     def __call__(self):
         return self.__pub
+
+def setDefaultSkin(request):
+    """Sets the default skin for the request.
+
+    The default skin is a marker interface that can be registered as an
+    adapter that provides IDefaultSkin for the request type.
+
+    If a default skin is not available, the default layer
+    (IDefaultBrowserLayer) is used.
+
+    To illustrate, we'll first use setDefaultSkin without a registered
+    IDefaultSkin adapter:
+
+      >>> from zope.publisher.interfaces.browser import IBrowserRequest
+      >>> from zope.interface import implements
+      >>> class Request(object):
+      ...     implements(IBrowserRequest)
+
+      >>> request = Request()
+      >>> IDefaultBrowserLayer.providedBy(request)
+      False
+
+      >>> setDefaultSkin(request)
+      >>> IDefaultBrowserLayer.providedBy(request)
+      True
+
+    When we register a default layer, however:
+
+      >>> from zope.interface import Interface
+      >>> class IMySkin(Interface):
+      ...     pass
+      >>> from zope.app.testing import ztapi
+      >>> ztapi.provideAdapter(IBrowserRequest, IDefaultSkin, IMySkin)
+
+    setDefaultSkin uses the layer instead of IDefaultBrowserLayer.providedBy:
+
+      >>> request = Request()
+      >>> IMySkin.providedBy(request)
+      False
+      >>> IDefaultSkin.providedBy(request)
+      False
+
+      >>> setDefaultSkin(request)
+
+      >>> IMySkin.providedBy(request)
+      True
+      >>> IDefaultBrowserLayer.providedBy(request)
+      False
+
+    Any interfaces that are directly provided by the request coming into this
+    method are replaced by the applied layer/skin interface:
+    
+      >>> request = Request()
+      >>> class IFoo(Interface):
+      ...     pass
+      >>> directlyProvides(request, IFoo)
+      >>> IFoo.providedBy(request)
+      True
+      >>> setDefaultSkin(request)
+      >>> IFoo.providedBy(request)
+      False
+
+    """
+    adapters = zapi.getSiteManager().adapters
+    skin = adapters.lookup((providedBy(request),), IDefaultSkin, '')
+    if skin is not None:
+        directlyProvides(request, skin)
+    else:
+        directlyProvides(request, IDefaultBrowserLayer)

Modified: Zope3/trunk/src/zope/app/publication/httpfactory.py
===================================================================
--- Zope3/trunk/src/zope/app/publication/httpfactory.py	2005-04-11 18:41:07 UTC (rev 29944)
+++ Zope3/trunk/src/zope/app/publication/httpfactory.py	2005-04-11 22:20:41 UTC (rev 29945)
@@ -17,18 +17,16 @@
 """
 __docformat__ = 'restructuredtext'
 
-from zope.interface import implements, providedBy
-from zope.interface import directlyProvides, directlyProvidedBy
+from zope.interface import implements
+
 from zope.publisher.http import HTTPRequest
 from zope.publisher.browser import BrowserRequest
-from zope.publisher.interfaces.browser import IDefaultSkin
-from zope.publisher.interfaces.browser import IDefaultBrowserLayer
 from zope.publisher.xmlrpc import XMLRPCRequest
 
 from zope.app import zapi
 from zope.app.publication.interfaces import IPublicationRequestFactory
 from zope.app.publication.http import HTTPPublication
-from zope.app.publication.browser import BrowserPublication
+from zope.app.publication.browser import BrowserPublication, setDefaultSkin
 from zope.app.publication.xmlrpc import XMLRPCPublication
 from zope.app.publication.soap import SOAPPublication
 from zope.app.publication.interfaces import ISOAPRequestFactory
@@ -65,13 +63,7 @@
             else:
                 request = BrowserRequest(input_stream, output_steam, env)
                 request.setPublication(self._brower)
-                # Set the default skin
-                adapters = zapi.getSiteManager().adapters
-                skin = adapters.lookup((providedBy(request),), IDefaultSkin, '')
-                if skin is not None:
-                    directlyProvides(request, directlyProvidedBy(request)+skin)
-                else:
-                    directlyProvides(request, IDefaultBrowserLayer)
+                setDefaultSkin(request)
         else:
             request = HTTPRequest(input_stream, output_steam, env)
             request.setPublication(self._http)

Modified: Zope3/trunk/src/zope/app/publication/tests/test_browserpublication.py
===================================================================
--- Zope3/trunk/src/zope/app/publication/tests/test_browserpublication.py	2005-04-11 18:41:07 UTC (rev 29944)
+++ Zope3/trunk/src/zope/app/publication/tests/test_browserpublication.py	2005-04-11 22:20:41 UTC (rev 29945)
@@ -16,8 +16,9 @@
 $Id$
 """
 import unittest
+from zope.testing import doctest
 
-from zope.app.testing import ztapi
+from zope.app.testing import placelesssetup, ztapi
 from StringIO import StringIO
 
 from zope.security.interfaces import ForbiddenAttribute
@@ -307,8 +308,15 @@
 def test_suite():
     t2 = unittest.makeSuite(BrowserPublicationTests, 'test')
     t3 = unittest.makeSuite(BrowserDefaultTests, 'test')
-    return unittest.TestSuite((t2, t3))
+    return unittest.TestSuite((
+        t2,
+        t3,
+        doctest.DocTestSuite('zope.app.publication.browser',
+                             setUp=placelesssetup.setUp,
+                             tearDown=placelesssetup.tearDown),
 
+        ))
 
+
 if __name__ == '__main__':
     unittest.TextTestRunner().run(test_suite())



More information about the Zope3-Checkins mailing list