[Zope3-Users] Get a site

Florian Lindner mailinglists at xgm.de
Mon Jan 22 08:39:53 EST 2007


Am Montag, 22. Januar 2007 08:32 schrieb Christian Theune:
> Hi,
>
> Am Sonntag, den 21.01.2007, 14:44 +0100 schrieb Florian Lindner:
> > Hello,
> > I am in a function that has no context and want to set a site.
> > The function is called periodically from a scheduler:
> >
> > from scheduler import loop
> >
> > def onStartup(event):
> >     refresher = loop.LoopTask(releaseRefreshEvent, interval=60)
> >
> > In this function I need to call getUtilitiesFor which needs a site set
> > but I don't know how to get a site:
> >
> > (Pdb)  from zope.app.component import hooks
> > (Pdb) from zope.app.zapi import *
> >
> > (Pdb) hooks.setSite(getSiteManager())
> > *** AttributeError: 'BaseGlobalComponents' object has no
> > attribute 'getSiteManager'
> >
> > (Pdb) hooks.setSite(getGlobalSiteManager())
> > *** AttributeError: 'BaseGlobalComponents' object has no
> > attribute 'getSiteManager'
>
> You don't set the site managers as a site, but the objects that are
> sites have site managers (or "component registries") attached to them.
>
> Also, the global registry is always "active", without settinga site.

And how do I get the global registry?

>
> > Just getting the root object (which AFAIK always is a site) would be ok.
> > Or the object of static path.
>
> You'd have to open a connection to the database, get the root object and
> use that with setSite(). I don't have the complete spelling for that in
> my head though. If this is code that runs within Zope 3, then you might
> not want to talk to the database directly anyway because of the
> automatic transaction management.

The code is not being run out of Zope. It's just not part of any class, but 
it's only called when Zope is app and running.

This function is called upon a IProcessStartingEvent event:

def onStartup(event):
    """ Perform initializations on startup of the server. """
    refresher = loop.LoopTask(releaseRefreshEvent, interval=60)
    refresher.start()

in the releaseRefreshEvent I want to call all registered utilities:

def releaseRefreshEvent():
    """ Calls all utlities that have a INewsfeed interface registered. """
    utils = getUtilitiesFor(INewsfeed)
    for i in utils:
        print "Utility called:", i.__name__
        i.sendNotification()


but that always returns no utilites at all. IMHO the problem is that a site is 
not set. How would you do that? 
(sorry, your answer is somehow too sophisticated for me...)

Thanks,

Florian


More information about the Zope3-users mailing list