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.

77 lines
2.5 KiB

6 years ago
  1. # Copyright (C) 2015 Sébastien BEAU <sebastien.beau@akretion.com>
  2. # Valentin CHEMIERE <valentin.chemiere@akretion.com>
  3. # Copyright (C) 2019 Eficent Business and IT Consulting Services, S.L.
  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 CLICKSEND_KEYCHAIN_NAMESPACE
  7. import requests
  8. import logging
  9. from xml.etree.ElementTree import fromstring
  10. _logger = logging.getLogger(__name__)
  11. class SmsClient(models.Model):
  12. _inherit = "sms.gateway"
  13. method = fields.Selection(selection_add=[
  14. ('http_clicksend', 'ClickSend HTTP')])
  15. @api.multi
  16. def _provider_get_provider_conf(self):
  17. for rec in self:
  18. keychain = rec.env['keychain.account']
  19. # TODO: implement suspend_security module
  20. # if rec._check_permissions():
  21. # retrieve = keychain.suspend_security().retrieve
  22. # else:
  23. # retrieve = keychain.retrieve
  24. retrieve = keychain.retrieve
  25. accounts = retrieve(
  26. [['namespace', '=', CLICKSEND_KEYCHAIN_NAMESPACE]])
  27. return accounts[0]
  28. class SmsSms(models.Model):
  29. _inherit = "sms.sms"
  30. @api.model
  31. def _prepare_http_clicksend(self):
  32. keychain_account = self.gateway_id._provider_get_provider_conf()
  33. params = {
  34. 'username': keychain_account['login'],
  35. 'key': keychain_account._get_password(),
  36. 'senderid': self.gateway_id.from_provider,
  37. 'url': self.gateway_id.url,
  38. 'to': self._convert_to_e164(self.mobile),
  39. 'message': self.message,
  40. }
  41. return params
  42. @api.model
  43. def _convert_to_e164(self, erp_number):
  44. to_dial_number = erp_number.replace(u'\xa0', u'')
  45. return to_dial_number
  46. @api.multi
  47. def _send_http_clicksend(self):
  48. self.ensure_one()
  49. params = self._prepare_http_clicksend()
  50. r = requests.get(params['url'], params=params.items())
  51. params.update({
  52. 'password': '*****',
  53. 'to': '*****',
  54. 'username': '*****',
  55. })
  56. _logger.debug("Call ClickSend API : %s params %s",
  57. params['url'], params)
  58. response = r.text
  59. xml_response = fromstring(response)
  60. for messages in xml_response:
  61. for message in messages:
  62. for element in message:
  63. if element.tag == 'result' and \
  64. element.text != '0000':
  65. raise ValueError(response)