0

I'm using Flask, Flask-SQLAlchemy, Flask-Login and Flask-WTForm. I'm trying to build a single user-settings view for my app ("/user/"). If I use Ajax or a form to modify the user's settings through POST, the settings are committed. However as soon as the user leaves that view, an SQLAlchemy UPDATE fires again, meaning that if the user logs out and attempts to log back in it will hang the server due to an uncommitted change.

I think this is due to Flask-Login creating another session for the current_user proxy in the "view" it creates for the POST request, and this is why I added the merge() command to the following code. But that doesn't seem to solve the problem.

@app.route('/user/', methods=['GET', 'POST'])
@login_required
def user():
    if request.method == 'POST':
        if 'something' in request.form:
            current_user.some_setting = 'some new data'
            db.session.merge(current_user)
            print "ABOUT TO COMMIT IN /USER/"
            db.session.commit()
            rjson = {'response': 'success'}
        else:
            rjson = {'response': 'failure'}
        return jsonify(rjson)

    return render_template('user/index.html', current_user=current_user)

Here is the debug info from the server. After the "POST /user/ HTTP/1.1" line I move to another page:

ABOUT TO COMMIT IN /USER/
2016-10-30 15:27:21,117 INFO sqlalchemy.engine.base.Engine UPDATE users SET some_setting=%(some_setting)s WHERE users.id = %(users_id)s
INFO:sqlalchemy.engine.base.Engine:UPDATE users SET some_setting=%(some_setting)s WHERE users.id = %(users_id)s
2016-10-30 15:27:21,117 INFO sqlalchemy.engine.base.Engine {'users_id': 1, 'some_setting': 'some new data'}
INFO:sqlalchemy.engine.base.Engine:{'users_id': 1, 'some_setting': 'some new data'}
2016-10-30 15:27:21,118 INFO sqlalchemy.engine.base.Engine COMMIT
INFO:sqlalchemy.engine.base.Engine:COMMIT
INFO:werkzeug:127.0.0.1 - - [30/Oct/2016 15:27:21] "POST /user/ HTTP/1.1" 200 -
### HERE IS WHERE I MOVE TO ANOTHER VIEW ###
2016-10-30 15:27:35,312 INFO sqlalchemy.engine.base.Engine UPDATE users SET some_setting=%(some_setting)s WHERE users.id = %(users_id)s
INFO:sqlalchemy.engine.base.Engine:UPDATE users SET some_setting=%(some_setting)s WHERE users.id = %(users_id)s
2016-10-30 15:27:35,313 INFO sqlalchemy.engine.base.Engine {'users_id': 1, 'some_setting': 'some new data'}
INFO:sqlalchemy.engine.base.Engine:{'users_id': 1, 'some_setting': 'some new data'}

Any ideas?

5
  • Why do you use merge? If your user is already in session, all you have to do is to modify it and commit… Oct 30, 2016 at 20:18
  • @LaurentLAPORTE I added the merge because I wasn't sure if another session was being created or not by the POST request, as it seemed to generate two UPDATEs. The merge did nothing to stop this of course. Oct 30, 2016 at 20:23
  • @dirn I believe so. If this was just a repeated debug message I would see another COMMIT message at the end of the snippet I posted, I think. I checked the postgres db and on the next commit (when a user logs out and logs back in, for instance, I update some data from an api) it's waiting for a previous commit. Oct 30, 2016 at 21:04
  • Mobile web had some gnarly line breaks. I can see the logs more clearly now. Your endpoint only executes the query and commits once. What is the second view? SQLAlcheny sessions aren't like browser sessions. They don't survive across requests.
    – dirn
    Oct 30, 2016 at 21:15
  • @dirn I consistently move to my index view or my logout view to test this, as everything else I have coded requires admin access (and those accounts won't have use for these settings). The index view simply: return render_template('index.html'), on that template there is a conditional to see if the user is logged in with current_user.is_authenticated. Also visiting the logout view calls the Flask-Login logout_user() and redirects to the index. Oct 30, 2016 at 21:40

0

Your Answer

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