From e0a8b41c23e7fd8621aebb4d4353b9f83e8c30c8 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Apr 28 2016 08:16:34 +0000 Subject: [PATCH 1/6] Fix the URLs for when editing a widget --- diff --git a/hubs/app.py b/hubs/app.py index 3732eea..5f5f3b6 100755 --- a/hubs/app.py +++ b/hubs/app.py @@ -211,20 +211,25 @@ def widget_json(hub, idx): return response -@app.route('///edit/', methods=['GET']) -@app.route('///edit', methods=['GET']) +@app.route('///edit/', methods=['GET', 'POST']) +@app.route('///edit', methods=['GET', 'POST']) +def widget_edit(hub, idx): + if flask.request.method == 'POST': + return widget_edit_post(hub, idx) + else: + return widget_edit_get(hub, idx) + + def widget_edit_get(hub, idx): widget = get_widget(session, hub, idx) return flask.render_template( 'edit.html', hub=hub, widget=widget, - url_to=flask.url_for('widget_edit_post', hub=hub, idx=idx) + url_to=flask.url_for('widget_edit', hub=hub, idx=idx) ) -@app.route('///edit/', methods=['POST']) -@app.route('///edit', methods=['POST']) def widget_edit_post(hub, idx): widget = get_widget(session, hub, idx) error = False diff --git a/hubs/widgets/base.py b/hubs/widgets/base.py index 49ca5b2..fcce04a 100755 --- a/hubs/widgets/base.py +++ b/hubs/widgets/base.py @@ -49,7 +49,7 @@ def AGPLv3(name): result['widget_url'] = flask.url_for( 'widget_render', hub=widget.hub.name, idx=widget.idx) result['edit_url'] = flask.url_for( - 'widget_edit_get', hub=widget.hub.name, idx=widget.idx) + 'widget_edit', hub=widget.hub.name, idx=widget.idx) result['widget'] = widget return result From f94340ee350277d3439ad8dce2f33013f564814f Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Apr 28 2016 08:16:34 +0000 Subject: [PATCH 2/6] Fix calls to url_for() --- diff --git a/hubs/templates/hubs.html b/hubs/templates/hubs.html index f4fa36b..c0c93c5 100644 --- a/hubs/templates/hubs.html +++ b/hubs/templates/hubs.html @@ -91,10 +91,10 @@
{% if hub.archived %} + src="{{ url_for('static', filename='img/archived.png') }}"/> {% else %} + src="{{ url_for('static', filename='img/cobweb.png') }}"/> {% endif %}
From 6e87bee5fa0298f2b89c7cba2d8809c4f038693f Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Apr 28 2016 08:16:35 +0000 Subject: [PATCH 3/6] Fix allowing to order widgets on the right column --- diff --git a/hubs/templates/hubs.html b/hubs/templates/hubs.html index c0c93c5..b142fd6 100644 --- a/hubs/templates/hubs.html +++ b/hubs/templates/hubs.html @@ -125,7 +125,7 @@
{% for widget in hub.left_widgets %} -
+
{% endfor %}
@@ -175,6 +175,12 @@ function make_widget_sortable() { var byId = function (id) { return document.getElementById(id); } + Sortable.create(byId('left_widgets'), { + animation: 150, + draggable: '.widget', + handle: '.card', + }); + Sortable.create(byId('right_widgets'), { animation: 150, draggable: '.widget', @@ -184,16 +190,23 @@ function make_widget_sortable() { }; $('#save_edits_btn').click(function() { - var _indexes = []; - var _widgets = []; + var _r_indexes = []; + var _r_widgets = []; $.each($('#right_widgets .widget'), function(i, el) { - _indexes.push(i); - _widgets.push($(el).attr('id').split('widget-')[1]) + _r_indexes.push(i); + _r_widgets.push($(el).attr('id').split('widget-')[1]) + }); + + $.each($('#left_widgets .widget'), function(i, el) { + _l_indexes.push(i); + _l_widgets.push($(el).attr('id').split('widget-')[1]) }); $.post( '{{ url_for("hub_edit", name=hub.name) }}', - {'indexes': _indexes, 'widgets': _widgets, 'js': true}, + {'right_indexes': _r_indexes, 'right_widgets': _r_widgets, + 'left_indexes': _l_indexes, 'left_widgets': _l_widgets, + 'js': true}, function(){ window.location = '{{ url_for("hub", name=hub.name) }}' } ); }); From 5095e60f7dd2235db50323a4d6ca12d0113e4740 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Apr 28 2016 08:16:35 +0000 Subject: [PATCH 4/6] Add the logic add a widget to a hub --- diff --git a/hubs/app.py b/hubs/app.py index 5f5f3b6..b5d3453 100755 --- a/hubs/app.py +++ b/hubs/app.py @@ -11,6 +11,7 @@ import six from flask.ext.openid import OpenID import hubs.models +import hubs.widgets import datanommer.models @@ -131,6 +132,7 @@ def hub_edit_get(name): return flask.render_template( 'hubs.html', hub=hub, session=session, edit=True) + def hub_edit_post(name): hub = get_hub(session, name) is_js = flask.request.form.get('js', False) @@ -193,6 +195,106 @@ def hub_edit_post(name): return flask.redirect(flask.url_for('hub', name=name)) +@app.route('//add/', methods=['GET', 'POST']) +@app.route('//add', methods=['GET', 'POST']) +def hub_add_widgets(name): + if flask.request.method == 'POST': + return hub_add_widget_post(name) + else: + return hub_add_widget_get(name) + + +@app.route('//add//', methods=['GET']) +@app.route('//add/', methods=['GET']) +def hub_add_widget(name, widget_name): + hub = get_hub(session, name) + side = str(flask.request.args.get('position')).lower() + if side not in ['right', 'left']: + flask.abort(400, 'Invalid position provided') + + widget = None + w_name = None + for widgt in hubs.widgets.registry: + if hubs.widgets.registry[widgt].position in ['both', side] \ + and widgt == widget_name: + w_name = widgt + widget = hubs.widgets.registry[widgt] + + return flask.render_template( + 'add_widget.html', + hub=hub, + widget=widget, + widget_name=w_name, + side=side, + url_to=flask.url_for('hub_add_widgets', name=name), + ) + + +def hub_add_widget_get(name): + hub = get_hub(session, name) + side = str(flask.request.args.get('position')).lower() + if side not in ['right', 'left']: + flask.abort(400, 'Invalid position provided') + + widgets = [ + widget + for widget in hubs.widgets.registry + if hubs.widgets.registry[widget].position in ['both', side] + ] + return flask.render_template( + 'add_widget.html', + hub=hub, + widgets=widgets, + side=side, + ) + + +def hub_add_widget_post(name): + print flask.request.form + widget_name = flask.request.form.get('widget_name') + position = flask.request.form.get('position', '').lower() + if not widget_name: + flask.abort(400, 'Invalid request sent') + if widget_name not in hubs.widgets.registry: + flask.abort(404, 'Unknown widget called') + + hub = get_hub(session, name) + + widget = widget = hubs.models.Widget( + plugin=widget_name, index=-1, left=position=='left') + error = False + config = {} + for arg in widget.module.data.widget_arguments: + val = flask.request.form.get(arg.name) + if not val: + flask.flash( + 'You must provide a value for: %s' % arg.name, 'error') + error = True + break + try: + arg.validator(session, val) + config[arg.name] = val + except Exception as err: + flask.flash('Invalid data provided, error: %s' % err, 'error') + error = True + if not error: + widget.hub = hub + widget.config = config + try: + session.add(widget) + session.flush() + widget.hub.last_edited = datetime.datetime.utcnow() + session.commit() + except Exception as err: + print err + flask.flash( + 'Could not save the configuration to the database '\ + 'if the error persists, please warn an admin', + 'error') + + return flask.render_template( + 'hubs.html', hub=hub, session=session, edit=True) + @app.route('///') @app.route('//') diff --git a/hubs/templates/add_widget.html b/hubs/templates/add_widget.html new file mode 100644 index 0000000..6aa0dfa --- /dev/null +++ b/hubs/templates/add_widget.html @@ -0,0 +1,84 @@ + + + +{% if widgets %} + +{% endif %} diff --git a/hubs/templates/hubs.html b/hubs/templates/hubs.html index b142fd6..a65f1d0 100644 --- a/hubs/templates/hubs.html +++ b/hubs/templates/hubs.html @@ -114,7 +114,7 @@

- + Add a widget

@@ -136,7 +136,7 @@

- + Add a widget

@@ -189,6 +189,35 @@ function make_widget_sortable() { }; +function setup_add_btns() { + $(".add_widget").unbind(); + $(".add_widget").click(function() { + console.log($(this)); + var _pos = $(this).attr('data-position'); + + $.ajax({ + url: 'add?position=' + _pos, + dataType: 'html', + success: function(html) { + $('#edit_modal_content').html(html); + $('#edit_modal').modal(); + }, + error: function() { + $('#edit_modal_content').html( + '' + ); + console.log('error'); + console.trace(); + $('#edit_modal').modal(); + }, + }); + return false; + }); +} + $('#save_edits_btn').click(function() { var _r_indexes = []; var _r_widgets = []; @@ -270,6 +299,7 @@ function setup_widgets(widgets) { {% if edit -%} make_widget_sortable(); + setup_add_btns(); {%- endif %} } From 9001ba2a1f67bdbbe96dd6a6c1f943198b211cef Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Apr 28 2016 08:16:35 +0000 Subject: [PATCH 5/6] After adding a new widget, remain on edit mode so that it can be placed --- diff --git a/hubs/app.py b/hubs/app.py index b5d3453..ac35c48 100755 --- a/hubs/app.py +++ b/hubs/app.py @@ -378,7 +378,7 @@ def widget_edit_delete(hub, idx): 'Could not delete this widget from this hub in the database '\ 'if the error persists, please warn an admin', 'error') - return flask.redirect(flask.url_for('hub', name=hub)) + return flask.redirect(flask.url_for('hub_edit', name=hub)) @app.route('/source//') From 9ef3e4f0474388dd77b022a5c617ee0da0cd6c34 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Apr 28 2016 08:16:35 +0000 Subject: [PATCH 6/6] 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])