[Checkins]
SVN: grok/branches/ulif-viewinfo/src/grok/admin/viewinfo.
Extend the viewinfo functionality.
Martijn Faassen
faassen at infrae.com
Fri Oct 5 09:14:38 EDT 2007
Log message for revision 80641:
Extend the viewinfo functionality.
Changed:
U grok/branches/ulif-viewinfo/src/grok/admin/viewinfo.py
U grok/branches/ulif-viewinfo/src/grok/admin/viewinfo.txt
-=-
Modified: grok/branches/ulif-viewinfo/src/grok/admin/viewinfo.py
===================================================================
--- grok/branches/ulif-viewinfo/src/grok/admin/viewinfo.py 2007-10-05 13:14:26 UTC (rev 80640)
+++ grok/branches/ulif-viewinfo/src/grok/admin/viewinfo.py 2007-10-05 13:14:38 UTC (rev 80641)
@@ -14,31 +14,58 @@
"""
"""
-from zope.interface import Interface, providedBy
+from zope.interface import Interface, providedBy, alsoProvides
from zope import component
from zope.publisher.browser import BrowserRequest
-
+from zope.publisher.interfaces.browser import (IBrowserSkinType,
+ IDefaultBrowserLayer)
+
import grok
class IViewInfo(Interface):
- def getViews():
- """Return a list of views available on the context.
+ def getViews(layer=None):
+ """Get the views for context object.
+
+ Optional layer argument retrieves views registered for this layer.
+
+ Returns iterator (view name, view factory) tuples.
"""
+ def getAllViews():
+ """Get all views for context objects, for any layer that is in a skin.
+
+ Returns iterator of (skin name, (skin) layer, view name,
+ view factory) tuples.
+
+ The default layer will be returned with u'' as the skin name.
+ """
+
class ViewInfo(grok.Adapter):
"""Determine views for contexts.
"""
grok.provides(IViewInfo)
grok.context(Interface)
- def getViews(self):
- """Return a list of tuples with a view name and a view
- factory.
- """
+ def getViews(self, layer=None):
request = BrowserRequest(None, {})
+ if layer is not None:
+ alsoProvides(request, layer)
sm = component.getSiteManager()
- return sm.adapters.lookupAll(map(providedBy, (self.context, request)),
- Interface)
+ return sm.adapters.lookupAll(
+ map(providedBy, (self.context, request)),
+ Interface)
+ def getAllViews(self):
+ for skin_name, layer in getSkins():
+ for view_name, factory in self.getViews(layer):
+ yield skin_name, layer, view_name, factory
+ for view_name, factory in self.getViews(IDefaultBrowserLayer):
+ yield u'', IDefaultBrowserLayer, view_name, factory
+
+def getSkins():
+ """Get all the skins registered in the system.
+ """
+ return component.getUtilitiesFor(IBrowserSkinType)
+
Modified: grok/branches/ulif-viewinfo/src/grok/admin/viewinfo.txt
===================================================================
--- grok/branches/ulif-viewinfo/src/grok/admin/viewinfo.txt 2007-10-05 13:14:26 UTC (rev 80640)
+++ grok/branches/ulif-viewinfo/src/grok/admin/viewinfo.txt 2007-10-05 13:14:38 UTC (rev 80641)
@@ -26,7 +26,7 @@
>>> manfred = Mammoth()
-Using ViewInfo we can ask for all the views for our particular
+Using ``ViewInfo`` we can ask for all the views for our particular
mammoth.
>>> from grok.admin.viewinfo import ViewInfo
@@ -35,7 +35,7 @@
[(u'index', <class 'MammothIndex'>)]
Let's create another view `simple` and register it for the base
-interface (Interface):
+interface (``Interface``):
>>> class SimpleView(grok.View):
... pass
@@ -44,27 +44,99 @@
... IBrowserRequest),
... provides=Interface, name=u'simple')
-When we call getViews() again, it should return both views, since any
-object provides Interface (so our Mammoth instance should too):
+When we call ``getViews()`` again, it should return both views, since
+any object provides ``Interface`` (so our ``Mammoth`` instance should
+too):
>>> sorted(list(info.getViews()))
[(u'index', <class 'MammothIndex'>), (u'simple', <class 'SimpleView'>)]
Because we really want to get all views for an object, we also want
-views defined in other layers and skins, not only the default one. We
-define a layer:
+views defined in other layers, not only the default one. We define a
+layer:
>>> from zope.publisher.interfaces.browser import (IBrowserPage,
... IDefaultBrowserLayer)
- >>> class IMammothSkin(IDefaultBrowserLayer):
- ... """Skin for a mammoth."""
+ >>> class ITestLayer(IDefaultBrowserLayer):
+ ... pass
-and register it:
+and register SimpleView again for this layer::
>>> component.provideAdapter(SimpleView,
- ... adapts=(Mammoth, IMammothSkin),
+ ... adapts=(Mammoth, ITestLayer),
... provides=IBrowserPage,
- ... name=u'index.html')
+ ... name=u'simple2')
+
+When we check the views, it won't show up::
+
>>> sorted(list(info.getViews()))
+ [(u'index', <class 'MammothIndex'>), (u'simple', <class 'SimpleView'>)]
+Instead, we need to list the views for this layer in particular. Since
+we already have some views registered on the request, we get those as
+well, as these are registered for all layers::
+ >>> sorted(list(info.getViews(ITestLayer)))
+ [(u'index', <class 'MammothIndex'>),
+ (u'simple', <class 'SimpleView'>),
+ (u'simple2', <class 'SimpleView'>)]
+
+In order to get *all* views registered for an object, we first need to
+know which layers exist in the system. In order to get all the layers
+that are in use, we retrieve all registered skins.
+
+When we don't have any skins registered yet, we don't find any::
+
+ >>> from grok.admin.viewinfo import getSkins
+ >>> sorted(list(getSkins()))
+ []
+
+Let's create and grok a skin now::
+
+ >>> class OurSkin(grok.Skin):
+ ... grok.name('ourskin')
+ ... grok.layer(ITestLayer)
+
+To grok the skin, we first need to grok the skin grokker itself::
+
+ >>> from grok.meta import SkinGrokker
+ >>> grok.grok_component('SkinGrokker', SkinGrokker)
+ True
+
+Now we can actually grok the skin::
+
+ >>> grok.grok_component('OurSkin', OurSkin)
+ True
+
+We should now see this skin::
+
+ >>> sorted(list(getSkins()))
+ [(u'ourskin', <InterfaceClass __builtin__.ITestLayer>)]
+
+We have a method ``getAllViews`` which gives the views registered on
+all skins for a particular context object (with possible
+duplicates). Besides those views defined on skins, we also return
+those views defined on IDefaultBrowserLayer::
+
+ >>> from pprint import pprint
+ >>> pprint(sorted(list(info.getAllViews())))
+ [(u'',
+ <InterfaceClass zope.publisher.interfaces.browser.IDefaultBrowserLayer>,
+ u'index',
+ <class 'MammothIndex'>),
+ (u'',
+ <InterfaceClass zope.publisher.interfaces.browser.IDefaultBrowserLayer>,
+ u'simple',
+ <class 'SimpleView'>),
+ (u'ourskin',
+ <InterfaceClass __builtin__.ITestLayer>,
+ u'index',
+ <class 'MammothIndex'>),
+ (u'ourskin',
+ <InterfaceClass __builtin__.ITestLayer>,
+ u'simple',
+ <class 'SimpleView'>),
+ (u'ourskin',
+ <InterfaceClass __builtin__.ITestLayer>,
+ u'simple2',
+ <class 'SimpleView'>)]
More information about the Checkins
mailing list