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.

112 lines
4.0 KiB

6 years ago
6 years ago
6 years ago
6 years ago
  1. # coding: utf-8
  2. # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
  3. # Copyright (C) 2013 Julius Network Solutions SARL <contact@julius.fr>
  4. # Copyright (C) 2015 Valentin Chemiere <valentin.chemiere@akretion.com>
  5. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  6. from odoo import models, fields, api, _
  7. class WizardMassSms(models.TransientModel):
  8. _name = 'wizard.mass.sms'
  9. @api.model
  10. def _default_get_gateway(self):
  11. return self.env['sms.gateway'].search([], limit=1).id
  12. @api.model
  13. def _default_get_partner(self):
  14. if self._context.get('active_model') == 'res.partner':
  15. return self._context.get('active_ids')
  16. else:
  17. rec = self.env[self._context.get('active_model')].browse(
  18. self._context.get('active_id'))
  19. if hasattr(rec, 'message_follower_ids'):
  20. rec.message_follower_ids.mapped('partner_id')
  21. gateway_id = fields.Many2one(
  22. 'sms.gateway',
  23. required=True,
  24. default=_default_get_gateway)
  25. message = fields.Text(required=True)
  26. validity = fields.Integer(
  27. help='The maximum time -in minute(s)- before the message is dropped')
  28. classes = fields.Selection([
  29. ('0', 'Flash'),
  30. ('1', 'Phone display'),
  31. ('2', 'SIM'),
  32. ('3', 'Toolkit'),
  33. ], help='The sms class: flash(0),phone display(1),SIM(2),toolkit(3)')
  34. deferred = fields.Integer(
  35. help='The time -in minute(s)- to wait before sending the message')
  36. priority = fields.Selection([
  37. ('0', '0'),
  38. ('1', '1'),
  39. ('2', '2'),
  40. ('3', '3')
  41. ], help='The priority of the message')
  42. coding = fields.Selection([
  43. ('1', '7 bit'),
  44. ('2', 'Unicode')
  45. ], help='The sms coding: 1 for 7 bit or 2 for unicode')
  46. tag = fields.Char(size=256, help='An optional tag')
  47. nostop = fields.Boolean(
  48. help='Do not display STOP clause in the message, this requires that '
  49. 'this is not an advertising message')
  50. partner_ids = fields.Many2many('res.partner', default=_default_get_partner)
  51. @api.onchange('gateway_id')
  52. def onchange_gateway_mass(self):
  53. for key in ['validity', 'classes', 'deferred', 'priority',
  54. 'coding', 'tag', 'nostop']:
  55. self[key] = self.gateway_id[key]
  56. @api.model
  57. def _prepare_sms_vals(self, partner):
  58. return {
  59. 'gateway_id': self.gateway_id.id,
  60. 'state': 'draft',
  61. 'message': self.message,
  62. 'validity': self.validity,
  63. 'classes': self.classes,
  64. 'deferred': self.deferred,
  65. 'priority': self.priority,
  66. 'coding': self.coding,
  67. 'tag': self.tag,
  68. 'nostop': self.nostop,
  69. 'partner_id': partner.id,
  70. 'mobile': partner.mobile,
  71. }
  72. @api.multi
  73. def send(self):
  74. sms_obj = self.env['sms.sms']
  75. for partner in self.partner_ids:
  76. vals = self._prepare_sms_vals(partner)
  77. sms_obj.create(vals)
  78. src_model = self.env[self._context.get('active_model')]
  79. if hasattr(src_model, 'message_follower_ids'):
  80. rec = src_model.browse(self._context.get('active_id'))
  81. self.post_message(rec)
  82. return {'type': 'ir.actions.act_window_close'}
  83. def post_message(self, rec):
  84. channel = 'wizard.mass.sms'
  85. message = _("SMS Message (%s) sent to %s" % (
  86. self.message, ', '.join(self.partner_ids.mapped('name'))))
  87. rec.message_post(body=message)
  88. self.env['bus.bus'].sendone(channel, [])
  89. def redirect_to_sms_wizard(self, **kwargs):
  90. id = kwargs.get("id")
  91. model = kwargs.get("model")
  92. action = self.env['wizard.mass.sms'].action_wizard_mass_sms()
  93. action['src_model'] = model
  94. action['domain'] = [('res_id', '=', id)]
  95. return action
  96. @api.model
  97. def action_wizard_mass_sms(self):
  98. action = self.env.ref(
  99. 'base_sms_client.action_wizard_mass_sms').read()[0]
  100. return action