event_mail.py 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import api, fields, models
  4. class EventTypeMail(models.Model):
  5. _inherit = 'event.type.mail'
  6. @api.model
  7. def _selection_template_model(self):
  8. return super(EventTypeMail, self)._selection_template_model() + [('sms.template', 'SMS')]
  9. notification_type = fields.Selection(selection_add=[('sms', 'SMS')], ondelete={'sms': 'set default'})
  10. @api.depends('notification_type')
  11. def _compute_template_model_id(self):
  12. sms_model = self.env['ir.model']._get('sms.template')
  13. sms_mails = self.filtered(lambda mail: mail.notification_type == 'sms')
  14. sms_mails.template_model_id = sms_model
  15. super(EventTypeMail, self - sms_mails)._compute_template_model_id()
  16. class EventMailScheduler(models.Model):
  17. _inherit = 'event.mail'
  18. @api.model
  19. def _selection_template_model(self):
  20. return super(EventMailScheduler, self)._selection_template_model() + [('sms.template', 'SMS')]
  21. def _selection_template_model_get_mapping(self):
  22. return {**super(EventMailScheduler, self)._selection_template_model_get_mapping(), 'sms': 'sms.template'}
  23. notification_type = fields.Selection(selection_add=[('sms', 'SMS')], ondelete={'sms': 'set default'})
  24. @api.depends('notification_type')
  25. def _compute_template_model_id(self):
  26. sms_model = self.env['ir.model']._get('sms.template')
  27. sms_mails = self.filtered(lambda mail: mail.notification_type == 'sms')
  28. sms_mails.template_model_id = sms_model
  29. super(EventMailScheduler, self - sms_mails)._compute_template_model_id()
  30. def execute(self):
  31. for scheduler in self:
  32. now = fields.Datetime.now()
  33. if scheduler.interval_type != 'after_sub' and scheduler.notification_type == 'sms':
  34. # before or after event -> one shot email
  35. if scheduler.mail_done:
  36. continue
  37. # no template -> ill configured, skip and avoid crash
  38. if not scheduler.template_ref:
  39. continue
  40. # Do not send SMS if the communication was scheduled before the event but the event is over
  41. if scheduler.scheduled_date <= now and (scheduler.interval_type != 'before_event' or scheduler.event_id.date_end > now):
  42. scheduler.event_id.registration_ids.filtered(lambda registration: registration.state != 'cancel')._message_sms_schedule_mass(
  43. template=scheduler.template_ref,
  44. mass_keep_log=True
  45. )
  46. scheduler.update({
  47. 'mail_done': True,
  48. 'mail_count_done': scheduler.event_id.seats_reserved + scheduler.event_id.seats_used,
  49. })
  50. return super(EventMailScheduler, self).execute()
  51. @api.onchange('notification_type')
  52. def set_template_ref_model(self):
  53. super().set_template_ref_model()
  54. mail_model = self.env['sms.template']
  55. if self.notification_type == 'sms':
  56. record = mail_model.search([('model', '=', 'event.registration')], limit=1)
  57. self.template_ref = "{},{}".format('sms.template', record.id) if record else False
  58. class EventMailRegistration(models.Model):
  59. _inherit = 'event.mail.registration'
  60. def execute(self):
  61. now = fields.Datetime.now()
  62. todo = self.filtered(lambda reg_mail:
  63. not reg_mail.mail_sent and \
  64. reg_mail.registration_id.state in ['open', 'done'] and \
  65. (reg_mail.scheduled_date and reg_mail.scheduled_date <= now) and \
  66. reg_mail.scheduler_id.notification_type == 'sms'
  67. )
  68. for reg_mail in todo:
  69. reg_mail.registration_id._message_sms_schedule_mass(
  70. template=reg_mail.scheduler_id.template_ref,
  71. mass_keep_log=True
  72. )
  73. todo.write({'mail_sent': True})
  74. return super(EventMailRegistration, self).execute()