[Checkins] SVN: grok/branches/ulif-viewinfo/src/grok/admin/ Added first viewinfo stuff.

Uli Fouquet uli at gnufix.de
Fri Oct 5 05:42:49 EDT 2007


Log message for revision 80627:
  Added first viewinfo stuff.

Changed:
  U   grok/branches/ulif-viewinfo/src/grok/admin/tests/test_grokadmin.py
  A   grok/branches/ulif-viewinfo/src/grok/admin/viewinfo.py
  A   grok/branches/ulif-viewinfo/src/grok/admin/viewinfo.txt

-=-
Modified: grok/branches/ulif-viewinfo/src/grok/admin/tests/test_grokadmin.py
===================================================================
--- grok/branches/ulif-viewinfo/src/grok/admin/tests/test_grokadmin.py	2007-10-05 09:36:03 UTC (rev 80626)
+++ grok/branches/ulif-viewinfo/src/grok/admin/tests/test_grokadmin.py	2007-10-05 09:42:48 UTC (rev 80627)
@@ -60,7 +60,8 @@
 
     for name in []:
         suite.addTest(suiteFromPackage(name))
-    for name in ['docgrok.txt', 'docgrok.py', 'objectinfo.txt', 'utilities.py']:
+    for name in ['docgrok.txt', 'docgrok.py', 'objectinfo.txt',
+                 'utilities.py', 'viewinfo.py']:
         suite.addTest(doctest.DocFileSuite(name,
                                            package='grok.admin',
                                            globs=globs,

Added: grok/branches/ulif-viewinfo/src/grok/admin/viewinfo.py
===================================================================
--- grok/branches/ulif-viewinfo/src/grok/admin/viewinfo.py	                        (rev 0)
+++ grok/branches/ulif-viewinfo/src/grok/admin/viewinfo.py	2007-10-05 09:42:48 UTC (rev 80627)
@@ -0,0 +1,44 @@
+##############################################################################
+#
+# Copyright (c) 2007 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+"""
+
+from zope.interface import Interface, providedBy
+from zope import component
+from zope.publisher.browser import BrowserRequest
+
+import grok
+
+class IViewInfo(Interface):
+
+    def getViews():
+        """Return a list of views available on the context.
+        """
+
+
+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.
+        """
+        request = BrowserRequest(None, {})
+        sm = component.getSiteManager()
+        return sm.adapters.lookupAll(map(providedBy, (self.context, request)),
+                              Interface)
+        

Added: grok/branches/ulif-viewinfo/src/grok/admin/viewinfo.txt
===================================================================
--- grok/branches/ulif-viewinfo/src/grok/admin/viewinfo.txt	                        (rev 0)
+++ grok/branches/ulif-viewinfo/src/grok/admin/viewinfo.txt	2007-10-05 09:42:48 UTC (rev 80627)
@@ -0,0 +1,70 @@
+=====================================
+Determining views for context objects
+=====================================
+
+Let's start with a simple object:
+   
+   >>> class Mammoth(object):
+   ...     pass
+
+We define a view:
+
+   >>> import grok
+   >>> class MammothIndex(grok.View):
+   ...     pass
+
+We provide this view directly for our mammoth.
+
+   >>> from zope import component
+   >>> from zope.interface import Interface
+   >>> from zope.publisher.interfaces.browser import IBrowserRequest
+   >>> component.provideAdapter(MammothIndex, adapts=(Mammoth, IBrowserRequest),
+   ...                          provides=Interface, name=u'index')
+
+
+We create an instance of the mammoth:
+
+   >>> manfred = Mammoth()
+
+Using ViewInfo we can ask for all the views for our particular
+mammoth.
+
+   >>> from grok.admin.viewinfo import ViewInfo
+   >>> info = ViewInfo(manfred)
+   >>> list(info.getViews())
+   [(u'index', <class 'MammothIndex'>)]
+
+Let's create another view `simple` and register it for the base
+interface (Interface):
+
+   >>> class SimpleView(grok.View):
+   ...     pass
+
+   >>> component.provideAdapter(SimpleView, adapts=(Interface, 
+   ...                                              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):
+
+   >>> 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:
+
+   >>> from zope.publisher.interfaces.browser import (IBrowserPage,
+   ...                                                IDefaultBrowserLayer)
+   >>> class IMammothSkin(IDefaultBrowserLayer):
+   ...     """Skin for a mammoth."""
+
+and register it:
+
+   >>> component.provideAdapter(SimpleView,
+   ...                          adapts=(Mammoth, IMammothSkin),
+   ...                          provides=IBrowserPage,
+   ...                          name=u'index.html')
+   >>> sorted(list(info.getViews()))
+
+



More information about the Checkins mailing list