From 9ef3e4f0474388dd77b022a5c617ee0da0cd6c34 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Apr 28 2016 08:16:35 +0000 Subject: Fix re-ordering the widgets on both left and right sides --- diff --git a/hubs/app.py b/hubs/app.py index ac35c48..a35dee8 100755 --- a/hubs/app.py +++ b/hubs/app.py @@ -138,30 +138,64 @@ def hub_edit_post(name): is_js = flask.request.form.get('js', False) error = False + # Check the input submitted - widget_ids = [ - w.strip().replace('widget-', '') for w in flask.request.form.getlist('widgets[]') + # Right side + r_widget_ids = [ + w.strip().replace('widget-', '') + for w in flask.request.form.getlist('right_widgets[]') if w.strip() ] try: - widget_ids = [int(w) for w in widget_ids] + r_widget_ids = [int(w) for w in r_widget_ids] except: flask.flash('Invalid widget identifiers submitted', 'error') error = True - indexes = [ - i.strip() for i in flask.request.form.getlist('indexes[]') + r_indexes = [ + i.strip() for i in flask.request.form.getlist('right_indexes[]') if i.strip() ] try: - indexes = [int(i) for i in indexes] + r_indexes = [int(i) for i in r_indexes] except: if not is_js: flask.flash('Invalid indexes submitted', 'error') error = True - if len(widget_ids) != len(indexes): + if len(r_widget_ids) != len(r_indexes): + if not is_js: + flask.flash( + 'The number of indexes and the number of widgets are not of ' + 'the same length', 'error') + error = True + + # Left side + l_widget_ids = [ + w.strip().replace('widget-', '') + for w in flask.request.form.getlist('left_widgets[]') + if w.strip() + ] + try: + l_widget_ids = [int(w) for w in l_widget_ids] + except: + flask.flash('Invalid widget identifiers submitted', 'error') + error = True + + l_indexes = [ + i.strip() for i in flask.request.form.getlist('left_indexes[]') + if i.strip() + ] + + try: + l_indexes = [int(i) for i in l_indexes] + except: + if not is_js: + flask.flash('Invalid indexes submitted', 'error') + error = True + + if len(l_widget_ids) != len(l_indexes): if not is_js: flask.flash( 'The number of indexes and the number of widgets are not of ' @@ -170,10 +204,15 @@ def hub_edit_post(name): # If all good, update the database if not error: - for cnt, wid in enumerate(widget_ids): + for cnt, wid in enumerate(r_widget_ids): + widget = hubs.models.Widget.get(session, wid) + if widget.index != r_indexes[cnt]: + widget.index = r_indexes[cnt] + session.add(widget) + for cnt, wid in enumerate(l_widget_ids): widget = hubs.models.Widget.get(session, wid) - if widget.index != indexes[cnt]: - widget.index = indexes[cnt] + if widget.index != l_indexes[cnt]: + widget.index = l_indexes[cnt] session.add(widget) hub.last_edited = datetime.datetime.utcnow() session.add(hub) diff --git a/hubs/templates/hubs.html b/hubs/templates/hubs.html index a65f1d0..77c0135 100644 --- a/hubs/templates/hubs.html +++ b/hubs/templates/hubs.html @@ -226,6 +226,8 @@ $('#save_edits_btn').click(function() { _r_widgets.push($(el).attr('id').split('widget-')[1]) }); + var _l_indexes = []; + var _l_widgets = []; $.each($('#left_widgets .widget'), function(i, el) { _l_indexes.push(i); _l_widgets.push($(el).attr('id').split('widget-')[1])