odoo.factory.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. angular.module('odoo')
  2. .factory('odoo', function (xmlrpc, methodCallManager) {
  3. return {
  4. auth: function (config, success, error) {
  5. xmlrpc.callMethod(methodCallManager.call('authenticate', config), [config.database, config.username, config.password, {}]).then(function(response) {
  6. if (!response || response['faultCode']) {
  7. error(response);
  8. return;
  9. }
  10. success({ id: response, username: config.username, password: config.password });
  11. }, function (xmlrpcError) {
  12. error(xmlrpcError);
  13. });
  14. },
  15. read: function (model, domain, config, success, error) {
  16. xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.remote_id, config.password, model, 'search_read', [domain]]).then(function (response) {
  17. if (!response || response['faultCode']) {
  18. error(response);
  19. return;
  20. }
  21. success(response);
  22. }, function (xmlrpcError) {
  23. error(xmlrpcError);
  24. });
  25. },
  26. create: function (model, data, config, success, error) {
  27. xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.remote_id, config.password, model, 'create', [data]]).then(function (response) {
  28. if (!response || response['faultCode']) {
  29. error(response);
  30. return;
  31. }
  32. success(response);
  33. }, function (xmlrpcError) {
  34. error(xmlrpcError);
  35. });
  36. },
  37. write: function (model, id, data, config, success, error) {
  38. xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.remote_id, config.password, model, 'write', [[id], data]]).then(function (response) {
  39. if (!response || response['faultCode']) {
  40. error(response);
  41. return;
  42. }
  43. success(response);
  44. }, function (xmlrpcError) {
  45. error(xmlrpcError);
  46. });
  47. },
  48. unlink: function (model, id, config, success, error) {
  49. xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.remote_id, config.password, model, 'unlink', [[id]]]).then(function (response) {
  50. if (!response || response['faultCode']) {
  51. error(response);
  52. return;
  53. }
  54. success(response);
  55. }, function (xmlrpcError) {
  56. error(xmlrpcError);
  57. });
  58. }
  59. }
  60. })
  61. .factory('methodCallManager', function(xmlrpc) {
  62. return {
  63. call: function(methodName, configuration) {
  64. var hostName = configuration.host + ':' + configuration.port;
  65. if(!hostName.startsWith('http://')) {
  66. hostName = 'http://' + hostName;
  67. }
  68. if(methodName == 'authenticate') {
  69. xmlrpc.config({ hostName: hostName, pathName: '/xmlrpc/2/common' });
  70. } else {
  71. xmlrpc.config({ hostName: hostName, pathName: '/xmlrpc/2/object' });
  72. }
  73. return methodName;
  74. }
  75. }
  76. });