From 6f5ec9ce4474059b03e516b0860e5cfcb06c7ed9 Mon Sep 17 00:00:00 2001 From: niv-openerp Date: Wed, 16 Jan 2013 15:53:56 +0100 Subject: [PATCH] Added client lib bzr revid: nicolas.vanhoren@openerp.com-20130116145356-piha174p7qx1ggzj --- .../static/ext/static/js/livesupport.js | 129 +++++++++++++++++- addons/live_support/static/ext/test.html | 22 +++ 2 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 addons/live_support/static/ext/test.html diff --git a/addons/live_support/static/ext/static/js/livesupport.js b/addons/live_support/static/ext/static/js/livesupport.js index d9a5e856f07..c77b7a2019c 100644 --- a/addons/live_support/static/ext/static/js/livesupport.js +++ b/addons/live_support/static/ext/static/js/livesupport.js @@ -2,9 +2,134 @@ define(["nova", "jquery", "underscore"], function(nova, $, _) { var livesupport = {}; - livesupport.main = function(server_url) { - + livesupport.main = function(server_url, db, login, password) { + console.log("hello"); + var connection = new Connection(new JsonRPCConnector(server_url), db, login, password); + var userModel = connection.getModel("res.users"); + userModel.call("search", [[["login", "=", "admin"]]]).then(function(result) { + console.log(result); + }); }; + function jsonRpc(url, fct_name, params, settings) { + var data = { + jsonrpc: "2.0", + method: fct_name, + params: params, + id: Math.floor(Math.random()* (1000*1000*1000)), + }; + return $.ajax(url, _.extend({}, settings, { + url: url, + dataType: 'json', + type: 'POST', + data: JSON.stringify(data), + contentType: 'application/json', + })).pipe(function(result) { + if (result.error !== undefined) { + console.error("Server application error", result.error); + return $.Deferred().reject("server", result.error); + } else { + return result.result; + } + }, function() { + console.error("JsonRPC communication error", _.toArray(arguments)); + var def = $.Deferred(); + return def.reject.apply(def, ["communication"].concat(_.toArray(arguments))); + }); + }; + + var JsonRPCConnector = nova.Class.$extend({ + __init__: function(url) { + this.url = url + "/jsonrpc"; + }, + getService: function(serviceName) { + return new Service(this, serviceName); + }, + send: function(serviceName, method, args) { + return jsonRpc(this.url, "call", {"service": serviceName, "method": method, "args": args}); + }, + }); + + var Service = nova.Class.$extend({ + __init__: function(connector, serviceName) { + this.connector = connector; + this.serviceName = serviceName; + }, + call: function(method, args) { + return this.connector.send(this.serviceName, method, args); + }, + }); + + var AuthenticationError = nova.Error.$extend({ + name: "AuthenticationError", + defaultMessage: "An error occured during authentication." + }); + + var Connection = nova.Class.$extend({ + __init__: function(connector, database, login, password, userId) { + this.connector = connector; + this.setLoginInfo(database, login, password, userId); + this.userContext = null; + }, + setLoginInfo: function(database, login, password, userId) { + this.database = database; + this.login = login; + this.password = password; + this.userId = userId; + }, + checkLogin: function(force) { + force = force === undefined ? true: force; + if (this.userId && ! force) + return $.when(); + + if (! this.database || ! this.login || ! this.password) + throw new AuthenticationError(); + + return this.getService("common").call("login", [this.database, this.login, this.password]) + .then(_.bind(function(result) { + this.userId = result; + if (! this.userId) { + console.error("Authentication failure"); + return $.Deferred().reject({message:"Authentication failure"}); + } + }, this)); + }, + getUserContext: function() { + if (! this.userContext) { + return this.getModel("res.users").call("context_get").then(_.bind(function(result) { + this.userContext = result; + return this.userContext; + }, this)); + } + return $.when(this.userContext); + }, + getModel: function(modelName) { + return new Model(this, modelName); + }, + getService: function(serviceName) { + return this.connector.getService(serviceName); + }, + }); + + var Model = nova.Class.$extend({ + __init__: function(connection, modelName) { + this.connection = connection; + this.modelName = modelName; + }, + call: function(method, args, kw) { + return this.connection.checkLogin().then(_.bind(function() { + return this.connection.getService("object").call("execute_kw", [ + this.connection.database, + this.connection.userId, + this.connection.password, + this.modelName, + method, + args || [], + kw || {}, + ]); + }, this)); + }, + }); + return livesupport; }); diff --git a/addons/live_support/static/ext/test.html b/addons/live_support/static/ext/test.html new file mode 100644 index 00000000000..9f944ed6f2a --- /dev/null +++ b/addons/live_support/static/ext/test.html @@ -0,0 +1,22 @@ + + + + + + + + + + \ No newline at end of file