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.

227 lines
7.5 KiB

7 years ago
  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. import logging
  9. from odoo import models, fields, api, _
  10. from odoo.exceptions import UserError
  11. _logger = logging.getLogger(__name__)
  12. PRIORITY_LIST = [
  13. ('0', '0'),
  14. ('1', '1'),
  15. ('2', '2'),
  16. ('3', '3')
  17. ]
  18. CLASSES_LIST = [
  19. ('0', 'Flash'),
  20. ('1', 'Phone display'),
  21. ('2', 'SIM'),
  22. ('3', 'Toolkit')
  23. ]
  24. class SMSClient(models.Model):
  25. _name = 'sms.gateway'
  26. _description = 'SMS Client'
  27. @api.model
  28. def get_method(self):
  29. return []
  30. name = fields.Char(string='Gateway Name', required=True)
  31. from_provider = fields.Char(string="From")
  32. method = fields.Selection(
  33. string='API Method',
  34. selection='get_method')
  35. url = fields.Char(
  36. string='Gateway URL', help='Base url for message')
  37. state = fields.Selection(selection=[
  38. ('new', 'Not Verified'),
  39. ('waiting', 'Waiting for Verification'),
  40. ('confirm', 'Verified'),
  41. ], string='Gateway Status', index=True, readonly=True, default='new')
  42. user_ids = fields.Many2many(
  43. comodel_name='res.users',
  44. string='Users Allowed')
  45. code = fields.Char('Verification Code')
  46. body = fields.Text(
  47. string='Message',
  48. help="The message text that will be send along with the"
  49. " email which is send through this server.")
  50. validity = fields.Integer(
  51. default=10,
  52. help="The maximum time - in minute(s) - before the message "
  53. "is dropped.")
  54. classes = fields.Selection(
  55. selection=CLASSES_LIST, string='Class',
  56. default='1',
  57. help='The SMS class: flash(0),phone display(1),SIM(2),toolkit(3)')
  58. deferred = fields.Integer(
  59. default=0,
  60. help='The time -in minute(s)- to wait before sending the message.')
  61. deferred_visible = fields.Boolean(default=False)
  62. priority = fields.Selection(
  63. selection=PRIORITY_LIST, string='Priority', default='3',
  64. help='The priority of the message')
  65. coding = fields.Selection(selection=[
  66. ('1', '7 bit'),
  67. ('2', 'Unicode')
  68. ], string='Coding',
  69. help='The SMS coding: 1 for 7 bit (160 chracters max'
  70. 'lenght) or 2 for unicode (70 characters max'
  71. 'lenght)',
  72. default='1'
  73. )
  74. tag = fields.Char('Tag', help='an optional tag')
  75. nostop = fields.Boolean(
  76. default=True,
  77. help='Do not display STOP clause in the message, this requires that '
  78. 'this is not an advertising message.')
  79. char_limit = fields.Boolean('Character Limit', default=True)
  80. default_gateway = fields.Boolean(default=False)
  81. company_id = fields.Many2one(comodel_name='res.company')
  82. @api.multi
  83. def _check_permissions(self):
  84. self.ensure_one()
  85. if self.env.uid not in self.sudo().user_ids.ids:
  86. return False
  87. return True
  88. @api.model
  89. def _run_send_sms(self, domain=None):
  90. if domain is None:
  91. domain = []
  92. domain.append(('state', '=', 'draft'))
  93. sms = self.env['sms.sms'].search(domain)
  94. return sms.send()
  95. class SmsSms(models.Model):
  96. _name = 'sms.sms'
  97. _description = 'SMS'
  98. _rec_name = 'mobile'
  99. message = fields.Text(
  100. size=256,
  101. required=True,
  102. readonly=True,
  103. states={'draft': [('readonly', False)]})
  104. mobile = fields.Char(
  105. required=True,
  106. readonly=True,
  107. states={'draft': [('readonly', False)]})
  108. gateway_id = fields.Many2one(
  109. comodel_name='sms.gateway',
  110. string='SMS Gateway',
  111. readonly=True,
  112. states={'draft': [('readonly', False)]})
  113. state = fields.Selection(selection=[
  114. ('draft', 'Queued'),
  115. ('sent', 'Sent'),
  116. ('cancel', 'Cancel'),
  117. ('error', 'Error'),
  118. ], string='Message Status',
  119. readonly=True,
  120. default='draft')
  121. error = fields.Text(
  122. string='Last Error',
  123. size=256,
  124. readonly=True,
  125. states={'draft': [('readonly', False)]})
  126. validity = fields.Integer(
  127. string='Validity',
  128. readonly=True,
  129. states={'draft': [('readonly', False)]},
  130. help='The maximum time -in minute(s)- before the message is dropped.')
  131. classes = fields.Selection(
  132. selection=CLASSES_LIST,
  133. readonly=True,
  134. states={'draft': [('readonly', False)]},
  135. help='The sms class: flash(0), phone display(1), SIM(2), toolkit(3)')
  136. deferred = fields.Integer(
  137. readonly=True,
  138. states={'draft': [('readonly', False)]},
  139. help='The time -in minute(s)- to wait before sending the message.')
  140. priority = fields.Selection(
  141. readonly=True,
  142. states={'draft': [('readonly', False)]},
  143. selection=PRIORITY_LIST,
  144. help='The priority of the message ')
  145. coding = fields.Selection([
  146. ('1', '7 bit'),
  147. ('2', 'Unicode')
  148. ], readonly=True,
  149. states={'draft': [('readonly', False)]},
  150. help='The sms coding: 1 for 7 bit or 2 for unicode')
  151. tag = fields.Char(
  152. size=256,
  153. readonly=True,
  154. states={'draft': [('readonly', False)]},
  155. help='An optional tag')
  156. nostop = fields.Boolean(
  157. string='NoStop',
  158. readonly=True,
  159. states={'draft': [('readonly', False)]},
  160. help='Do not display STOP clause in the message, this requires that'
  161. 'this is not an advertising message.')
  162. partner_id = fields.Many2one(
  163. 'res.partner',
  164. readonly=True,
  165. states={'draft': [('readonly', False)]},
  166. string='Partner')
  167. company_id = fields.Many2one(
  168. comodel_name='res.company',
  169. readonly=True,
  170. states={'draft': [('readonly', False)]})
  171. @api.model
  172. def _convert_to_e164(self, erp_number):
  173. to_dial_number = erp_number.replace(u'\xa0', u' ')
  174. return to_dial_number
  175. @api.onchange('partner_id')
  176. def onchange_partner_id(self):
  177. self.mobile = self.partner_id.mobile
  178. @api.multi
  179. def send(self):
  180. for sms in self:
  181. if not sms.gateway_id._check_permissions():
  182. sms.write(
  183. {'error': 'no permission on gateway', 'state': 'error'})
  184. sms._cr.commit()
  185. return False
  186. if sms.gateway_id.char_limit and len(sms.message) > 160:
  187. sms.write({
  188. 'state': 'error',
  189. 'error': _('Size of SMS should not be more then 160 char'),
  190. })
  191. if not hasattr(sms, "_send_%s" % sms.gateway_id.method):
  192. #may not exist the gateway
  193. raise UserError(_("No method gateway selected"))
  194. else:
  195. try:
  196. with sms._cr.savepoint():
  197. getattr(sms, "_send_%s" % sms.gateway_id.method)()
  198. sms.write({'state': 'sent', 'error': ''})
  199. except Exception, e:
  200. _logger.error('Failed to send sms %s', e)
  201. sms.write({'error': e, 'state': 'error'})
  202. sms._cr.commit()
  203. return True
  204. @api.multi
  205. def cancel(self):
  206. self.write({'state': 'cancel'})
  207. @api.multi
  208. def retry(self):
  209. self.write({'state': 'draft'})