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.

52 lines
2.0 KiB

  1. # Copyright 2018 Tecnativa - Jairo Llopis
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  3. from odoo import models
  4. class MailMail(models.Model):
  5. _inherit = "mail.mail"
  6. def _postprocess_sent_message(
  7. self, success_pids, failure_reason=False, failure_type=None
  8. ):
  9. """Write consent status after sending message."""
  10. # Know if mail was successfully sent to a privacy consent
  11. if (
  12. self.mail_message_id.model == "privacy.consent"
  13. and self.state == "sent"
  14. and success_pids
  15. and not failure_reason
  16. and not failure_type
  17. ):
  18. # Get related consent
  19. consent = self.env["privacy.consent"].browse(self.mail_message_id.res_id)
  20. # Set as sent if needed
  21. if consent.state == "draft" and consent.partner_id.id in {
  22. par.id for par in success_pids
  23. }:
  24. consent.write({"state": "sent"})
  25. return super()._postprocess_sent_message(
  26. success_pids=success_pids,
  27. failure_reason=failure_reason,
  28. failure_type=failure_type,
  29. )
  30. def _send_prepare_body(self):
  31. """Replace privacy consent magic links.
  32. This replacement is done here instead of directly writing it into
  33. the ``mail.template`` to avoid writing the tokeinzed URL
  34. in the mail thread for the ``privacy.consent`` record,
  35. which would enable any reader of such thread to impersonate the
  36. subject and choose in its behalf.
  37. """
  38. result = super(MailMail, self)._send_prepare_body()
  39. # Avoid polluting other model mails
  40. if self.model != "privacy.consent":
  41. return result
  42. # Tokenize consent links
  43. consent = self.env["privacy.consent"].browse(self.mail_message_id.res_id)
  44. result = result.replace("/privacy/consent/accept/", consent._url(True),)
  45. result = result.replace("/privacy/consent/reject/", consent._url(False),)
  46. return result