From 79aa34d2af8a3430c174b0d01e98ceb211b33618 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Apr 14 2016 18:25:22 +0000 Subject: [PATCH 1/7] Fix setting the configuration of a widget --- diff --git a/hubs/models.py b/hubs/models.py index 086e0b1..059ad28 100755 --- a/hubs/models.py +++ b/hubs/models.py @@ -309,7 +309,7 @@ class Widget(BASE): return json.loads(self._config) @config.setter - def config_setter(self, config): + def config(self, config): self._config = json.dumps(config) def __json__(self, session): From 3d1ec7bbc8aba94de6b72f77a6a8e0dce9c45322 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Apr 14 2016 18:25:22 +0000 Subject: [PATCH 2/7] Let's have the edit_url and the widget idx in the panel template --- diff --git a/hubs/widgets/base.py b/hubs/widgets/base.py index e784ef1..a842baf 100755 --- a/hubs/widgets/base.py +++ b/hubs/widgets/base.py @@ -48,6 +48,9 @@ def AGPLv3(name): result['source_url'] = flask.url_for('widget_source', name=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) + result['widget_idx'] = widget.idx return result return inner From 9b2bb1885f85cd935c591c707459f578de24e32c Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Apr 14 2016 18:25:22 +0000 Subject: [PATCH 3/7] Fix the edit button in the panel of the widgets --- diff --git a/hubs/widgets/templates/panel.html b/hubs/widgets/templates/panel.html index ba6dff2..9af630b 100644 --- a/hubs/widgets/templates/panel.html +++ b/hubs/widgets/templates/panel.html @@ -1,12 +1,16 @@ -
- {{heading}} +
+ {{ heading }}
- - - + + + + + +
- {{content}} + {{ content }}
From 4d22eaa688fb8b2190203087edd2b483767f40e8 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Apr 14 2016 18:25:22 +0000 Subject: [PATCH 4/7] Show the flashed message in the hubs template --- diff --git a/hubs/templates/hubs.html b/hubs/templates/hubs.html index 3b04247..8cf43ba 100644 --- a/hubs/templates/hubs.html +++ b/hubs/templates/hubs.html @@ -111,6 +111,32 @@

{{hub.name}}

{% endif %}
+ +{%- with messages = get_flashed_messages(with_categories=true) -%} + {%- if category, messages -%} +
+
+
+
    + {%- for category, message in messages -%} + + {%- endfor -%} +
+
+
+
+ {%- endif -%} +{%- endwith -%} +
From 7ea1395c24147429400b4429dd45b850091dfab0 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Apr 14 2016 18:25:22 +0000 Subject: [PATCH 5/7] Implement configuring widgets directly in the UI --- diff --git a/hubs/app.py b/hubs/app.py index c57cdb4..75c53ed 100755 --- a/hubs/app.py +++ b/hubs/app.py @@ -156,20 +156,47 @@ def widget_json(hub, idx): @app.route('///edit', methods=['GET']) def widget_edit_get(hub, idx): widget = get_widget(session, hub, idx) - raise NotImplementedError('next step is to get the widgets to render ' - 'editable versions of themselves with their ' - 'declared @arguments, etc...') - return widget.render(session, edit=True) + if not widget.module.data.widget_arguments: + flask.abort(404, 'Nothing to configure for this hub.') + return flask.render_template( + 'edit.html', + hub=hub, + widget=widget, + url_to=flask.url_for('widget_edit_post', 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) - # TODO -- save things to the db ... and then redirect - widget.hub.last_edited = datetime.datetime.utcnow() - raise NotImplementedError('TODO - save changes to widget config') - flask.redirect(flask.url_for('hub', name=hub)) + widget = get_widget(session, hub, idx) + 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.config = config + widget.hub.last_edited = datetime.datetime.utcnow() + session.add(widget) + try: + session.commit() + except Exception as err: + flask.flash( + 'Could not save the configuration to the database '\ + 'if the error persists, please warn an admin', + 'error') + return flask.redirect(flask.url_for('hub', name=hub)) @app.route('/source//') diff --git a/hubs/templates/edit.html b/hubs/templates/edit.html new file mode 100644 index 0000000..f4c2718 --- /dev/null +++ b/hubs/templates/edit.html @@ -0,0 +1,35 @@ + + diff --git a/hubs/templates/hubs.html b/hubs/templates/hubs.html index 8cf43ba..d00c71c 100644 --- a/hubs/templates/hubs.html +++ b/hubs/templates/hubs.html @@ -202,31 +202,64 @@
+ + - From daf156eba7f17ee361dae2d30948091aacb7f8b8 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Apr 15 2016 08:41:04 +0000 Subject: [PATCH 6/7] Be a little more careful on how we update the widget configuration --- diff --git a/hubs/app.py b/hubs/app.py index 75c53ed..8fa873e 100755 --- a/hubs/app.py +++ b/hubs/app.py @@ -186,6 +186,8 @@ def widget_edit_post(hub, idx): flask.flash('Invalid data provided, error: %s' % err, 'error') error = True if not error: + cur_config = widget.config + cur_config.update(config) widget.config = config widget.hub.last_edited = datetime.datetime.utcnow() session.add(widget) From 3f0474bcb7558503c44dd133451b31b3c51c6a0f Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Apr 20 2016 08:33:11 +0000 Subject: [PATCH 7/7] Fix typo seen by @sayanchowdhury --- diff --git a/hubs/app.py b/hubs/app.py index 8fa873e..8fb079b 100755 --- a/hubs/app.py +++ b/hubs/app.py @@ -188,7 +188,7 @@ def widget_edit_post(hub, idx): if not error: cur_config = widget.config cur_config.update(config) - widget.config = config + widget.config = cur_config widget.hub.last_edited = datetime.datetime.utcnow() session.add(widget) try: