[Grok-dev] grok.local_utility is not registering anything in the app

Vanderson Mota dos Santos vanderson.mota at gmail.com
Fri Feb 20 19:33:14 EST 2009


Sorry Martjin, i've forgot to click on reply all:

"If you create a view like this for your application:

class Index(grok.View):
    def render(self):
        intids = component.getUtility(IIntIds)
        print intids
        return "the result"

do you still get a component lookup error when you access the view, or
do you get the intids? (assuming you re-installed your app first)."

I'm tried to do this in View (I've inherited from Form, but i guess there is
no problem) class indeed.

I can only access these utilities (intids and relationCatalog) from the
Application class context. When i try to do "intids =
component.getUtility(IIntIds)" from a diferent context, like: MyApp
->ContainerA --> ContainerB -> Model -> View class a LookupError is
returned. I guess that these utilities should be accessible in any context,
right?

"To make it work in your tests, you might need to do something like this
before you do any getUtility lookups:

from zope.app.component.hooks import setSite

setSite(your_app)"

I didn't did this in this test, and the test passed:
http://pastebin.com/m10b71a8

As you see, if i pass the app as context, the getUtility Works.

2009/2/20 Martijn Faassen <faassen at startifact.com>

> Hi there,
>
> Using local_utility should indeed be the right way to set up utilities
> with Grok.
>
> Vanderson Mota dos Santos wrote:
>  > the problem is, when i try to use component.getUtility(IIntIds), a
>  > componentLookUpError appear to me.
>
> ...
>
> > I know this, but the tests are failing. And i'm running them with the
> > functional layer. Every time i run the tests, a new instance is
> > created(obviously), so, the local_utilities should be accessible.
>
> It'd be interested to see your failing tests.
>
> If you create a view like this for your application:
>
> class Index(grok.View):
>     def render(self):
>         intids = component.getUtility(IIntIds)
>         print intids
>         return "the result"
>
> do you still get a component lookup error when you access the view, or
> do you get the intids? (assuming you re-installed your app first).
>
> One thing to know is that in order to lookup local utilities, the
> correct site needs to be set first to your application. The normal
> traversal process does this for you, which is why I hope that the above
> view will do the right thing for you.
>
> In tests, setSite() may very well not have been used and therefore you
> would look up only global utilities, not those for your application object.
>
> To make it work in your tests, you might need to do something like this
> before you do any getUtility lookups:
>
> from zope.app.component.hooks import setSite
>
> setSite(your_app)
>
> Hopefully this will help resolve the problem. If not there might be
> something deeper going on...
>
> Regards,
>
> Martijn
>
>
> _______________________________________________
> Grok-dev mailing list
> Grok-dev at zope.org
> http://mail.zope.org/mailman/listinfo/grok-dev
>



-- 
Vanderson Mota dos Santos
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.zope.org/pipermail/grok-dev/attachments/20090220/3645db3f/attachment.html 


More information about the Grok-dev mailing list