Accessing identity

4 views
Skip to first unread message

Richard (koorb)

unread,
Mar 21, 2006, 9:28:09 AM3/21/06
to TurboGears
The project I am working on at the moment uses a controllers package,
the directory contains __init__.py controllers.py and
otherControllers.py

The login and logout controllers remain in controllers.py while the
otherControllers.py uses identity.require quite happily. So what's the
problem? I have another package called forms in which I declare my form
schema's. This works fine until I try to use identity in there:

import turbogears
from turbogears import identity, widgets
from myProject import model

then for a SingleSelectField I want to output a list of groups the
current user is in

options=[(group.id, group.displayName) for group in
identity.current.groups]

sadly this results in

turbogears.identity.exceptions.IdentityManagementNotEnabledException:
An attempt was made to use a facility of the TurboGears Identity
Management framework but identity management hasn't been enabled in the
config file [via identity.on].

It is and I have been using it successfully what is different about
using it in this package? is it a name space issue? I don't know. I
hope I'm not doing something stupid but if this is something other may
run into as well it's good to talk about it.

Kevin Dangoor

unread,
Mar 21, 2006, 10:08:22 AM3/21/06
to turbo...@googlegroups.com
Hi Richard,

What does your config file look like? I ask because I want to be sure
that identity.on = True for the path in question. (Remember that
CherryPy allows config values to vary by path...)

Kevin

Richard (koorb)

unread,
Mar 21, 2006, 11:19:01 AM3/21/06
to TurboGears
Hi Kevin,

I have literally just turned on the commented items in config.py

visit.on=True
(...)
identity.on=True
(...)
identity.failure_url="/login"

And as I say it does work, correctly protects the methods even of the
mounted controller. It is only when I try to access it from
"project/forms/my_forms.py" that it raises an exception. If you want
the entire config I can send it but the only other thing that I did was
turn on tg.mochikit_all (which didn't seem to do anything incidentally)

thank you!

Kevin Dangoor

unread,
Mar 21, 2006, 11:29:04 AM3/21/06
to turbo...@googlegroups.com
The exception made me think of configuration, but the real problem is
clear from your original message...

On 3/21/06, Richard (koorb) <richard.s...@googlemail.com> wrote:

> then for a SingleSelectField I want to output a list of groups the
> current user is in
>
> options=[(group.id, group.displayName) for group in
> identity.current.groups]

And *there's* the problem. identity.current is only going to work in
the context of a request, not when you're initially setting up your
widgets. (The thing to remember about widgets is that they generally
outlive requests.)

I believe options can be a callable. Try something like:

options = lambda: [(group.id, group.displayName) for group in
identity.current.groups]

Kevin

Jeff Watkins

unread,
Mar 21, 2006, 5:36:16 PM3/21/06
to turbo...@googlegroups.com
In addition to Kevin's comment about only accessing the current identity during a request, you should probably keep in mind that the groups set is only the IDs of the groups, not the groups themselves -- this was done to minimise the impact of the actual model. If you want the model specific data, you'll have to access the user object. If you're using the default SqlObjectIdentityProvider with the default TG_* classes, the following is more what you're looking for:

    options= [(group.groupId, group.displayName) for group \
              in identity.current.user.groups]

I'll see what I can do about making the exception check whether there's a request active... it would be much smarter to have the exception say something like: I know you've turned on Identity, however, you're using code that can only execute during an HTTP request...

On 21 Mar, 2006, at 9:28 am, Richard (koorb) wrote:

options=[(group.id, group.displayName) for group in

identity.current.groups]


--
Jeff Watkins

"Advertising directed at children is inherently deceptive and exploits children under eight years of age."
-- American Academy of Pediatrics


Richard (koorb)

unread,
Mar 22, 2006, 5:13:41 AM3/22/06
to TurboGears
Ahh! thank you very much guys suddenly the light dawns, that's great!

Michele Cella

unread,
Mar 22, 2006, 5:19:05 AM3/22/06
to TurboGears
Jeff Watkins wrote:
>
> I'll see what I can do about making the exception check whether
> there's a request active... it would be much smarter to have the
> exception say something like: I know you've turned on Identity,
> however, you're using code that can only execute during an HTTP
> request...
>

Hi Jeff,

As soon as commit access works again I will add to turbogears.util a
request_available function that you could use for this.

Ciao
Michele

Richard (koorb)

unread,
Mar 22, 2006, 11:33:07 AM3/22/06
to TurboGears
Thanks so much for your help, but ...

Kevin Dangoor wrote:
> options = lambda: [(group.id, group.displayName) for group in
> identity.current.groups]

I tried that, only got the same error. Also tried specially defining a
function:

def current_group_list():
return [(group.id, group.displayName) for group in
identity.current.user.groups]

(...)

group = widgets.SingleSelectField(name="group", label="Assign to
group",
options=current_group_list,
help_text=_("..."))

just results in IdentityManagementNotEnabledException

Michele Cella

unread,
Mar 22, 2006, 12:19:16 PM3/22/06
to TurboGears
Richard (koorb) wrote:
> I tried that, only got the same error. Also tried specially defining a
> function:
>
> def current_group_list():
> return [(group.id, group.displayName) for group in
> identity.current.user.groups]
>
> (...)
>
> group = widgets.SingleSelectField(name="group", label="Assign to
> group",
> options=current_group_list,
> help_text=_("..."))
>
> just results in IdentityManagementNotEnabledException

It's strange if you're really passing current_group_list and not
current_group_list() this function will be called only at display time
(when the request is available), when are you receiving this exception?
when you click that page or before?

Ciao
Michele

Richard (koorb)

unread,
Mar 22, 2006, 1:00:49 PM3/22/06
to TurboGears

Michele Cella wrote:
> It's strange if you're really passing current_group_list and not
> current_group_list() this function will be called only at display time
> (when the request is available), when are you receiving this exception?
> when you click that page or before?
>
> Ciao
> Michele

actually I cant even start the server! It raises the exception when I
do ./start-project.py

Reply all
Reply to author
Forward
0 new messages