Improve API and request error handling

This commit is contained in:
Alejandro Avilés 2014-10-29 09:09:27 +01:00
parent b9ae7b124a
commit 0a6a756f73
3 changed files with 17 additions and 29 deletions

View File

@ -16,30 +16,21 @@
from __future__ import unicode_literals
from flask import jsonify
from flask import jsonify, request
from flask_pluginengine import current_plugin
from MaKaC.webinterface.rh.base import RHProtected
class RHDataImport(RHProtected):
"""
Fetches data from the specified importer plugin.
Arguments:
importer - name of an importer plugin being used
query - string used in importer's search phrase
size - number of returned queries
"""
def process(self, params):
importer = current_plugin.importer_engines.get(params['importer_name'])
if not importer:
return jsonify(dict(success=False))
query = params.get('query')
size = params.get('size', 10)
return importer.import_data(query, size)
class RHGetImporters(RHProtected):
def process(self, params):
def _process(self):
importers = {k: v.name for k, v in current_plugin.importer_engines.iteritems()}
return jsonify(importers)
class RHImportData(RHProtected):
def _process(self):
size = request.form.get('size', 10)
query = request.form.get('query')
importer = current_plugin.importer_engines.get(request.view_args['importer_name'])
return importer.import_data(query, size)

View File

@ -20,7 +20,7 @@ from indico.core import signals
from indico.core.plugins import IndicoPlugin, IndicoPluginBlueprint, plugin_url_rule_to_js
from MaKaC.webinterface.pages.conferences import WPConfModifScheduleGraphic
from .controllers import RHDataImport, RHGetImporters
from .controllers import RHGetImporters, RHImportData
class ImporterPlugin(IndicoPlugin):
@ -58,5 +58,5 @@ class ImporterPlugin(IndicoPlugin):
blueprint = IndicoPluginBlueprint('importer', __name__)
blueprint.add_url_rule('/import/<importer_name>', 'import_data', RHDataImport, methods=('GET', 'POST'))
blueprint.add_url_rule('/importers', 'importers', RHGetImporters, methods=('GET', 'POST'))
blueprint.add_url_rule('/importers/<importer_name>/search', 'import_data', RHImportData, methods=('POST',))
blueprint.add_url_rule('/importers/', 'importers', RHGetImporters)

View File

@ -218,7 +218,7 @@ type("ImportDialog", ["ExclusivePopupWithButtons", "PreLoadHandler"], {
var self = this;
$.ajax({
url: build_url(ImporterPlugin.urls.importers, {}),
type: 'POST',
type: 'GET',
dataType: 'json',
success: function(data) {
if (handleAjaxError(data)) {
@ -652,7 +652,6 @@ type("ImporterListWidget", ["SelectableListWidget"], {
*/
_searchBase: function(query, importer, size, successFunc, callbacks) {
var self = this;
var killProgress = IndicoUI.Dialogs.Util.progress();
$.ajax({
// One more entry is fetched to be able to check if it's possible to fetch
// more entries in case of further requests.
@ -661,17 +660,15 @@ type("ImporterListWidget", ["SelectableListWidget"], {
'size': size + 1}),
type: 'POST',
dataType: 'json',
complete: IndicoUI.Dialogs.Util.progress(),
success: function(data) {
if (handleAjaxError(data)) {
return;
}
if (data.success) {
successFunc(data.result);
}
each(callbacks, function(callback) {
successFunc(data.result);
_.each(callbacks, function(callback) {
callback();
});
killProgress();
}
});
//Saves last request data