1

In my Flask web app I have a login system. When user logged, in the table of my database I want to update the datetime when user made the last login. I'm using this code:

 @app.route('/login', methods=['POST'])
 def do_admin_login():
     POST_CODICE_FISCALE = str(request.form['codice_fiscale'])
     POST_PASSWORD = str(request.form['password'])
     pwd_enc=base64.b64encode(POST_PASSWORD)

     Session = sessionmaker(bind=engine)
     s = Session()
     query = s.query(User).filter(User.codice_fiscale.in_([POST_CODICE_FISCALE]), User.password.in_([pwd_enc]))
     result = query.first()
     if result:
        session['logged_in'] = True
        query = s.query(User).filter(User.codice_fiscale.in_([POST_CODICE_FISCALE]).update(User.data_ora_ultimo_accesso=datetime.now()))  
        query.first()
        db.session.commit()
    else:
        flash('wrong password!')
    return home()

but I receive the error:

 query = s.query(User).filter(User.codice_fiscale.in_([POST_CODICE_FISCALE]).update(User.
 data_ora_ultimo_accesso=datetime.now())) 
 SyntaxError: keyword can't be an expression

what it is wrong? thanks.

1 Answer 1

1

As the error says, a keyword in a function call argument list cannot be an expression, such as User.data_ora_ultimo_accesso, but an identifier. Instead you should pass Query.update() a dictionary of column, expression pairs:

    query = s.query(User).\
        filter(User.codice_fiscale.in_([POST_CODICE_FISCALE]).\
        update({ User.data_ora_ultimo_accesso: datetime.now() },
               synchronize_session=False))

Note that since you commit right away, there's no need to synchronize the session, since all state will be expired anyway.

You could also make some changes that'd improve your code's readability. For example instead of

filter(User.codice_fiscale.in_([POST_CODICE_FISCALE]),
       User.password.in_([pwd_enc]))

just

filter(User.codice_fiscale == POST_CODICE_FISCALE,
       User.password == pwd_enc)

No point in checking if a list of 1 item contains something, compared to just testing equality.

Finally, you create a new Session class and an instance s of it, but you commit a different session: db.session, which you should've probably been using all along. What this means is that your updates will not take place, as that session's transaction is not actually committed.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. it works without errors but it seems in database data_ora_ultimo_accesso isn't update.
Did you fix your session handling? Don't create separate session, but use the db.session consistently. Btw you could also use the ORM to perform the update: result.codice_fiscale = datetime.now() and then commit.
Do you mean to use the same session: Session = sessionmaker(bind=engine) s = Session() How can I use db.session consistently?
In other words don't create sessions, but use the one provided to you by flask-sqlalchemy: db.session.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.