[Zope3-checkins] SVN: Zope3/branches/roger-contentprovider/src/zope/viewlet/ Got written tests running.

Stephan Richter srichter at cosmos.phy.tufts.edu
Sun Oct 9 04:10:57 EDT 2005


Log message for revision 38985:
  Got written tests running.
  

Changed:
  U   Zope3/branches/roger-contentprovider/src/zope/viewlet/README.txt
  U   Zope3/branches/roger-contentprovider/src/zope/viewlet/interfaces.py
  U   Zope3/branches/roger-contentprovider/src/zope/viewlet/manager.py
  U   Zope3/branches/roger-contentprovider/src/zope/viewlet/tests.py

-=-
Modified: Zope3/branches/roger-contentprovider/src/zope/viewlet/README.txt
===================================================================
--- Zope3/branches/roger-contentprovider/src/zope/viewlet/README.txt	2005-10-09 07:58:49 UTC (rev 38984)
+++ Zope3/branches/roger-contentprovider/src/zope/viewlet/README.txt	2005-10-09 08:10:57 UTC (rev 38985)
@@ -23,8 +23,27 @@
 You can then create a viewlet manager for this viewlet type:
 
   >>> from zope.viewlet import manager
-  >>> leftColumn = manager.ViewletManager(ILeftColumnViewlet)
+  >>> LeftColumn = manager.ViewletManager(ILeftColumnViewlet)
 
+Now we have to instantiate it:
+
+  >>> import zope.interface
+  >>> class Content(object):
+  ...     zope.interface.implements(zope.interface.Interface)
+  >>> content = Content()
+
+  >>> from zope.publisher.browser import TestRequest
+  >>> request = TestRequest()
+
+  >>> from zope.app.publisher.interfaces.browser import IBrowserView
+  >>> class View(object):
+  ...     zope.interface.implements(IBrowserView)
+  ...     def __init__(self, context, request):
+  ...         pass
+  >>> view = View(content, request)
+
+  >>> leftColumn = LeftColumn(content, request, view)
+
 So initially nothing gets rendered:
 
   >>> leftColumn()
@@ -34,9 +53,9 @@
 
   >>> import zope.component
   >>> from zope.publisher.interfaces.browser import IDefaultBrowserLayer
-  >>> from zope.app.publisher.interfaces.browser import IBrowserView
 
-  >>> class WeatherBox(ILeftColumnViewlet):
+  >>> class WeatherBox(object):
+  ...     zope.interface.implements(ILeftColumnViewlet)
   ...
   ...     def __init__(self, context, request, view):
   ...         pass
@@ -44,12 +63,17 @@
   ...     def __call__(self):
   ...         return u'<div class="box">It is sunny today!</div>'
 
+  >>> from zope.security.checker import NamesChecker, defineChecker
+  >>> viewletChecker = NamesChecker(('__call__', 'weight'))
+  >>> defineChecker(WeatherBox, viewletChecker)
+
   >>> zope.component.provideAdapter(
   ...     WeatherBox,
   ...     (zope.interface.Interface, IDefaultBrowserLayer, IBrowserView),
   ...     ILeftColumnViewlet, name='weather')
 
-  >>> class SportBox(ILeftColumnViewlet):
+  >>> class SportBox(object):
+  ...     zope.interface.implements(ILeftColumnViewlet)
   ...
   ...     def __init__(self, context, request, view):
   ...         pass
@@ -57,6 +81,8 @@
   ...     def __call__(self):
   ...         return u'<div class="box">Patriots (23) : Steelers (7)</div>'
 
