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.

266 lines
9.1 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. import logging
  9. from openerp import models, fields, api, _
  10. from openerp.addons.server_environment import serv_config
  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. @api.multi
  31. def _get_provider_conf(self):
  32. for sms_provider in self:
  33. global_section_name = 'sms_provider'
  34. config_vals = {}
  35. if serv_config.has_section(global_section_name):
  36. config_vals.update(serv_config.items(global_section_name))
  37. custom_section_name = '.'.join((global_section_name,
  38. sms_provider.name))
  39. if serv_config.has_section(custom_section_name):
  40. config_vals.update(serv_config.items(custom_section_name))
  41. for key in config_vals:
  42. sms_provider[key] = config_vals[key]
  43. name = fields.Char('Gateway Name', required=True)
  44. url = fields.Char(
  45. string='Gateway URL', compute='_get_provider_conf',
  46. help='Base url for message')
  47. url_visible = fields.Boolean(default=False)
  48. method = fields.Selection(
  49. string='API Method',
  50. selection='get_method')
  51. state = fields.Selection([
  52. ('new', 'Not Verified'),
  53. ('waiting', 'Waiting for Verification'),
  54. ('confirm', 'Verified'),
  55. ], 'Gateway Status', index=True, readonly=True, default='new')
  56. user_ids = fields.Many2many(
  57. 'res.users',
  58. string='Users Allowed')
  59. sms_account = fields.Char(compute='_get_provider_conf')
  60. sms_account_visible = fields.Boolean(default=False)
  61. login_provider = fields.Char(compute='_get_provider_conf')
  62. login_provider_visible = fields.Boolean(default=False)
  63. password_provider = fields.Char(compute='_get_provider_conf')
  64. password_provider_visible = fields.Boolean(default=False)
  65. from_provider = fields.Char(compute='_get_provider_conf')
  66. from_provider_visible = fields.Boolean(default=False)
  67. code = fields.Char('Verification Code')
  68. code_visible = fields.Boolean(default=False)
  69. body = fields.Text(
  70. string='Message',
  71. help="The message text that will be send along with the"
  72. " email which is send through this server.")
  73. validity = fields.Integer(
  74. default=10,
  75. help="The maximum time - in minute(s) - before the message "
  76. "is dropped.")
  77. validity_visible = fields.Boolean(default=False)
  78. classes = fields.Selection(
  79. CLASSES_LIST, 'Class',
  80. default='1',
  81. help='The SMS class: flash(0),phone display(1),SIM(2),toolkit(3)')
  82. classes_visible = fields.Boolean(default=False)
  83. deferred = fields.Integer(
  84. default=0,
  85. help='The time -in minute(s)- to wait before sending the message.')
  86. deferred_visible = fields.Boolean(default=False)
  87. priority = fields.Selection(
  88. PRIORITY_LIST, string='Priority', default='3',
  89. help='The priority of the message')
  90. priority_visible = fields.Boolean(default=False)
  91. coding = fields.Selection([
  92. ('1', '7 bit'),
  93. ('2', 'Unicode')
  94. ], 'Coding',
  95. help='The SMS coding: 1 for 7 bit (160 chracters max'
  96. 'lenght) or 2 for unicode (70 characters max'
  97. 'lenght)',
  98. default='1'
  99. )
  100. coding_visible = fields.Boolean(default=False)
  101. tag = fields.Char('Tag', help='an optional tag')
  102. tag_visible = fields.Boolean(default=False)
  103. nostop = fields.Boolean(
  104. default=True,
  105. help='Do not display STOP clause in the message, this requires that '
  106. 'this is not an advertising message.')
  107. nostop_visible = fields.Boolean(default=False)
  108. char_limit = fields.Boolean('Character Limit', default=True)
  109. char_limit_visible = fields.Boolean(default=False)
  110. default_gateway = fields.Boolean(default=False)
  111. company_id = fields.Many2one('res.company')
  112. @api.onchange('method')
  113. def onchange_method(self):
  114. if not self.method:
  115. self.url_visible = False
  116. self.sms_account_visible = False
  117. self.login_provider_visible = False
  118. self.password_provider_visible = False
  119. self.from_provider_visible = False
  120. self.validity_visible = False
  121. self.classes_visible = False
  122. self.deferred_visible = False
  123. self.nostop_visible = False
  124. self.priority_visible = False
  125. self.coding_visible = False
  126. self.tag_visible = False
  127. self.char_limit_visible = False
  128. @api.multi
  129. def _check_permissions(self):
  130. self.ensure_one()
  131. if self.env.uid not in self.sudo().user_ids.ids:
  132. return False
  133. return True
  134. @api.model
  135. def _run_send_sms(self, domain=None):
  136. if domain is None:
  137. domain = []
  138. domain.append(('state', '=', 'draft'))
  139. sms = self.env['sms.sms'].search(domain)
  140. return sms.send()
  141. class SmsSms(models.Model):
  142. _name = 'sms.sms'
  143. _description = 'SMS'
  144. _rec_name = 'mobile'
  145. message = fields.Text(
  146. size=256,
  147. required=True,
  148. readonly=True,
  149. states={'draft': [('readonly', False)]})
  150. mobile = fields.Char(
  151. required=True,
  152. readonly=True,
  153. states={'draft': [('readonly', False)]})
  154. gateway_id = fields.Many2one(
  155. 'sms.gateway',
  156. 'SMS Gateway',
  157. readonly=True,
  158. states={'draft': [('readonly', False)]})
  159. state = fields.Selection([
  160. ('draft', 'Queued'),
  161. ('sent', 'Sent'),
  162. ('cancel', 'Cancel'),
  163. ('error', 'Error'),
  164. ], 'Message Status',
  165. select=True,
  166. readonly=True,
  167. default='draft')
  168. error = fields.Text(
  169. 'Last Error',
  170. size=256,
  171. readonly=True,
  172. states={'draft': [('readonly', False)]})
  173. validity = fields.Integer(
  174. 'Validity',
  175. readonly=True,
  176. states={'draft': [('readonly', False)]},
  177. help='The maximum time -in minute(s)- before the message is dropped.')
  178. classes = fields.Selection(
  179. readonly=True,
  180. states={'draft': [('readonly', False)]},
  181. selection=CLASSES_LIST,
  182. help='The sms class: flash(0), phone display(1), SIM(2), toolkit(3)')
  183. deferred = fields.Integer(
  184. readonly=True,
  185. states={'draft': [('readonly', False)]},
  186. help='The time -in minute(s)- to wait before sending the message.')
  187. priority = fields.Selection(
  188. readonly=True,
  189. states={'draft': [('readonly', False)]},
  190. selection=PRIORITY_LIST,
  191. help='The priority of the message ')
  192. coding = fields.Selection([
  193. ('1', '7 bit'),
  194. ('2', 'Unicode')
  195. ], readonly=True,
  196. states={'draft': [('readonly', False)]},
  197. help='The sms coding: 1 for 7 bit or 2 for unicode')
  198. tag = fields.Char(
  199. size=256,
  200. readonly=True,
  201. states={'draft': [('readonly', False)]},
  202. help='An optional tag')
  203. nostop = fields.Boolean(
  204. 'NoStop',
  205. readonly=True,
  206. states={'draft': [('readonly', False)]},
  207. help='Do not display STOP clause in the message, this requires that'
  208. 'this is not an advertising message.')
  209. partner_id = fields.Many2one(
  210. 'res.partner',
  211. readonly=True,
  212. states={'draft': [('readonly', False)]},
  213. string='Partner')
  214. company_id = fields.Many2one(
  215. 'res.company',
  216. readonly=True,
  217. states={'draft': [('readonly', False)]})
  218. @api.onchange('partner_id')
  219. def onchange_partner_id(self):
  220. self.mobile = self.partner_id.mobile
  221. @api.multi
  222. def send(self):
  223. for sms in self:
  224. if sms.gateway_id.char_limit and len(sms.message) > 160:
  225. sms.write({
  226. 'state': 'error',
  227. 'error': _('Size of SMS should not be more then 160 char'),
  228. })
  229. if not hasattr(sms, "_send_%s" % sms.gateway_id.method):
  230. raise NotImplemented
  231. else:
  232. try:
  233. with sms._cr.savepoint():
  234. getattr(sms, "_send_%s" % sms.gateway_id.method)()
  235. sms.write({'state': 'sent', 'error': ''})
  236. except Exception, e:
  237. _logger.error('Failed to send sms %s', e)
  238. sms.write({'error': e, 'state': 'error'})
  239. sms._cr.commit()
  240. return True
  241. @api.multi
  242. def cancel(self):
  243. self.write({'state': 'cancel'})
  244. @api.multi
  245. def retry(self):
  246. self.write({'state': 'draft'})