From 8d871d1dd3fa657410f68371c7398d089adcdc31 Mon Sep 17 00:00:00 2001 From: Adrian Moennich Date: Fri, 24 Feb 2017 10:29:02 +0100 Subject: [PATCH] LiveSync: Ajaxify & improve agent management --- livesync/indico_livesync/controllers.py | 20 ++++--- .../indico_livesync/templates/edit_agent.html | 23 +------- .../templates/plugin_details_extra.html | 58 +++++++++++++++---- livesync/indico_livesync/views.py | 24 -------- 4 files changed, 62 insertions(+), 63 deletions(-) delete mode 100644 livesync/indico_livesync/views.py diff --git a/livesync/indico_livesync/controllers.py b/livesync/indico_livesync/controllers.py index 307a65d..db2802c 100644 --- a/livesync/indico_livesync/controllers.py +++ b/livesync/indico_livesync/controllers.py @@ -18,16 +18,16 @@ from __future__ import unicode_literals from flask import request, redirect, flash from flask_pluginengine import render_plugin_template, current_plugin +from werkzeug.exceptions import NotFound from indico.core.db import db from indico.modules.admin import RHAdminBase from indico.web.flask.util import url_for from indico.web.forms.base import FormDefaults -from indico.web.util import jsonify_data +from indico.web.util import jsonify_data, jsonify_template from indico_livesync import _ from indico_livesync.models.agents import LiveSyncAgent -from indico_livesync.views import WPLiveSync def extend_plugin_details(): @@ -52,7 +52,10 @@ class RHAddAgent(RHAdminBase): def _checkParams(self): self.backend_name = request.view_args['backend'] - self.backend = current_plugin.backend_classes[self.backend_name] + try: + self.backend = current_plugin.backend_classes[self.backend_name] + except KeyError: + raise NotFound def _process(self): form = self.backend.form(obj=FormDefaults(name=self.backend.title)) @@ -63,9 +66,9 @@ class RHAddAgent(RHAdminBase): db.session.add(agent) flash(_('Agent added'), 'success') flash(_("Don't forget to run the initial export!"), 'highlight') - return redirect(url_for('plugins.details', plugin='livesync')) + return jsonify_data(flash=False) - return WPLiveSync.render_template('edit_agent.html', form=form, backend=self.backend) + return jsonify_template('edit_agent.html', render_plugin_template, form=form, backend=self.backend, edit=False) class RHEditAgent(RHAdminBase): @@ -78,12 +81,13 @@ class RHEditAgent(RHAdminBase): return redirect(url_for('plugins.details', plugin='livesync')) def _process(self): - form = self.agent.backend.form(obj=FormDefaults(self.agent, {'name'}, **self.agent.settings)) + form = self.agent.backend.form(obj=FormDefaults(name=self.agent.name, **self.agent.settings)) if form.validate_on_submit(): data = form.data self.agent.name = data.pop('name') self.agent.settings = data flash(_('Agent updated'), 'success') - return redirect(url_for('plugins.details', plugin='livesync')) + return jsonify_data(flash=False) - return WPLiveSync.render_template('edit_agent.html', form=form, backend=self.agent.backend, agent=self.agent) + return jsonify_template('edit_agent.html', render_plugin_template, form=form, backend=self.agent.backend, + edit=True) diff --git a/livesync/indico_livesync/templates/edit_agent.html b/livesync/indico_livesync/templates/edit_agent.html index 9cee2d8..27af138 100644 --- a/livesync/indico_livesync/templates/edit_agent.html +++ b/livesync/indico_livesync/templates/edit_agent.html @@ -1,27 +1,10 @@ -{% extends 'layout/base.html' %} -{% from 'forms/_form.html' import form_header, form_row, form_footer %} - -{% block title %}LiveSync{% endblock %} - -{% block subtitle %} - {%- if agent -%} - {% trans %}Edit Agent{% endtrans %} - {%- else -%} - {% trans backend=backend.title %}Add {{ backend }} agent{% endtrans %} - {%- endif -%} -{% endblock %} +{% extends 'layout/dialog_base.html' %} +{% from 'forms/_form.html' import simple_form %} {% block description %} {{ backend.description }} {% endblock %} {% block content %} - {{ form_header(form) }} - {% for field in form.visible_fields %} - {{ form_row(field) }} - {% endfor %} - {% call form_footer(form) %} - - {% trans %}Cancel{% endtrans %} - {% endcall %} + {{ simple_form(form, back=_('Cancel'), disabled_until_change=edit) }} {% endblock %} diff --git a/livesync/indico_livesync/templates/plugin_details_extra.html b/livesync/indico_livesync/templates/plugin_details_extra.html index 666f24d..1db3158 100644 --- a/livesync/indico_livesync/templates/plugin_details_extra.html +++ b/livesync/indico_livesync/templates/plugin_details_extra.html @@ -1,3 +1,20 @@ +{% from 'message_box.html' import message_box %} + +{% macro add_agent_link(name, backend, standalone=false) %} + + {% if standalone %} + {% trans backend=backend.title %}Add {{ backend }} agent{% endtrans %} + {% else %} + {{ backend.title }} + {% endif %} + +{% endmacro %} +

{% trans %}LiveSync Agents{% endtrans %}

@@ -50,7 +67,11 @@ data-reload-after data-ajax> {%- if agent.backend -%} - + {%- endif -%} @@ -58,14 +79,28 @@ {% else %} -
-
{% trans %}No agents have been added yet.{% endtrans %}
-
+ {% call message_box('info') %} + {% trans %}No agents have been added yet.{% endtrans %} + {% endcall %} {% endif %} - {% for name, backend in backends.items() | sort(attribute='1.title') %} - {% trans backend=backend.title %}Add {{ backend }} agent{% endtrans %} - {% endfor %} +
+ {% if backends|length == 1 %} + {% set name, backend = backends.viewitems()|first %} + {{ add_agent_link(name, backend, standalone=true) }} + {% else %} + + {% trans %}Add{% endtrans %} + + + {% endif %} +
{% set missing_initial_export = agents|rejectattr('initial_data_exported')|list %} {% if missing_initial_export %} @@ -76,10 +111,11 @@ {%- endtrans %}


-{#- Don't "fix" the indentation of these lines! -#}
-{%- for agent in missing_initial_export -%}
-indico livesync initial_export {{ agent.id }}
-{% endfor -%}
+            {%- filter dedent %}
+                {%- for agent in missing_initial_export -%}
+                    indico livesync initial_export {{ agent.id }}
+                {% endfor -%}
+            {% endfilter -%}
         
{% endif %}
diff --git a/livesync/indico_livesync/views.py b/livesync/indico_livesync/views.py deleted file mode 100644 index 4809528..0000000 --- a/livesync/indico_livesync/views.py +++ /dev/null @@ -1,24 +0,0 @@ -# This file is part of Indico. -# Copyright (C) 2002 - 2017 European Organization for Nuclear Research (CERN). -# -# Indico is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License as -# published by the Free Software Foundation; either version 3 of the -# License, or (at your option) any later version. -# -# Indico is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Indico; if not, see . - -from __future__ import unicode_literals - -from indico.core.plugins import WPJinjaMixinPlugin -from indico.core.plugins.views import WPPlugins - - -class WPLiveSync(WPJinjaMixinPlugin, WPPlugins): - pass