[Zope-Checkins] SVN: Products.Five/branches/1.4/browser/ Issue #2327: Don't require wrapped context / view to render template.

Tres Seaver tseaver at palladion.com
Tue May 22 10:43:38 EDT 2007


Log message for revision 75876:
  Issue #2327:  Don't require wrapped context / view to render template.

Changed:
  U   Products.Five/branches/1.4/browser/pagetemplatefile.py
  U   Products.Five/branches/1.4/browser/tests/test_pages.py

-=-
Modified: Products.Five/branches/1.4/browser/pagetemplatefile.py
===================================================================
--- Products.Five/branches/1.4/browser/pagetemplatefile.py	2007-05-22 14:40:47 UTC (rev 75875)
+++ Products.Five/branches/1.4/browser/pagetemplatefile.py	2007-05-22 14:43:37 UTC (rev 75876)
@@ -17,6 +17,7 @@
 """
 import os, sys
 
+from Acquisition import aq_inner
 from Globals import package_home
 from AccessControl import getSecurityManager
 from Shared.DC.Scripts.Bindings import Unauthorized, UnauthorizedBinding
@@ -65,14 +66,18 @@
         try:
             root = self.getPhysicalRoot()
         except AttributeError:
-            root = self.context.getPhysicalRoot()
+            try:
+                root = self.context.getPhysicalRoot()
+            except AttributeError:
+                root = None
+
         # Even if the context isn't a view (when would that be exaclty?),
         # there shouldn't be any dange in applying a view, because it
         # won't be used.  However assuming that a lack of getPhysicalRoot
         # implies a missing view causes problems.
         view = self._getContext()
 
-        here = self.context.aq_inner
+        here = aq_inner(self.context)
 
         request = getattr(root, 'REQUEST', None)
         c = {'template': self,

Modified: Products.Five/branches/1.4/browser/tests/test_pages.py
===================================================================
--- Products.Five/branches/1.4/browser/tests/test_pages.py	2007-05-22 14:40:47 UTC (rev 75875)
+++ Products.Five/branches/1.4/browser/tests/test_pages.py	2007-05-22 14:43:37 UTC (rev 75876)
@@ -26,8 +26,8 @@
       >>> zcml.load_config("configure.zcml", Products.Five)
       >>> zcml.load_config('pages.zcml', package=Products.Five.browser.tests)
 
-      >>> from Products.Five.tests.testing.simplecontent import manage_addSimpleContent
-      >>> manage_addSimpleContent(self.folder, 'testoid', 'Testoid')
+      >>> from Products.Five.tests.testing import simplecontent as sc
+      >>> sc.manage_addSimpleContent(self.folder, 'testoid', 'Testoid')
       >>> uf = self.folder.acl_users
       >>> uf._doAddUser('manager', 'r00t', ['Manager'], [])
       >>> self.login('manager')
@@ -60,6 +60,54 @@
       >>> tearDown()
     """
 
+def test_view_with_unwrapped_context():
+    """
+    It may be desirable when writing tests for views themselves to
+    provide dummy contexts which are not wrapped.
+
+    >>> import Products.Five.browser.tests
+    >>> from Products.Five import zcml
+    >>> zcml.load_config("configure.zcml", Products.Five)
+    >>> zcml.load_config('pages.zcml', package=Products.Five.browser.tests)
+    >>> from Products.Five.tests.testing import simplecontent as sc
+    >>> from zope.interface import Interface
+    >>> from zope.interface import implements
+    >>> from zope.component import queryMultiAdapter
+    >>> class Unwrapped:
+    ...     implements(sc.ISimpleContent)
+    >>> unwrapped = Unwrapped()
+
+    Simple views should work fine without having their contexts wrapped:
+
+    >>> eagle = queryMultiAdapter((unwrapped, self.app.REQUEST),
+    ...                            Interface, 'eagle.txt')
+    >>> eagle is not None
+    True
+    >>> from Products.Five.browser.tests.pages import SimpleView
+    >>> isinstance(eagle, SimpleView)
+    True
+    >>> eagle()
+    'The eagle has landed'
+
+    We also want to be able to render the file-based ZPT without requiring
+    that the context be wrapped:
+
+    >>> falcon = queryMultiAdapter((unwrapped, self.app.REQUEST),
+    ...                            Interface, 'falcon.html')
+    >>> falcon is not None
+    True
+    >>> from Products.Five.browser.tests.pages import SimpleView
+    >>> isinstance(falcon, SimpleView)
+    True
+    >>> print falcon()
+    <p>The falcon has taken flight</p>
+
+    Clean up:
+
+    >>> from zope.app.testing.placelesssetup import tearDown
+    >>> tearDown()
+    """
+
 def test_suite():
     import unittest
     from Testing.ZopeTestCase import installProduct, ZopeDocTestSuite



More information about the Zope-Checkins mailing list