odoo.factory.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. angular.module('odoo')
  2. /**
  3. * -----------------------------------------------------------------------------
  4. * Description: Odoo connection manager
  5. * -----------------------------------------------------------------------------
  6. */
  7. .factory('odooFactory', function (
  8. $localStorage,
  9. xmlrpc,
  10. methodCallManager
  11. ) {
  12. var configuration = $localStorage;
  13. /**
  14. *
  15. */
  16. var auth = function (success, error) {
  17. xmlrpc.callMethod(methodCallManager.call('authenticate', configuration), [configuration.database, configuration.username, configuration.password, {}]).then(function (response) {
  18. if (!response || response['faultCode']) {
  19. return error(response);
  20. }
  21. success(response);
  22. }, function (xmlrpcError) {
  23. error(xmlrpcError);
  24. });
  25. }
  26. /**
  27. *
  28. */
  29. var read = function (model, domain, success, error) {
  30. xmlrpc.callMethod(methodCallManager.call('execute_kw', configuration), [configuration.database, configuration.id, configuration.password, model, 'search_read', [domain]]).then(function (response) {
  31. if (!response || response['faultCode']) {
  32. return error(response);
  33. }
  34. success(response);
  35. }, function (xmlrpcError) {
  36. error(xmlrpcError);
  37. });
  38. }
  39. /**
  40. *
  41. */
  42. var create = function (model, data, success, error) {
  43. xmlrpc.callMethod(methodCallManager.call('execute_kw', configuration), [configuration.database, configuration.id, configuration.password, model, 'create', [data]]).then(function (response) {
  44. if (!response || response['faultCode']) {
  45. return error(response);
  46. }
  47. success(response);
  48. }, function (xmlrpcError) {
  49. error(xmlrpcError);
  50. });
  51. }
  52. /**
  53. *
  54. */
  55. var write = function (model, id, data, success, error) {
  56. xmlrpc.callMethod(methodCallManager.call('execute_kw', configuration), [configuration.database, configuration.id, configuration.password, model, 'write', [[id], data]]).then(function (response) {
  57. if (!response || response['faultCode']) {
  58. return error(response);
  59. }
  60. success(response);
  61. }, function (xmlrpcError) {
  62. error(xmlrpcError);
  63. });
  64. }
  65. /**
  66. *
  67. */
  68. var unlink = function (model, id, success, error) {
  69. xmlrpc.callMethod(methodCallManager.call('execute_kw', configuration), [configuration.database, configuration.id, configuration.password, model, 'unlink', [[id]]]).then(function (response) {
  70. if (!response || response['faultCode']) {
  71. return error(response);
  72. }
  73. success(response);
  74. }, function (xmlrpcError) {
  75. error(xmlrpcError);
  76. });
  77. }
  78. /**
  79. *
  80. */
  81. var check = function (model, operation, success, error) {
  82. xmlrpc.callMethod(methodCallManager.call('execute_kw', configuration), [configuration.database, configuration.id, configuration.password, model, 'check_access_rights', [operation], { 'raise_exception': false }]).then(function (response) {
  83. if (!response || response['faultCode']) {
  84. return error(response);
  85. }
  86. success(response);
  87. }, function (xmlrpcError) {
  88. error(xmlrpcError);
  89. });
  90. }
  91. /**
  92. *
  93. */
  94. var fields = function (model, success, error) {
  95. xmlrpc.callMethod(methodCallManager.call('execute_kw', configuration), [configuration.database, configuration.id, configuration.password, model, 'fields_get', [], { 'attributes': ['type', 'readonly', 'required', 'relation', 'relation_field'] }]).then(function (response) {
  96. if (!response || response['faultCode']) {
  97. return error(response);
  98. }
  99. success(response);
  100. }, function (xmlrpcError) {
  101. error(xmlrpcError);
  102. });
  103. }
  104. return {
  105. auth: auth,
  106. read: read,
  107. create: create,
  108. write: write,
  109. unlink: unlink,
  110. check: check,
  111. fields: fields
  112. }
  113. })
  114. /**
  115. *
  116. */
  117. .factory('methodCallManager', function (xmlrpc) {
  118. return {
  119. call: function (methodName, configuration) {
  120. var hostName = configuration.host + ':' + configuration.port;
  121. if (!hostName.startsWith('http://')) {
  122. hostName = 'http://' + hostName;
  123. }
  124. if (methodName == 'authenticate') {
  125. xmlrpc.config({ hostName: hostName, pathName: '/xmlrpc/2/common' });
  126. } else {
  127. xmlrpc.config({ hostName: hostName, pathName: '/xmlrpc/2/object' });
  128. }
  129. return methodName;
  130. }
  131. }
  132. });