+  >>> defineChecker(SportBox, viewletChecker)
+
   >>> zope.component.provideAdapter(
   ...     SportBox,
   ...     (zope.interface.Interface, IDefaultBrowserLayer, IBrowserView),
@@ -64,7 +90,9 @@
 
 and thus the left column is filled:
 
-  >>> leftColumn()
+  >>> print leftColumn()
+  <div class="box">Patriots (23) : Steelers (7)</div>
+  <div class="box">It is sunny today!</div>
 
 But this is of course pretty lame, since there is no way of specifying how the
 viewlets are put together. But we have a solution. The second argument of the
@@ -76,17 +104,22 @@
   >>> leftColTemplate = os.path.join(temp_dir, 'leftCol.pt')
   >>> open(leftColTemplate, 'w').write('''
   ... <div class="left-column">
-  ...   <tal:block repeat="viewlet viewlets"
+  ...   <tal:block repeat="viewlet options/viewlets"
   ...              replace="structure viewlet" />
   ... </div>
   ... ''')
 
-  >>> leftColumn = manager.ViewletManager(ILeftColumnViewlet, leftColTemplate)
+  >>> LeftColumn = manager.ViewletManager(ILeftColumnViewlet, leftColTemplate)
+  >>> leftColumn = LeftColumn(content, request, view)
 
 As you can see, the viewlet manager provides a global ``viewlets`` variable
 that is an iterable of all the avialable viewlets in the correct order:
 
-  >>> leftColumn()
+  >>> print leftColumn().strip()
+  <div class="left-column">
+    <div class="box">Patriots (23) : Steelers (7)</div>
+    <div class="box">It is sunny today!</div>
+  </div>
 
 You can also lookup the viewlets directly for management purposes:
 
@@ -98,6 +131,9 @@
 If the viewlet is not found, then the expected behavior is provided:
 
   >>> leftColumn['stock']
+  Traceback (most recent call last):
+  ...
+  ComponentLookupError: 'No provider with name `stock` found.'
 
   >>> leftColumn.get('stock') is None
   True

Modified: Zope3/branches/roger-contentprovider/src/zope/viewlet/interfaces.py
===================================================================
--- Zope3/branches/roger-contentprovider/src/zope/viewlet/interfaces.py	2005-10-09 07:58:49 UTC (rev 38984)
+++ Zope3/branches/roger-contentprovider/src/zope/viewlet/interfaces.py	2005-10-09 08:10:57 UTC (rev 38985)
@@ -30,7 +30,8 @@
     """
 
 
-class IViewletManager(IContentProvider, IReadMapping):
+class IViewletManager(IContentProvider,
+                      zope.interface.common.mapping.IReadMapping):
     """An object that provides access to the content providers.
 
     The viewlet manager's resposibilities are:

Modified: Zope3/branches/roger-contentprovider/src/zope/viewlet/manager.py
===================================================================
--- Zope3/branches/roger-contentprovider/src/zope/viewlet/manager.py	2005-10-09 07:58:49 UTC (rev 38984)
+++ Zope3/branches/roger-contentprovider/src/zope/viewlet/manager.py	2005-10-09 08:10:57 UTC (rev 38985)
@@ -20,8 +20,9 @@
 import zope.component
 import zope.interface
 import zope.security
+from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile
 
-from zope.contentprovider import interfaces
+from zope.viewlet import interfaces
 
 
 class ViewletManagerBase(object):
@@ -43,7 +44,8 @@
         """See zope.interface.common.mapping.IReadMapping"""
         # Find the content provider
         provider = zope.component.queryMultiAdapter(
-            (self.context, self.request, self.view), self.region, name=name)
+            (self.context, self.request, self.view), self.providerType,
+            name=name)
 
         # If the content provider was not found, then raise a lookup error
         if provider is None:
@@ -69,12 +71,12 @@
             return default
 
 
-    def __call__(self, *args, **kw)
+    def __call__(self, *args, **kw):
         """See zope.contentprovider.interfaces.IContentProvider"""
 
         # Find all content providers for the region
         viewlets = zope.component.getAdapters(
-            (self.context, self.request, self.view), self.viewType)
+            (self.context, self.request, self.view), self.providerType)
 
         # Sort out all content providers that cannot be accessed by the
         # principal
@@ -82,7 +84,7 @@
                     if zope.security.canAccess(viewlet, '__call__')]
 
         # Sort the content providers by weight.
-        if interfaces.IWeightSupport in self.viewletType.flattened():
+        if self.providerType.extends(interfaces.IWeightSupport):
             viewlets.sort(lambda x, y: cmp(x.weight, y.weight))
         else:
             viewlets.sort()
@@ -91,11 +93,14 @@
         if self.template:
             return self.template(viewlets=viewlets)
         else:
-            return u'\n'.join(viewlets)
+            return u'\n'.join([viewlet() for viewlet in viewlets])
 
 
-def ViewletManager(type, template=None):
+def ViewletManager(providerType, template=None):
 
-    return type('<ViewletManager for %s>' %type.getName(),
+    if template is not None:
+        template = ViewPageTemplateFile(template)
+
+    return type('<ViewletManager for %s>' %providerType.getName(),
                 (ViewletManagerBase,),
-                {'providerType': type, 'template': None})
+                {'providerType': providerType, 'template': template})

Modified: Zope3/branches/roger-contentprovider/src/zope/viewlet/tests.py
===================================================================
--- Zope3/branches/roger-contentprovider/src/zope/viewlet/tests.py	2005-10-09 07:58:49 UTC (rev 38984)
+++ Zope3/branches/roger-contentprovider/src/zope/viewlet/tests.py	2005-10-09 08:10:57 UTC (rev 38985)
@@ -24,7 +24,6 @@
 from zope.testing.doctestunit import DocTestSuite, DocFileSuite
 from zope.app.testing import setup
 
-from zope.contentprovider.interfaces import IRegion
 from zope.viewlet import interfaces
 
 



More information about the Zope3-Checkins mailing list