LiveSync: Fix (csrf check) & ajaxify agent deletion

This commit is contained in:
Adrian Moennich 2017-02-23 19:17:09 +01:00
parent 6557434695
commit 149154f920
5 changed files with 14 additions and 40 deletions

View File

@ -23,5 +23,5 @@ from indico_livesync.controllers import RHAddAgent, RHEditAgent, RHDeleteAgent
blueprint = IndicoPluginBlueprint('livesync', 'indico_livesync', url_prefix='/admin/plugins/livesync')
blueprint.add_url_rule('/agents/create/<backend>', 'add_agent', RHAddAgent, methods=('GET', 'POST'))
blueprint.add_url_rule('/agents/<int:agent_id>/', 'edit_agent', RHEditAgent, methods=('GET', 'POST'))
blueprint.add_url_rule('/agents/<int:agent_id>/delete', 'delete_agent', RHDeleteAgent, methods=('POST',))
blueprint.add_url_rule('/agents/<int:agent_id>', 'edit_agent', RHEditAgent, methods=('GET', 'POST'))
blueprint.add_url_rule('/agents/<int:agent_id>', 'delete_agent', RHDeleteAgent, methods=('DELETE',))

View File

@ -20,9 +20,10 @@ from flask import request, redirect, flash
from flask_pluginengine import render_plugin_template, current_plugin
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 MaKaC.webinterface.rh.admins import RHAdminBase
from indico.web.util import jsonify_data
from indico_livesync import _
from indico_livesync.models.agents import LiveSyncAgent
@ -38,12 +39,12 @@ class RHDeleteAgent(RHAdminBase):
"""Deletes a LiveSync agent"""
def _checkParams(self):
self.agent = LiveSyncAgent.find_one(id=request.view_args['agent_id'])
self.agent = LiveSyncAgent.get_one(request.view_args['agent_id'])
def _process(self):
db.session.delete(self.agent)
flash(_('Agent deleted'), 'success')
return redirect(url_for('plugins.details', plugin='livesync'))
return jsonify_data(flash=False)
class RHAddAgent(RHAdminBase):
@ -71,7 +72,7 @@ class RHEditAgent(RHAdminBase):
"""Edits a LiveSync agent"""
def _checkParams(self):
self.agent = LiveSyncAgent.find_one(id=request.view_args['agent_id'])
self.agent = LiveSyncAgent.get_one(request.view_args['agent_id'])
if self.agent.backend is None:
flash(_('Cannot edit an agent that is not loaded'), 'error')
return redirect(url_for('plugins.details', plugin='livesync'))

View File

@ -16,13 +16,10 @@
from __future__ import unicode_literals
from flask import request
from wtforms.validators import NumberRange
from wtforms.fields.html5 import IntegerField
from indico.core.plugins import IndicoPlugin, PluginCategory, wrap_cli_manager
from indico.core.plugins.views import WPPlugins
from indico.web.forms.base import IndicoForm
from indico.web.forms.fields import MultipleItemsField
@ -63,15 +60,10 @@ class LiveSyncPlugin(IndicoPlugin):
self.backend_classes = {}
connect_signals(self)
self.template_hook('plugin-details', self._extend_plugin_details)
self.inject_js('livesync_admin_js', WPPlugins, subclasses=False,
condition=lambda: request.view_args.get('plugin') == self.name)
def get_blueprints(self):
return blueprint
def register_assets(self):
self.register_js_bundle('livesync_admin_js', 'js/livesync_admin.js')
def add_cli_command(self, manager):
manager.add_command('livesync', wrap_cli_manager(cli_manager, self))

View File

@ -1,20 +0,0 @@
(function(global) {
var $t = $T.domain('livesync');
global.liveSyncPluginPage = function liveSyncPluginPage() {
$('.js-delete-agent').on('click', function(e) {
e.preventDefault();
var $this = $(this);
var msg = $t.gettext('Do you really want to delete this agent and all its queue entries?');
new ConfirmPopup($t.gettext('Delete this agent?'), msg, function(confirmed) {
if (!confirmed) {
return;
}
$('<form>', {
action: $this.data('href'),
method: 'post'
}).appendTo('body').submit();
}).open();
});
};
})(window);

View File

@ -42,8 +42,13 @@
</td>
<td class="text-right">{{ agent.queue.filter_by(processed=false).count() }}</td>
<td>
<a href="#" class="action-icon icon-remove js-delete-agent"
data-href="{{ url_for_plugin('livesync.delete_agent', agent) }}"></a>
<a href="#" class="action-icon icon-remove"
data-href="{{ url_for_plugin('livesync.delete_agent', agent) }}"
data-method="DELETE"
data-title="{% trans %}Delete this agent?{% endtrans %}"
data-confirm="{% trans %}Do you really want to delete this agent and all its queue entries?{% endtrans %}"
data-reload-after
data-ajax></a>
{%- if agent.backend -%}
<a class="action-icon icon-edit" href="{{ url_for_plugin('livesync.edit_agent', agent) }}"></a>
{%- endif -%}
@ -78,7 +83,3 @@ indico livesync initial_export {{ agent.id }}
</code></pre>
{% endif %}
</div>
<script>
liveSyncPluginPage();
</script>