skip to navigation
skip to content

zope.globalrequest 1.3

Global way of retrieving the currently active request.


This package provides a global way to retrieve the currently active request object in a zope-based web framework. To do so you simply need to do the following:

from zope.globalrequest import getRequest
request = getRequest()

This package is mainly intended to be used with the Zope2/Plone stack. While it also works with the Zope3 framework, the latter promotes a clean separation of concerns and the pattern of having a globally available request object is discouraged.

Functional Tests

The remainder of this file contains functional tests to demonstrate that the package works as intended.

First we need to define a browser view along with an interface for a utility that will be used by that view:

>>> from zope.interface import Interface
>>> class IFoo(Interface):
...     """ interface for a foo-ish utility """
...     def foo():
...         """ return some foo """
>>> from zope.publisher.browser import BrowserPage
>>> from zope.component import queryUtility
>>> class FooView(BrowserPage):
...    """ a browser view """
...    def __call__(self, *args, **kw):
...        foo = queryUtility(IFoo, default=None)
...        if foo is not None:
...            return
...        else:
...            return 'sif!'

Unfortunately the view class cannot be directly imported from here, i.e. relatively, so we have to make it available from somewhere else in order to register it:

>>> from zope.globalrequest import tests
>>> tests.FooView = FooView
>>> zcml("""
... <configure
...     xmlns=""
...     xmlns:browser="">
...   <include package="zope.browserpage" file="meta.zcml" />
...   <browser:page
...     name="foo"
...     for="*"
...     class="zope.globalrequest.tests.FooView"
...     permission="zope.Public" />
... </configure>
... """)

Next let’s make sure our test view actually works:

>>> from zope.testbrowser.wsgi import Browser
>>> browser = Browser()
>>> browser.contents

The view tries to query for a utility and use it to “calculate” it’s response, so let’s define one:

>>> from zope.globalrequest import getRequest
>>> class Foo(object):
...     def foo(self):
...         request = getRequest()
...         if request:
...             name = request.get('name', 'n00b')
...         else:
...             name = 'foo'
...         return 'y0 %s!' % name

Again, the utility class and interface cannot be directly imported from here, so let’s also make them available from somewhere else in order to register utility:

>>> tests.Foo = Foo
>>> tests.IFoo = IFoo
>>> zcml("""
... <configure xmlns="">
...   <include package="zope.component" file="meta.zcml" />
...   <utility
...     factory="zope.globalrequest.tests.Foo"
...     provides="zope.globalrequest.tests.IFoo" />
... </configure>
... """)

Rendering the view again should now give us the default value provided by the utility:

>>> browser.reload()
>>> browser.contents
'y0 foo!'

Up to now the request hasn’t been stored for us yet, so let’s hook up the necessary event subscribers and try that again:

>>> zcml("""
... <configure xmlns="">
...   <include package="zope.component" file="meta.zcml" />
...   <include package="zope.globalrequest" />
... </configure>
... """)

Now we should get the request and therefore the fallback value from the form lookup:

>>> browser.reload()
>>> browser.contents
'y0 n00b!'

If we now provide a request value we should be greeted properly:

>>> browser.contents
'y0 d4wg!!'

Once the request has been processed, it should not be available anymore:

>>> print(getRequest())


1.3 (2016-10-22)

  • Python 3 compatibility.

1.2 (2016-06-07)

  • Lighten test dependencies by using neither nor any longer.

1.1 (2015-04-29)

  • Fix import locations and declare all dependencies. [thet]

1.0 (2010-08-07)

  • Fix test setup regarding zope.securitypolicy. [ldr]

1.0a2 (2009-01-17)

1.0a1 (2009-01-15)

  • Initial release [witsch]
File Type Py Version Uploaded on Size
zope.globalrequest-1.3.tar.gz (md5) Source 2016-10-22 5KB