flask-sqlalchemy: how to conditionally insert or update a row,db.session.commit()