[Checkins] SVN: zope.globalrequest/trunk/s use a utility in the view to (artificially) create a place where we don't have direct access to the request
Andreas Zeidler
az at zitc.de
Thu Jan 15 09:43:55 EST 2009
Log message for revision 94750:
use a utility in the view to (artificially) create a place where we don't have direct access to the request
Changed:
U zope.globalrequest/trunk/setup.py
U zope.globalrequest/trunk/src/zope/globalrequest/README.txt
U zope.globalrequest/trunk/src/zope/globalrequest/ftests.py
U zope.globalrequest/trunk/src/zope/globalrequest/tests.py
-=-
Modified: zope.globalrequest/trunk/setup.py
===================================================================
--- zope.globalrequest/trunk/setup.py 2009-01-15 14:43:48 UTC (rev 94749)
+++ zope.globalrequest/trunk/setup.py 2009-01-15 14:43:55 UTC (rev 94750)
@@ -37,6 +37,7 @@
extras_require = dict(
test = [
'zope.testing',
+ 'zope.configuration',
'zope.app.publisher',
'zope.app.securitypolicy',
'zope.testbrowser',
Modified: zope.globalrequest/trunk/src/zope/globalrequest/README.txt
===================================================================
--- zope.globalrequest/trunk/src/zope/globalrequest/README.txt 2009-01-15 14:43:48 UTC (rev 94749)
+++ zope.globalrequest/trunk/src/zope/globalrequest/README.txt 2009-01-15 14:43:55 UTC (rev 94750)
@@ -21,3 +21,33 @@
>>> browser.contents
'sif!'
+The view tries to query for a utility and use it to "calculate" it's response,
+so let's define one:
+
+ >>> from zope.interface import implements
+ >>> from zope.globalrequest import ftests
+ >>> class Foo(object):
+ ... implements(ftests.IFoo)
+ ... def foo(self):
+ ... return 'foo!'
+
+Unfortunately the utility class cannot be directly imported from here, i.e.
+relatively, so we have to make it available from somewhere else to register
+the utility:
+
+ >>> ftests.Foo = Foo
+ >>> zcml("""
+ ... <configure xmlns="http://namespaces.zope.org/zope">
+ ... <include package="zope.component" file="meta.zcml" />
+ ... <utility
+ ... factory="zope.globalrequest.ftests.Foo"
+ ... provides="zope.globalrequest.ftests.IFoo" />
+ ... </configure>
+ ... """)
+
+Rendering the view again should now give us the value provided by the utility:
+
+ >>> browser.reload()
+ >>> browser.contents
+ 'foo!'
+
Modified: zope.globalrequest/trunk/src/zope/globalrequest/ftests.py
===================================================================
--- zope.globalrequest/trunk/src/zope/globalrequest/ftests.py 2009-01-15 14:43:48 UTC (rev 94749)
+++ zope.globalrequest/trunk/src/zope/globalrequest/ftests.py 2009-01-15 14:43:55 UTC (rev 94750)
@@ -1,9 +1,22 @@
from zope.publisher.browser import BrowserPage
+from zope.interface import Interface
+from zope.component import queryUtility
+class IFoo(Interface):
+ """ interface for a foo-ish utility """
+
+ def foo():
+ """ return some foo """
+
+
class FooView(BrowserPage):
""" a browser view """
def __call__(self, *args, **kw):
- return 'sif!'
+ foo = queryUtility(IFoo, default=None)
+ if foo is not None:
+ return foo.foo()
+ else:
+ return 'sif!'
Modified: zope.globalrequest/trunk/src/zope/globalrequest/tests.py
===================================================================
--- zope.globalrequest/trunk/src/zope/globalrequest/tests.py 2009-01-15 14:43:48 UTC (rev 94749)
+++ zope.globalrequest/trunk/src/zope/globalrequest/tests.py 2009-01-15 14:43:55 UTC (rev 94750)
@@ -1,10 +1,17 @@
from unittest import TestSuite
from zope.testing import doctest
from zope.testing.cleanup import cleanUp
+from zope.configuration import config
+from zope.configuration import xmlconfig
from zope.app.testing import functional
from os.path import join, abspath, dirname
+def zcml(source):
+ context = config.ConfigurationMachine()
+ xmlconfig.registerCommonDirectives(context)
+ xmlconfig.string(source, context)
+
def tearDown(test):
cleanUp()
@@ -15,7 +22,8 @@
def test_suite():
flags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS
readme = functional.FunctionalDocFileSuite('README.txt',
- package='zope.globalrequest', optionflags=flags, tearDown=tearDown)
+ package='zope.globalrequest', globs={'zcml': zcml},
+ optionflags=flags, tearDown=tearDown)
readme.layer = testLayer
return TestSuite((readme,))
More information about the Checkins
mailing list