Utility can't find REQUEST
plone at hannosch.info
Thu Aug 30 07:16:06 UTC 2007
Ross Patterson wrote:
> sirgarr <georgeleejr at gmail.com> writes:
>> I am trying to code a utility as follows, but on runtime the call of
>> toLocalizedTime raises an AttributeError of REQUEST. I thought that there
>> was a REQUEST somehow built in and lurking in the background when this code
>> got called -- for instance, doesn't getUtility know about the request and
>> use it to find the closest site manager? How would I fix this code?
The nearest site manager is determined during URL traversal (the part of
the Zope publisher which inspects the URL and matches it against
folders/methods/views/whatever). During this step whenever one path
segment of the URL happens to be a site manager this is recorded in a
thread local variable.
Once URL traversal has done its work and the actual code is invoked, all
calls to functions returning components will internally respect this
thread local variable to find the nearest site manager. After the
request ends the thread local variable is cleared again and recalculated
for the next request.
> Only acquisition wrapped objects can access self.REQUEST. Utilities are
> not wrapped. Either pass in the request or use a tool.
Utilities are also wrapped so the Zope2 security machinery still works,
but their Acquisition chain does not contain the REQUEST.
Whenever you need the request in your code you probably want to write a
browser view which has access to both the context and the request. A
method that does some kind of work on the request but is completely
independent from the context could be both written as a simple method
(made importable TTW) or an utility. Both of these would need to take
the request as an argument to their function calls.
More information about the Product-Developers