odoo.factory.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. return error(response);
  8. }
  9. success(response);
  10. }, function (xmlrpcError) {
  11. error(xmlrpcError);
  12. });
  13. },
  14. read: function (model, domain, config, success, error) {
  15. xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.id, config.password, model, 'search_read', [domain]]).then(function (response) {
  16. if (!response || response['faultCode']) {
  17. return error(response);
  18. }
  19. success(response);
  20. }, function (xmlrpcError) {
  21. error(xmlrpcError);
  22. });
  23. },
  24. create: function (model, data, config, success, error) {
  25. xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.id, config.password, model, 'create', [data]]).then(function (response) {
  26. if (!response || response['faultCode']) {
  27. return error(response);
  28. }
  29. success(response);
  30. }, function (xmlrpcError) {
  31. error(xmlrpcError);
  32. });
  33. },
  34. write: function (model, id, data, config, success, error) {
  35. xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.id, config.password, model, 'write', [[id], data]]).then(function (response) {
  36. if (!response || response['faultCode']) {
  37. return error(response);
  38. }
  39. success(response);
  40. }, function (xmlrpcError) {
  41. error(xmlrpcError);
  42. });
  43. },
  44. unlink: function (model, id, config, success, error) {
  45. xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.id, config.password, model, 'unlink', [[id]]]).then(function (response) {
  46. if (!response || response['faultCode']) {
  47. return error(response);
  48. }
  49. success(response);
  50. }, function (xmlrpcError) {
  51. error(xmlrpcError);
  52. });
  53. },
  54. check: function (model, operation, success, error) {
  55. xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.id, config.password, model, 'check_access_rights', [operation], { 'raise_exception': false }]).then(function (response) {
  56. if (!response || response['faultCode']) {
  57. return error(response);
  58. }
  59. success(response);
  60. }, function (xmlrpcError) {
  61. error(xmlrpcError);
  62. });
  63. },
  64. fields: function (model, config, success, error) {
  65. xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.id, config.password, model, 'fields_get', [], { 'attributes': ['type', 'readonly', 'required', 'relation', 'relation_field'] }]).then(function (response) {
  66. if (!response || response['faultCode']) {
  67. return error(response);
  68. }
  69. success(response);
  70. }, function (xmlrpcError) {
  71. error(xmlrpcError);
  72. });
  73. }
  74. }
  75. })
  76. .factory('methodCallManager', function (xmlrpc) {
  77. return {
  78. call: function (methodName, configuration) {
  79. var hostName = configuration.host + ':' + configuration.port;
  80. if (!hostName.startsWith('http://')) {
  81. hostName = 'http://' + hostName;
  82. }
  83. if (methodName == 'authenticate') {
  84. xmlrpc.config({ hostName: hostName, pathName: '/xmlrpc/2/common' });
  85. } else {
  86. xmlrpc.config({ hostName: hostName, pathName: '/xmlrpc/2/object' });
  87. }
  88. return methodName;
  89. }
  90. }
  91. });