Added system API to client lib and migrated users to it

This commit is contained in:
Gina Häußge 2015-10-01 14:57:41 +02:00
parent 8875f257ea
commit 085ab77c57
5 changed files with 51 additions and 34 deletions

View file

@ -580,25 +580,19 @@ $(function() {
showConfirmationDialog({
message: gettext("This will restart your OctoPrint server."),
onproceed: function() {
$.ajax({
url: self.restartCommandSpec.resource,
type: "POST",
dataType: "json",
data: "{}",
contentType: "application/json; charset=UTF-8",
success: function() {
OctoPrint.system.executeCommand("core", "restart")
.done(function() {
new PNotify({
title: gettext("Restart in progress"),
text: gettext("The server is now being restarted in the background")
})
},
error: function() {
})
.fail(function() {
new PNotify({
title: gettext("Something went wrong"),
text: gettext("Trying to restart the server produced an error, please check octoprint.log for details. You'll have to restart manually.")
})
}
});
});
}
});
}

View file

@ -926,6 +926,7 @@ class Server(object):
"js/app/client/printerprofiles.js",
"js/app/client/settings.js",
"js/app/client/slicing.js",
"js/app/client/system.js",
"js/app/client/timelapse.js",
"js/app/client/users.js",
"js/app/client/util.js",

View file

@ -39,10 +39,6 @@
script: script,
context: context
}, opts);
},
executeSystemCommand: function (action, opts) {
return OctoPrint.postJson("api/system", {action: action}, opts);
}
}
});

View file

@ -0,0 +1,24 @@
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["OctoPrint"], factory);
} else {
factory(window.OctoPrint);
}
})(window || this, function(OctoPrint) {
var url = "api/system";
var commandUrl = "api/system/commands";
OctoPrint.system = {
getCommands: function (opts) {
return OctoPrint.get(commandUrl, opts);
},
getCommandsForSource: function (source, opts) {
return OctoPrint.get(commandUrl + "/" + source, opts);
},
executeCommand: function (source, action, opts) {
return OctoPrint.postJson(commandUrl + "/" + source + "/" + action, {}, opts);
}
};
});

View file

@ -13,15 +13,11 @@ $(function() {
self.requestCommandData = function() {
if (!self.loginState.isAdmin()) {
return;
return $.Deferred().reject().promise();
}
$.ajax({
url: API_BASEURL + "system/commands",
type: "GET",
dataType: "json",
success: self.fromCommandResponse
});
return OctoPrint.system.getCommands()
.done(self.fromCommandResponse);
};
self.fromCommandResponse = function(response) {
@ -44,35 +40,41 @@ $(function() {
};
self.triggerCommand = function(commandSpec) {
var deferred = $.Deferred();
var callback = function() {
$.ajax({
url: commandSpec.resource,
type: "POST",
dataType: "json",
data: "{}",
contentType: "application/json; charset=UTF-8",
success: function() {
OctoPrint.system.executeCommand(commandSpec.actionSource, commandSpec.action)
.done(function() {
new PNotify({title: "Success", text: _.sprintf(gettext("The command \"%(command)s\" executed successfully"), {command: commandSpec.name}), type: "success"});
},
error: function(jqXHR, textStatus, errorThrown) {
deferred.resolve(["success", arguments]);
})
.fail(function(jqXHR, textStatus, errorThrown) {
if (!commandSpec.hasOwnProperty("ignore") || !commandSpec.ignore) {
var error = "<p>" + _.sprintf(gettext("The command \"%(command)s\" could not be executed."), {command: commandSpec.name}) + "</p>";
error += pnotifyAdditionalInfo("<pre>" + jqXHR.responseText + "</pre>");
new PNotify({title: gettext("Error"), text: error, type: "error", hide: false});
deferred.reject(["error", arguments]);
} else {
deferred.resolve(["ignored", arguments]);
}
}
})
});
};
if (commandSpec.confirm) {
showConfirmationDialog({
message: commandSpec.confirm,
onproceed: function(e) {
onproceed: function() {
callback();
},
oncancel: function() {
deferred.reject("cancelled", arguments);
}
});
} else {
callback();
}
return deferred.promise();
};
self.onUserLoggedIn = function(user) {