[Grok-dev] getUtility() fails from IApplicationInitializedEvent function

Àlex Magaz Graça rivaldi8 at gmail.com
Sat Dec 24 19:28:33 UTC 2011

Thanks Souheil, I solved the problem setting the site:

from zope.component.hooks import setSite

@grok.subscribe(Helpdesk, grok.IApplicationInitializedEvent)
def setUpTestUsers(obj, event):
     authPlugin = getUtility(IAuthenticatorPlugin, "users")

Still, it seems the application isn't entirely initialised yet, because 
now I get an exception when the Account object is added to userFolder 
(just a container):

NotYet: <helpdesk.auth.Account object at 0x41ffb10>

The code is like this:

class UserAuthenticatorPlugin(grok.LocalUtility):

     def __init__(self):
         self.userFolder = UserFolder()

     def addTestingUsers(self):
         roleManager = IPrincipalRoleManager( grok.getSite() )
         self.userFolder["tech"] = Account("tech", "tech", "Technician", 

I just want to add some test users to the application. Should I take 
another approach to do this? Before trying with the event, I called the 
method from the plugin's __init__, but this made functional tests fail:

File "/home/alex/uni/projecte/repo/trunk/HelpDesk/src/helpdesk/auth.py", 
line 36, in __init__
"/home/alex/uni/projecte/repo/trunk/HelpDesk/src/helpdesk/auth.py", line 
41, in addTestingUsers
     roleManager = IPrincipalRoleManager( grok.getSite() )
TypeError: ('Could not adapt', None, <InterfaceClass 


El 22/12/11 11:32, Souheil CHELFOUH escribió:
> My guess : your IAuthenticatorPlugin is supposed to be a local utility
> The site manager is not yet set in the event.
> Therefore, your getUtility will only query the global registry.
> Try to set the site manager in the event or use directly the SM from
> the application object you get
> - Souheil
> 2011/12/21 Àlex Magaz Graça<rivaldi8 at gmail.com>:
>> Hi,
>> I'm getting a ComponentLookupError when getUtility() is called in the
>> following function:
>> @grok.subscribe(Helpdesk, grok.IApplicationInitializedEvent)
>> def setUpTestUsers(obj, event):
>>      authPlugin = getUtility(IAuthenticatorPlugin, "users")
>> According to Grok documentation [1]:
>> "This event can be used to trigger the creation of contents or other tasks
>> that require the application to be fully operational : utilities installed
>> and indexes created in the catalog."
>> I understand utilities should be ready. In fact, I have a print statement in
>> the utility's __init__() which is executed before getUtility() fails. I've
>> also checked the getUtility() call in the function is the same that is used
>> in other places and working just fine.
>> Any idea why this fails?
>> [1]
>> http://grok.zope.org/doc/current/reference/events.html#iapplicationinitializedevent
>> Thanks,
>> Àlex
>> _______________________________________________
>> Grok-dev mailing list
>> Grok-dev at zope.org
>> https://mail.zope.org/mailman/listinfo/grok-dev

More information about the Grok-dev mailing list