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.

129 lines
3.9 KiB

  1. # coding: utf-8
  2. # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
  3. # Copyright (C) 2011 SYLEAM (<http://syleam.fr/>)
  4. # Copyright (C) 2013 Julius Network Solutions SARL <contact@julius.fr>
  5. # Copyright (C) 2015 Valentin Chemiere <valentin.chemiere@akretion.com>
  6. # Copyright (C) 2015 Sébastien BEAU <sebastien.beau@akretion.com>
  7. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  8. from odoo import models, fields, api, _
  9. import logging
  10. _logger = logging.getLogger(__name__)
  11. class SmsSms(models.Model):
  12. _name = 'sms.sms'
  13. _description = 'SMS'
  14. _rec_name = 'mobile'
  15. _inherit = 'sms.abstract'
  16. message = fields.Text(
  17. size=256,
  18. required=True,
  19. readonly=True,
  20. states={'draft': [('readonly', False)]})
  21. mobile = fields.Char(
  22. required=True,
  23. readonly=True,
  24. states={'draft': [('readonly', False)]})
  25. gateway_id = fields.Many2one(
  26. comodel_name='sms.gateway',
  27. string='SMS Gateway',
  28. readonly=True,
  29. states={'draft': [('readonly', False)]})
  30. partner_id = fields.Many2one(
  31. 'res.partner',
  32. readonly=True,
  33. states={'draft': [('readonly', False)]},
  34. string='Partner')
  35. state = fields.Selection(selection=[
  36. ('draft', 'Queued'),
  37. ('sent', 'Sent'),
  38. ('cancel', 'Cancel'),
  39. ('error', 'Error'),
  40. ], string='Message Status',
  41. readonly=True,
  42. default='draft')
  43. error = fields.Text(
  44. string='Last Error',
  45. size=256,
  46. readonly=True,
  47. states={'draft': [('readonly', False)]})
  48. @api.onchange('partner_id')
  49. def onchange_partner_id(self):
  50. self.mobile = self.partner_id.mobile
  51. # commit is use to not loose each sms state with orm rollback
  52. @api.multi
  53. def _check_gateway_method(self):
  54. self.ensure_one()
  55. if self.gateway_id.method:
  56. return True
  57. else:
  58. self.write({
  59. 'state': 'error',
  60. 'error': _("No method gateway selected ")})
  61. self._cr.commit()
  62. return False
  63. @api.multi
  64. def _check_gateway_permission(self):
  65. self.ensure_one()
  66. if self.gateway_id._check_permissions():
  67. return True
  68. else:
  69. self.write(
  70. {'error': 'no permission on gateway', 'state': 'error'})
  71. self._cr.commit()
  72. return False
  73. @api.multi
  74. def _check_sms_length(self):
  75. self.ensure_one()
  76. if len(self.message) <= self.gateway_id.char_limit:
  77. return True
  78. else:
  79. self.write({
  80. 'state': 'error',
  81. 'error': _("Size of SMS should not be more than %s "
  82. "characters ") % self.sms.gateway_id.char_limit
  83. })
  84. self._cr.commit()
  85. return False
  86. @api.multi
  87. def send(self):
  88. allsend_ok = True
  89. for sms in self:
  90. sms_check = True
  91. if not sms._check_gateway_method():
  92. allsend_ok = False
  93. sms_check = False
  94. continue
  95. if not sms.gateway_id._check_permissions():
  96. allsend_ok = False
  97. sms_check = False
  98. continue
  99. if not sms._check_sms_length():
  100. allsend_ok = False
  101. sms_check = False
  102. continue
  103. if sms_check:
  104. try:
  105. with sms._cr.savepoint():
  106. getattr(sms, "_send_%s" % sms.gateway_id.method)()
  107. sms.write({'state': 'sent', 'error': ''})
  108. except Exception as e:
  109. _logger.error('Failed to send sms %s', e)
  110. sms.write({'error': e, 'state': 'error'})
  111. sms._cr.commit()
  112. return allsend_ok
  113. @api.multi
  114. def cancel(self):
  115. self.write({'state': 'cancel'})
  116. @api.multi
  117. def retry(self):
  118. self.write({'state': 'draft'})