mail_message.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from collections import defaultdict
  4. from operator import itemgetter
  5. from odoo import exceptions, fields, models
  6. from odoo.tools import groupby
  7. class MailMessage(models.Model):
  8. """ Override MailMessage class in order to add a new type: SMS messages.
  9. Those messages comes with their own notification method, using SMS
  10. gateway. """
  11. _inherit = 'mail.message'
  12. message_type = fields.Selection(selection_add=[
  13. ('sms', 'SMS')
  14. ], ondelete={'sms': lambda recs: recs.write({'message_type': 'email'})})
  15. has_sms_error = fields.Boolean(
  16. 'Has SMS error', compute='_compute_has_sms_error', search='_search_has_sms_error')
  17. def _compute_has_sms_error(self):
  18. sms_error_from_notification = self.env['mail.notification'].sudo().search([
  19. ('notification_type', '=', 'sms'),
  20. ('mail_message_id', 'in', self.ids),
  21. ('notification_status', '=', 'exception')]).mapped('mail_message_id')
  22. for message in self:
  23. message.has_sms_error = message in sms_error_from_notification
  24. def _search_has_sms_error(self, operator, operand):
  25. if operator == '=' and operand:
  26. return ['&', ('notification_ids.notification_status', '=', 'exception'), ('notification_ids.notification_type', '=', 'sms')]
  27. raise NotImplementedError()
  28. def message_format(self, format_reply=True):
  29. """ Override in order to retrieves data about SMS (recipient name and
  30. SMS status)
  31. TDE FIXME: clean the overall message_format thingy
  32. """
  33. message_values = super(MailMessage, self).message_format(format_reply=format_reply)
  34. all_sms_notifications = self.env['mail.notification'].sudo().search([
  35. ('mail_message_id', 'in', [r['id'] for r in message_values]),
  36. ('notification_type', '=', 'sms')
  37. ])
  38. msgid_to_notif = defaultdict(lambda: self.env['mail.notification'].sudo())
  39. for notif in all_sms_notifications:
  40. msgid_to_notif[notif.mail_message_id.id] += notif
  41. for message in message_values:
  42. customer_sms_data = [(notif.id, notif.res_partner_id.display_name or notif.sms_number, notif.notification_status) for notif in msgid_to_notif.get(message['id'], [])]
  43. message['sms_ids'] = customer_sms_data
  44. return message_values