On Thursday, December 20, 2012 11:00:28 AM UTC-5, Ramos wrote:

> Now i have 2 problems.
> Recap
> 1 - 
>     print auth.user.isempreiteiro  # *returns None*
>     record=db(db.auth_user.id==auth.user_id).select()[0]
>     print  record['isempreiteiro'] #*returns True  "This is the real 
> value"*
> Why?
>

Is isempreiteiro a virtual field or regular field? Is its value set after 
login (auth.user is stored in the session, so it won't be updated after 
login even if the record is updated)? If so, you can manually update 
auth.user when the record is updated.
 

>
> 2 - 
> my decorated function in controller default works ok when user logged in
>
> @auth.requires_login()   
> *#@auth.requires(checkempreiteiro(auth.user_id)==True) *
> def inserttrab():
>     form = SQLFORM(db.trabalhador)
>     if form.accepts(request.vars, session):
>         session.flash = 'registo inserido com sucesso'
>         redirect(URL())
>     return dict(form=form)
>
> but gives an error when the user does logout. logging out should redirect 
> to login page but the line
>

Your checkempreiteiro() function does db(db.auth_user.id
==auth.user_id).select()[0], but if the user is not logged in, auth.user_id 
will be None, and the query will return no results -- so subscripting with 
[0] will produce an error. Instead, you can do db(db.auth_user.id
==auth.user_id).select().first(), which will return None if there are no 
records.

Also, note that by default, @auth.requires(...) already requires login, so 
you don't have to precede it with @auth.requires_login().

Finally, keep in mind that the condition within @auth.requires(...) will be 
executed on every request to that controller (even requests to other 
functions within the controller), so it is better to put the condition 
inside a lambda so it will only get executed when that specific function is 
called. Actually, in a case like this, you're probably better off putting a 
flag in the session so you don't have to repeat the db query on every 
request to that function.

Anthony

-- 



Reply via email to