Skip to content

Commit

Permalink
use a utility in the view to (artificially) create a place where we d…
Browse files Browse the repository at this point in the history
…on't have direct access to the request
  • Loading branch information
witsch committed Jan 15, 2009
1 parent ba796f5 commit 5c9d0af
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
extras_require = dict(
test = [
'zope.testing',
'zope.configuration',
'zope.app.publisher',
'zope.app.securitypolicy',
'zope.testbrowser',
Expand Down
30 changes: 30 additions & 0 deletions src/zope/globalrequest/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,33 @@ Also make sure our test view works:
>>> 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!'

15 changes: 14 additions & 1 deletion src/zope/globalrequest/ftests.py
Original file line number Diff line number Diff line change
@@ -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!'

10 changes: 9 additions & 1 deletion src/zope/globalrequest/tests.py
Original file line number Diff line number Diff line change
@@ -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()

Expand All @@ -15,7 +22,8 @@ def tearDown(test):
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,))

0 comments on commit 5c9d0af

Please sign in to comment.