[Zope3-Users] Re: Calling utitlity without context
Philipp von Weitershausen
philipp at weitershausen.de
Tue Apr 10 13:04:48 EDT 2007
Florian Lindner wrote:
> Hello,
> I have a function that is called every 60 seconds from zope.scheduler. Due to
> that it does not have a context. However I need to call a utility. I use that
> piece of code:
>
> def releaseRefreshEvent():
> """ Calls all utlities that have a INewsfeed interface registered. """
> import pdb; pdb.set_trace()
> print "releaseRefreshEvent!!"
> db = getUtility(ZODB.interfaces.IDatabase)
> conn = db.open()
> root = conn.root().data['Application']
> zope.app.component.hooks.setSite(root)
This function (setSite) sets the active site. Here you set it to the
root folder. That means all components registered in the root site or
higher (=global registry) will be found.
> utils = getUtilitiesFor(INewsfeed)
> for i in utils:
> print "Utility called:", i[0]
> i[1].sendNotification()
>
> conn.close()
>
>
> This works fine as long as the registered utility is in root site. As soon as
> it is in another site it does not find the utility anymore.
>
> How can I fix that?
Call setSite() with the appropriate site argument if you want to be able
to look up components from there.
A note for the general audience who's reading this: you *don't* have to
call setSite() in your "normal" Zope 3 code (views, etc.) if you're
looking up local components. Zope 3 already does it for you during
traversal. The reason Florian has to call it here is that his code
clearly operates outside of any request, therefore no traversal is going on.
--
http://worldcookery.com -- Professional Zope documentation and training
More information about the Zope3-users
mailing list