From 2ddf810909179d1cf959e2aea3b187cd3ce9d194 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Apr 22 2016 06:30:39 +0000 Subject: Adjust hub_edit_post behavior for request made from javascript If a POST request is made from javascript, we do not want to flash messages, as they will appear the next time someone load a page, potentially a completely different page. So, we will assume request made from JS will declare themselves as such using js=true and for them we will make things easier: work (200 OK) or does not work (400), no message flashed. --- diff --git a/hubs/app.py b/hubs/app.py index 7b725a1..375816d 100755 --- a/hubs/app.py +++ b/hubs/app.py @@ -133,6 +133,7 @@ def hub_edit_get(name): def hub_edit_post(name): hub = get_hub(session, name) + is_js = flask.request.form.get('js', False) error = False # Check the input submitted @@ -154,13 +155,15 @@ def hub_edit_post(name): try: indexes = [int(i) for i in indexes] except: - flask.flash('Invalid indexes submitted', 'error') + if not is_js: + flask.flash('Invalid indexes submitted', 'error') error = True if len(widget_ids) != len(indexes): - flask.flash( - 'The number of indexes and the number of widgets are not of ' - 'the same length', 'error') + if not is_js: + flask.flash( + 'The number of indexes and the number of widgets are not of ' + 'the same length', 'error') error = True # If all good, update the database @@ -175,11 +178,20 @@ def hub_edit_post(name): try: session.commit() except Exception as err: - flask.flash( - 'Could not save your changes to the database '\ - 'if the error persists, please warn an admin', - 'error') - return flask.redirect(flask.url_for('hub', name=name)) + error = True + if not is_js: + flask.flash( + 'Could not save your changes to the database '\ + 'if the error persists, please warn an admin', + 'error') + if is_js: + if error: + flask.abort(400) + else: + return ('ok', 200) + else: + return flask.redirect(flask.url_for('hub', name=name)) + @app.route('///')