odoo.factory.js 5.0 KB

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