[Zope3-checkins] SVN: Zope3/branches/ZopeX3-3.0/src/zope/component/ Added an api for unegistering views and resources.

Jim Fulton jim at zope.com
Wed Aug 25 15:29:34 EDT 2004


Log message for revision 27269:
  Added an api for unegistering views and resources.
  


Changed:
  U   Zope3/branches/ZopeX3-3.0/src/zope/component/presentation.py
  U   Zope3/branches/ZopeX3-3.0/src/zope/component/tests/test_presentation.py


-=-
Modified: Zope3/branches/ZopeX3-3.0/src/zope/component/presentation.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/component/presentation.py	2004-08-25 19:29:16 UTC (rev 27268)
+++ Zope3/branches/ZopeX3-3.0/src/zope/component/presentation.py	2004-08-25 19:29:34 UTC (rev 27269)
@@ -374,6 +374,34 @@
             ] = PresentationRegistration(layer, ifaces, providing, name,
                                          factory, info)
 
+    def unprovideAdapter(self, request_type, factory, name=u'', contexts=(), 
+                       providing=zope.interface.Interface, layer='default',
+                       info=''):
+        """Provide a presentation adapter
+
+        This is a fairly low-level interface that supports both
+        resources and views.
+
+        """
+
+        ifaces = []
+        for context in contexts:
+            if not IInterface.providedBy(context) and context is not None:
+                if not isinstance(context, (type, ClassType)):
+                    raise TypeError(context, IInterface)
+                context = zope.interface.implementedBy(context)
+
+            ifaces.append(context)
+
+        ifaces.append(request_type)
+        ifaces = tuple(ifaces)
+
+        reg = self._layers[layer]
+
+        reg.unregister(ifaces, providing, name, factory)
+
+        del self._registrations[(layer, ifaces, providing, name)]
+
     def queryResource(self, name, request, default=None,
                       providing=zope.interface.Interface):
         """Look up a named resource for a given request

Modified: Zope3/branches/ZopeX3-3.0/src/zope/component/tests/test_presentation.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/component/tests/test_presentation.py	2004-08-25 19:29:16 UTC (rev 27268)
+++ Zope3/branches/ZopeX3-3.0/src/zope/component/tests/test_presentation.py	2004-08-25 19:29:34 UTC (rev 27269)
@@ -16,7 +16,7 @@
 $Id$
 """
 import unittest
-from doctest import DocTestSuite
+from zope.testing.doctest import DocTestSuite
 from zope.component.presentation import GlobalPresentationService
 import zope.interface
 
@@ -158,10 +158,55 @@
     >>> s.provideView(IContact, 'index.html', IRequest, MyView, layer='custom')
 
     >>> c = Contact()
+
+    >>> s.queryView(c, 'index.html', request)
+
     >>> request.skin = 'custom'
 
+    >>> v = s.queryView(c, 'index.html', request)
+
+    >>> v.__class__.__name__
+    'MyView'
+    >>> v.request is request
+    True
+    >>> v.context is c
+    True
+    """
+
+def test_provideAdapter():
+    """
+    provideAdapter provides a basic interface for adding views.  It is
+    similar to the adapter-service provideAdapter, except that it
+    allows a layer to be specified:
+
+    >>> s = GlobalPresentationService()
+    >>> request = Request()
+    >>> s.provideAdapter(IRequest, MyView, 'foo', [IContact])
+
+    >>> c = Contact()
     >>> v = s.queryView(c, 'foo', request)
+    >>> v.__class__.__name__
+    'MyView'
+    >>> v.request is request
+    True
+    >>> v.context is c
+    True
 
+    We can specify a layer and we can provide a view factory directly:
+
+    >>> s.defineLayer('custom')
+    >>> s.defineSkin('custom', ['custom', 'default'])
+    >>> s.provideAdapter(IRequest, MyView, 'index.html', [IContact],
+    ...                  layer='custom')
+
+    >>> c = Contact()
+
+    >>> s.queryView(c, 'index.html', request)
+
+    >>> request.skin = 'custom'
+
+    >>> v = s.queryView(c, 'index.html', request)
+
     >>> v.__class__.__name__
     'MyView'
     >>> v.request is request
@@ -170,7 +215,45 @@
     True
     """
 
+def test_unprovideAdapter():
+    """
+    unprovideAdapter allows adapters to be unregistered
 
+    >>> s = GlobalPresentationService()
+    >>> request = Request()
+    >>> s.provideAdapter(IRequest, MyView, 'foo', [IContact])
+
+    >>> c = Contact()
+    >>> v = s.queryView(c, 'foo', request)
+    >>> v.__class__.__name__
+    'MyView'
+
+    >>> s.unprovideAdapter(IRequest, MyView, 'foo', [IContact])
+    >>> s.queryView(c, 'foo', request)
+
+    We can specify a layer and we can provide a view factory directly:
+
+    >>> s.defineLayer('custom')
+    >>> s.defineSkin('custom', ['custom', 'default'])
+    >>> s.provideAdapter(IRequest, MyView, 'index.html', [IContact],
+    ...               layer='custom')
+
+    >>> c = Contact()
+
+    >>> request.skin = 'custom'
+
+    >>> v = s.queryView(c, 'index.html', request)
+
+    >>> v.__class__.__name__
+    'MyView'
+
+    >>> s.unprovideAdapter(IRequest, MyView, 'index.html', [IContact],
+    ...                 layer='custom')
+    >>> s.queryView(c, 'index.html', request)
+
+
+    """
+
 def test_default_view_names():
     """
     >>> s = GlobalPresentationService()



More information about the Zope3-Checkins mailing list