[Checkins]
SVN: Sandbox/darrylcousins/mars.contentprovider/src/mars/contentprovider/
Working up tests
Darryl Cousins
darryl at darrylcousins.net.nz
Tue Jul 17 07:10:49 EDT 2007
Log message for revision 78060:
Working up tests
Changed:
U Sandbox/darrylcousins/mars.contentprovider/src/mars/contentprovider/contentprovider.txt
U Sandbox/darrylcousins/mars.contentprovider/src/mars/contentprovider/directive.txt
-=-
Modified: Sandbox/darrylcousins/mars.contentprovider/src/mars/contentprovider/contentprovider.txt
===================================================================
--- Sandbox/darrylcousins/mars.contentprovider/src/mars/contentprovider/contentprovider.txt 2007-07-17 11:09:06 UTC (rev 78059)
+++ Sandbox/darrylcousins/mars.contentprovider/src/mars/contentprovider/contentprovider.txt 2007-07-17 11:10:48 UTC (rev 78060)
@@ -8,14 +8,14 @@
For more detailed information about Zope 3 content providers please refer to the
zope.contentprovider package. (A snippet of the README follows).
-The Zope 3 concept of content providers is that they are multi-adapters that are
-looked up by the context, request (and thus the layer/skin), and view they are
-displayed in.
+ The Zope 3 concept of content providers is that they are multi-adapters that are
+ looked up by the context, request (and thus the layer/skin), and view they are
+ displayed in.
-The second important concept of content providers are their two-phase
-rendering design. In the first phase the state of the content provider is
-prepared and, if applicable, any data, the provider is responsible for, is
-updated.
+ The second important concept of content providers are their two-phase
+ rendering design. In the first phase the state of the content provider is
+ prepared and, if applicable, any data, the provider is responsible for, is
+ updated.
Set up
------
@@ -26,10 +26,26 @@
>>> import zope.component
>>> import grok
>>> import mars.contentprovider
+ >>> import mars.view
+ >>> import mars.template
>>> import os, tempfile
>>> temp_dir = tempfile.mkdtemp()
-Define the content provider, note that none of the possible directives have been
+We need also to set up a fake module_ info class to pass to the content provider
+grokker.
+
+ >>> from martian.interfaces import IModuleInfo
+ >>> class ModuleInfo(object):
+ ... zope.interface.implements(IModuleInfo)
+ ... path = ''
+ ... package_dotted_name = ''
+ ... def getAnnotation(self, name, default):
+ ... return default
+
+Define the content provider
+---------------------------
+
+Note that none of the possible directives described in directive.txt have been
used. The default name that the provider is registered as is
factory.__name__.lower(), in this case: 'title'.
@@ -38,15 +54,6 @@
... def render(self):
... return self.context.title
-We need also to set up a fake module info class to pass to the content provider
-grokker.
-
- >>> from martian.interfaces import IModuleInfo
- >>> class ModuleInfo(object):
- ... zope.interface.implements(IModuleInfo)
- ... def getAnnotation(self, name, default):
- ... return default
-
In the test we manually ``grok`` the factory, normally this happens when a
module is ``grokked`` on start up.
@@ -79,14 +86,16 @@
Provider lookup
---------------
-Content providers are looked up as named multiadapters to a context, request and
-a view. This is usually done through the use of the tales expression
-``provider`` from within a page template. Other mars packages could be used
-here: mars.layer, mars.template and mars.view but we will use the `vanilla` zope
-3 architecture.
+Content providers are looked up as named multiadapters to a ``context``,
+``request`` and a ``view``. This is usually done through the use of the tales
+expression ``provider`` from within a page template. This could be done with the
+`vanilla` zope 3 architecture but here we'll use other mars package for creating
+an registering view.
- >>> templateFileName = os.path.join(temp_dir, 'template.pt')
- >>> open(templateFileName, 'w').write('''
+First we need a template file for the view.
+
+ >>> template= os.path.join(temp_dir, 'template.pt')
+ >>> open(template, 'w').write('''
... <html>
... <body>
... <h1 tal:content="provider:title">Title</h1>
@@ -97,41 +106,52 @@
As you can see, we expect the ``provider`` expression to simply look up the
content provider and insert the HTML content at this place.
-Next we register the template as a view (browser page) for all objects:
+Next we create the view and its template.
- >>> from zope.app.pagetemplate.simpleviewclass import SimpleViewClass
- >>> FrontPage = SimpleViewClass(templateFileName, name='main.html')
+ >>> class View(mars.view.LayoutView):
+ ... grok.name('main.html')
+ ... grok.context(zope.interface.Interface)
- >>> from zope.publisher.interfaces.browser import IDefaultBrowserLayer
- >>> zope.component.provideAdapter(
- ... FrontPage,
- ... (zope.interface.Interface, IDefaultBrowserLayer),
- ... zope.interface.Interface,
- ... name='main.html')
+ >>> class ViewTemplate(mars.template.LayoutFactory):
+ ... grok.template(template)
+ ... grok.context(View)
-Finally we look up the view and render it:
+As before we need to manually ``grok`` the classes.
- >>> from zope.publisher.browser import TestRequest
- >>> request = TestRequest()
+ >>> from mars.view.meta import LayoutViewGrokker
+ >>> LayoutViewGrokker().grok('', View, None, ModuleInfo(), None)
+ True
+ >>> from mars.template.meta import LayoutFactoryGrokker
+ >>> LayoutFactoryGrokker().grok('', ViewTemplate, None, ModuleInfo(), None)
+ True
+
+Now we can look up the view.
+
>>> view = zope.component.getMultiAdapter((content, request),
... name='main.html')
- >>> print view().strip()
- <html>
- <body>
- <h1>My Title</h1>
- </body>
- </html>
-With the view we could also directly look up the content provider as a multi
+With the view we can directly look up the content provider as a multi
adapter:
>>> from zope.contentprovider.interfaces import IContentProvider
- >>> title= zope.component.queryMultiAdapter((content, request, view),
+ >>> title = zope.component.queryMultiAdapter((content, request, view),
... IContentProvider, name='title')
>>> print title.render()
My Title
+Finally we call the view.
+
+ >>> from zope.publisher.browser import TestRequest
+ >>> request = TestRequest()
+
+ >>> print view().strip()
+ <html>
+ <body>
+ <h1>My Title</h1>
+ </body>
+ </html>
+
The other directives
--------------------
Modified: Sandbox/darrylcousins/mars.contentprovider/src/mars/contentprovider/directive.txt
===================================================================
--- Sandbox/darrylcousins/mars.contentprovider/src/mars/contentprovider/directive.txt 2007-07-17 11:09:06 UTC (rev 78059)
+++ Sandbox/darrylcousins/mars.contentprovider/src/mars/contentprovider/directive.txt 2007-07-17 11:10:48 UTC (rev 78060)
@@ -13,7 +13,8 @@
------------------------
* grok.name(name):
- Name of the view, available in url as object/@@viewname.
+ Name of the view, available in tal as <tal:block replace="structure provider:
+ name" />
Default: factory.__name__.lower()
* grok.context(class_or_interface):
More information about the Checkins
mailing list