mail_wizard_invite.py 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from lxml import etree
  4. from lxml.html import builder as html
  5. from odoo import _, api, fields, models
  6. from odoo.exceptions import UserError
  7. class Invite(models.TransientModel):
  8. """ Wizard to invite partners (or channels) and make them followers. """
  9. _name = 'mail.wizard.invite'
  10. _description = 'Invite wizard'
  11. @api.model
  12. def default_get(self, fields):
  13. result = super(Invite, self).default_get(fields)
  14. if 'message' not in fields:
  15. return result
  16. user_name = self.env.user.display_name
  17. model = result.get('res_model')
  18. res_id = result.get('res_id')
  19. if model and res_id:
  20. document = self.env['ir.model']._get(model).display_name
  21. title = self.env[model].browse(res_id).display_name
  22. msg_fmt = _('%(user_name)s invited you to follow %(document)s document: %(title)s')
  23. else:
  24. msg_fmt = _('%(user_name)s invited you to follow a new document.')
  25. text = msg_fmt % locals()
  26. message = html.DIV(
  27. html.P(_('Hello,')),
  28. html.P(text)
  29. )
  30. result['message'] = etree.tostring(message)
  31. return result
  32. res_model = fields.Char('Related Document Model', required=True, help='Model of the followed resource')
  33. res_id = fields.Integer('Related Document ID', help='Id of the followed resource')
  34. partner_ids = fields.Many2many('res.partner', string='Recipients', help="List of partners that will be added as follower of the current document.",
  35. domain=[('type', '!=', 'private')])
  36. message = fields.Html('Message')
  37. send_mail = fields.Boolean('Send Email', default=True, help="If checked, the partners will receive an email warning they have been added in the document's followers.")
  38. def add_followers(self):
  39. if not self.env.user.email:
  40. raise UserError(_("Unable to post message, please configure the sender's email address."))
  41. email_from = self.env.user.email_formatted
  42. for wizard in self:
  43. Model = self.env[wizard.res_model]
  44. document = Model.browse(wizard.res_id)
  45. # filter partner_ids to get the new followers, to avoid sending email to already following partners
  46. new_partners = wizard.partner_ids - document.sudo().message_partner_ids
  47. document.message_subscribe(partner_ids=new_partners.ids)
  48. model_name = self.env['ir.model']._get(wizard.res_model).display_name
  49. # send an email if option checked and if a message exists (do not send void emails)
  50. if wizard.send_mail and wizard.message and not wizard.message == '<br>': # when deleting the message, cleditor keeps a <br>
  51. message = self.env['mail.message'].create(
  52. self._prepare_message_values(document, model_name, email_from)
  53. )
  54. email_partners_data = []
  55. recipients_data = self.env['mail.followers']._get_recipient_data(document, 'comment', False, pids=new_partners.ids)[document.id]
  56. for _pid, pdata in recipients_data.items():
  57. pdata['notif'] = 'email'
  58. email_partners_data.append(pdata)
  59. document._notify_thread_by_email(
  60. message, email_partners_data,
  61. send_after_commit=False
  62. )
  63. # in case of failure, the web client must know the message was
  64. # deleted to discard the related failure notification
  65. self.env['bus.bus']._sendone(self.env.user.partner_id, 'mail.message/delete', {'message_ids': message.ids})
  66. message.unlink()
  67. return {'type': 'ir.actions.act_window_close'}
  68. def _prepare_message_values(self, document, model_name, email_from):
  69. return {
  70. 'subject': _('Invitation to follow %(document_model)s: %(document_name)s', document_model=model_name,
  71. document_name=document.display_name),
  72. 'body': self.message,
  73. 'record_name': document.display_name,
  74. 'email_from': email_from,
  75. 'reply_to': email_from,
  76. 'model': self.res_model,
  77. 'res_id': self.res_id,
  78. 'reply_to_force_new': True,
  79. 'email_add_signature': True,
  80. }