odoo.factory.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. angular.module('odoo')
  2. .factory('odooFactory', 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. check: function (model, operation, success, error) {
  60. xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.remote_id, config.password, model, 'check_access_rights', [operation], { 'raise_exception': false }]).then(function (response) {
  61. if (!response || response['faultCode']) {
  62. return error(response);
  63. }
  64. success(response);
  65. }, function (xmlrpcError) {
  66. error(xmlrpcError);
  67. });
  68. },
  69. fields: function (model, success, error) {
  70. xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.remote_id, config.password, model, 'fields_get', [], { 'attributes': ['string', 'type'] }]).then(function (response) {
  71. if (!response || response['faultCode']) {
  72. return error(response);
  73. }
  74. success(response);
  75. }, function (xmlrpcError) {
  76. error(xmlrpcError);
  77. });
  78. }
  79. }
  80. })
  81. .factory('methodCallManager', function (xmlrpc) {
  82. return {
  83. call: function (methodName, configuration) {
  84. var hostName = configuration.host + ':' + configuration.port;
  85. if (!hostName.startsWith('http://')) {
  86. hostName = 'http://' + hostName;
  87. }
  88. if (methodName == 'authenticate') {
  89. xmlrpc.config({ hostName: hostName, pathName: '/xmlrpc/2/common' });
  90. } else {
  91. xmlrpc.config({ hostName: hostName, pathName: '/xmlrpc/2/object' });
  92. }
  93. return methodName;
  94. }
  95. }
  96. });