[Checkins] SVN: grokcore.view/trunk/ view components can optionally use the grok.provides directive to specify the interface that the component provides instead of the default zope.interface.Interface

Jan-Wijbrand Kolman janwijbrand at gmail.com
Mon Feb 28 13:47:19 EST 2011


Log message for revision 120612:
  view components can optionally use the grok.provides directive to specify the interface that the component provides instead of the default zope.interface.Interface

Changed:
  U   grokcore.view/trunk/CHANGES.txt
  U   grokcore.view/trunk/src/grokcore/view/meta/views.py
  A   grokcore.view/trunk/src/grokcore/view/tests/view/interfacereg.py

-=-
Modified: grokcore.view/trunk/CHANGES.txt
===================================================================
--- grokcore.view/trunk/CHANGES.txt	2011-02-28 17:44:13 UTC (rev 120611)
+++ grokcore.view/trunk/CHANGES.txt	2011-02-28 18:47:19 UTC (rev 120612)
@@ -4,6 +4,10 @@
 2.4 (unreleased)
 ----------------
 
+- grok.View component can optionally use the ``grok.provides`` directive,
+  specifying an interface that the component provides (instead of the
+  zope.interface.Interface that views by default provide).
+
 - Add a new ZCML directive, ``ignoreTemplates`` that let you configure which
   template filename pattern should be ignored by the template registry. The
   pattern attribute of the directive accepts regular expresssion that will be

Modified: grokcore.view/trunk/src/grokcore/view/meta/views.py
===================================================================
--- grokcore.view/trunk/src/grokcore/view/meta/views.py	2011-02-28 17:44:13 UTC (rev 120611)
+++ grokcore.view/trunk/src/grokcore/view/meta/views.py	2011-02-28 18:47:19 UTC (rev 120612)
@@ -82,9 +82,10 @@
     martian.component(components.View)
     martian.directive(grokcore.component.context)
     martian.directive(grokcore.view.layer, default=IDefaultBrowserLayer)
+    martian.directive(grokcore.component.provides, default=interface.Interface)
     martian.directive(grokcore.component.name, get_default=default_view_name)
 
-    def execute(self, factory, config, context, layer, name, **kw):
+    def execute(self, factory, config, context, layer, provides, name, **kw):
         # safety belt: make sure that the programmer didn't use
         # @grok.require on any of the view's methods.
         methods = util.methods_from_class(factory)
@@ -100,9 +101,9 @@
         adapts = (context, layer)
 
         config.action(
-            discriminator=('adapter', adapts, interface.Interface, name),
+            discriminator=('adapter', adapts, provides, name),
             callable=component.provideAdapter,
-            args=(factory, adapts, interface.Interface, name),
+            args=(factory, adapts, provides, name),
             )
         return True
 

Added: grokcore.view/trunk/src/grokcore/view/tests/view/interfacereg.py
===================================================================
--- grokcore.view/trunk/src/grokcore/view/tests/view/interfacereg.py	                        (rev 0)
+++ grokcore.view/trunk/src/grokcore/view/tests/view/interfacereg.py	2011-02-28 18:47:19 UTC (rev 120612)
@@ -0,0 +1,80 @@
+"""
+Views can also provide an interface, in which case they can be looked up (via
+getMultiAdapter) on the interface. This approach is a bit more explicit than
+requiring all views to have a certain name, since this is an interface/contract
+rather than an arbitrary naming standard.
+
+First, do some initialization
+
+  >>> grok.testing.grok(__name__)
+  >>> manfred = Mammoth()
+  >>> from zope.publisher.browser import TestRequest
+  >>> request = TestRequest()
+  >>> from zope import component
+
+If grok.name is used, it needs to be supplied. If not supplied, grok.name
+defaults to the lowercase of the class:
+
+  >>> view = component.getMultiAdapter((manfred, request), name='cavepainting')
+  >>> view()
+  'a chalk cave painting'
+
+It is also possible to look up the same view by also including the interface:
+
+  >>> view = component.getMultiAdapter(
+  ...     (manfred, request), interface=IChalk, name='cavepainting')
+  >>> view()
+  'a chalk cave painting'
+
+The name can be set to '', in which case it is an 'unnamed' view:
+
+  >>> view = component.getMultiAdapter((manfred, request), interface=IRealist)
+  >>> view()
+  'a realist cave painting'
+
+Multipl IPaintStyles can now be looked up by interface, rather than name:
+
+  >>> view = component.getMultiAdapter(
+  ...     (manfred, request), interface=IImpressionist)
+  >>> view()
+  'an impressionist cave painting'
+
+"""
+
+import grokcore.view as grok
+from zope.interface import Interface
+
+class Mammoth(grok.Context):
+    pass
+
+class IPaintStyle(Interface):
+    pass
+
+class IChalk(IPaintStyle):
+    pass
+
+class IImpressionist(IPaintStyle):
+    pass
+
+class IRealist(IPaintStyle):
+    pass
+
+class CavePainting(grok.View):
+    grok.provides(IChalk)
+
+    def render(self):
+        return "a chalk cave painting"
+
+class ImpressionistCavePainting(grok.View):
+    grok.provides(IImpressionist)
+    grok.name('')
+
+    def render(self):
+        return "an impressionist cave painting"
+
+class RealistCavePainting(grok.View):
+    grok.provides(IRealist)
+    grok.name('')
+
+    def render(self):
+        return "a realist cave painting"



More information about the checkins mailing list