views:

84

answers:

2

Hi I'm trying to update a user row upon the user logging in. I simply want to increase the users login count by one. Here is the code in the post_login controller method:

@expose()  
def post_login(self, came_from=url('/')):  
    """  
    Redirect the user to the initially requested page on successful  
    authentication or redirect her back to the login page if login failed.  
    """  
    if not request.identity:  
        login_counter = request.environ['repoze.who.logins'] + 1  
        redirect(url('/user/login', came_from=came_from, __logins=login_counter))  

    user_name = request.identity['repoze.who.userid']
    user = User.by_user_name(user_name)
    user.tll_num_logins += 1
    user.tll_last_login = datetime.now()
    redirect(came_from)

The user record simply isnt getting updated in the database. The TG documentation says that the transaction manager should flush all of the transactions and automatically execute all the outstanding SQL but it doesnt seem to be working with update. I've tried putting in a DBSession.commit() after to manually commit but get an error message. Likewise, adding DBSession.flush() to the controller method does not error but does not actually update the record either.

A: 

You must tell the session object to update the object:

DBSession.update(user)
Stefan Lundström
A: 

Sorry all, turns out the TG2 transaction manager was working after all. The error came because I was calling the post_login function outside of the transaction manager and so the record update was not getting flushed. I'm not sure why it wasn't letting me commit. But I moved the post_login controller and now the above code I specified works, it updates automatically - no need even for DBSession.update(user).

Marc