You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.5 KiB

  1. # coding: utf-8
  2. # Copyright (C) 2015 Sébastien BEAU <sebastien.beau@akretion.com>
  3. # Valentin CHEMIERE <valentin.chemiere@akretion.com>
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. from odoo import api, models, fields
  6. from ..models.keychain import OVH_KEYCHAIN_NAMESPACE
  7. import requests
  8. import logging
  9. _logger = logging.getLogger(__name__)
  10. class SmsClient(models.Model):
  11. _inherit = "sms.gateway"
  12. method = fields.Selection(selection_add=[('http_ovh', 'OVH HTTP')])
  13. @api.multi
  14. def _provider_get_provider_conf(self):
  15. for rec in self:
  16. keychain = rec.env['keychain.account']
  17. if rec._check_permissions():
  18. retrieve = keychain.suspend_security().retrieve
  19. else:
  20. retrieve = keychain.retrieve
  21. accounts = retrieve(
  22. [['namespace', '=', OVH_KEYCHAIN_NAMESPACE]])
  23. return accounts[0]
  24. class SmsSms(models.Model):
  25. _inherit = "sms.sms"
  26. @api.model
  27. def _prepare_http_ovh(self):
  28. keychain_account = self.gateway_id._provider_get_provider_conf()
  29. keychain_data = keychain_account.get_data()
  30. params = {
  31. 'smsAccount': keychain_data['sms_account'],
  32. 'login': keychain_account['login'],
  33. 'password': keychain_account.get_password(),
  34. 'from': self.gateway_id.from_provider,
  35. 'url': self.gateway_id.url,
  36. 'to': self._convert_to_e164(self.mobile),
  37. 'message': self.message,
  38. }
  39. if self.nostop:
  40. params['noStop'] = 1
  41. if self.deferred:
  42. params['deferred'] = self.deferred
  43. if self.classes:
  44. params['class'] = self.classes
  45. if self.tag:
  46. params['tag'] = self.tag
  47. if self.coding:
  48. params['smsCoding'] = self.coding
  49. return params
  50. @api.model
  51. def _convert_to_e164(self, erp_number):
  52. to_dial_number = erp_number.replace(u'\xa0', u'')
  53. return to_dial_number
  54. @api.multi
  55. def _send_http_ovh(self):
  56. self.ensure_one()
  57. params = self._prepare_http_ovh()
  58. r = requests.get(params['url'], params=params.items())
  59. params.update({
  60. 'password': '*****',
  61. 'to': '*****',
  62. 'smsAccount': '*****',
  63. 'login': '*****',
  64. })
  65. _logger.debug("Call OVH API : %s params %s",
  66. params['url'], params)
  67. response = r.text
  68. if response[0:2] != 'OK':
  69. raise ValueError(response)