advice about `correct' use of decorator

G

Gerardo Herzig

Hi all. I guess i have a conceptual question:
Im planing using a quite simple decorator to be used as a conditional
for the execution of the function. I mean something like that:

@is_logued_in
def change_pass():
bla
bla

And so on for all the other functions who needs that the user is still
loged in.

where obviosly the is_logued_in() function will determine if the dude is
still loged in, and THEN execute change_pass(). If the dude is not loged
in, change_pass() is NOT executed at all. Instead, it will be redirected
to the `login' screen.

Something in my mind tells me that this is not the pythonic way...But i
like the idea, so please tell me that im in the right way :)

Cheers.
Gerardo
 
S

Steven Bethard

Gerardo said:
Hi all. I guess i have a conceptual question:
Im planing using a quite simple decorator to be used as a conditional
for the execution of the function. I mean something like that:

@is_logued_in
def change_pass():
bla
bla

And so on for all the other functions who needs that the user is still
loged in.

where obviosly the is_logued_in() function will determine if the dude is
still loged in, and THEN execute change_pass(). If the dude is not loged
in, change_pass() is NOT executed at all. Instead, it will be redirected
to the `login' screen.

Something in my mind tells me that this is not the pythonic way...But i
like the idea, so please tell me that im in the right way :)

Django does it almost exactly this way:

http://www.djangoproject.com/documentation/authentication/#the-login-required-decorator

STeVe
 
B

Bruno Desthuilliers

Gerardo Herzig a écrit :
(snip)
>
Great! So from now on, my idea is labeled as `djangothic' :)
Thanks a lot!

Which doesn't necessarily imply it's pythonic !-)
 
L

Lawrence D'Oliveiro

@is_logued_in
def change_pass():
bla
bla

And so on for all the other functions who needs that the user is still
loged in.

My suspicion is that most of the methods in your session object (with the
obvious exception of the login method) will require the user to be logged
in, right?

So the trouble with an extra decorator like this, is that you have to
remember to put it on nearly _all_ your methods.

Wouldn't it be simpler and safer to default the other way? Have some way of
saying "doesn't require user to be logged in", and require a valid
logged-in session on all methods where this is not specified?
 
G

Gregor Horvath

Gerardo said:
@is_logued_in
def change_pass():
bla
bla

And so on for all the other functions who needs that the user is still
loged in.

where obviosly the is_logued_in() function will determine if the dude is
still loged in, and THEN execute change_pass(). If the dude is not loged
in, change_pass() is NOT executed at all. Instead, it will be redirected
to the `login' screen.

Something in my mind tells me that this is not the pythonic way...But i
like the idea, so please tell me that im in the right way :)

That's how turbogears does it.
See:

http://docs.turbogears.org/1.0/UsingIdentity?highlight=(identity)


Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top