[Checkins] SVN: grokcore.view/branches/1.12/ view components can optionally use the grok.provides directive to specify the interface that the component provides instead of the default zope.interface.Interface. Backport from trunk.
Jan-Wijbrand Kolman
janwijbrand at gmail.com
Tue Mar 1 04:11:05 EST 2011
Log message for revision 120621:
view components can optionally use the grok.provides directive to specify the interface that the component provides instead of the default zope.interface.Interface. Backport from trunk.
Changed:
U grokcore.view/branches/1.12/CHANGES.txt
U grokcore.view/branches/1.12/src/grokcore/view/meta/views.py
A grokcore.view/branches/1.12/src/grokcore/view/tests/view/interfacereg.py
-=-
Modified: grokcore.view/branches/1.12/CHANGES.txt
===================================================================
--- grokcore.view/branches/1.12/CHANGES.txt 2011-03-01 08:58:26 UTC (rev 120620)
+++ grokcore.view/branches/1.12/CHANGES.txt 2011-03-01 09:11:05 UTC (rev 120621)
@@ -6,6 +6,9 @@
- Make package comply to zope.org repository policy.
+- 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).
1.12.2 (2009-09-17)
-------------------
Modified: grokcore.view/branches/1.12/src/grokcore/view/meta/views.py
===================================================================
--- grokcore.view/branches/1.12/src/grokcore/view/meta/views.py 2011-03-01 08:58:26 UTC (rev 120620)
+++ grokcore.view/branches/1.12/src/grokcore/view/meta/views.py 2011-03-01 09:11:05 UTC (rev 120621)
@@ -37,6 +37,7 @@
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 grok(self, name, factory, module_info, **kw):
@@ -45,7 +46,7 @@
factory.module_info = module_info
return super(ViewGrokker, self).grok(name, factory, module_info, **kw)
- def execute(self, factory, config, context, layer, name, **kw):
+ def execute(self, factory, config, context, layer, provides, name, **kw):
# find templates
templates = factory.module_info.getAnnotation('grok.templates', None)
if templates is not None:
@@ -70,9 +71,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
@@ -85,7 +86,7 @@
def has_no_render(factory):
return not has_render(factory)
-
+
templates.checkTemplates(module_info, factory, 'view',
has_render, has_no_render)
Copied: grokcore.view/branches/1.12/src/grokcore/view/tests/view/interfacereg.py (from rev 120612, grokcore.view/trunk/src/grokcore/view/tests/view/interfacereg.py)
===================================================================
--- grokcore.view/branches/1.12/src/grokcore/view/tests/view/interfacereg.py (rev 0)
+++ grokcore.view/branches/1.12/src/grokcore/view/tests/view/interfacereg.py 2011-03-01 09:11:05 UTC (rev 120621)
@@ -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