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.

65 lines
2.3 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(self, success_pids, failure_reason=False,
  7. failure_type=None):
  8. """Write consent status after sending message."""
  9. # Know if mail was successfully sent to a privacy consent
  10. if (
  11. self.mail_message_id.model == "privacy.consent"
  12. and self.state == "sent"
  13. and success_pids
  14. and not failure_reason
  15. and not failure_type
  16. ):
  17. # Get related consent
  18. consent = self.env["privacy.consent"].browse(
  19. self.mail_message_id.res_id,
  20. self._prefetch,
  21. )
  22. # Set as sent if needed
  23. if (
  24. consent.state == "draft"
  25. and consent.partner_id.id in {par.id for par in success_pids}
  26. ):
  27. consent.write({
  28. "state": "sent",
  29. })
  30. return super()._postprocess_sent_message(
  31. success_pids=success_pids,
  32. failure_reason=failure_reason,
  33. failure_type=failure_type,
  34. )
  35. def _send_prepare_body(self):
  36. """Replace privacy consent magic links.
  37. This replacement is done here instead of directly writing it into
  38. the ``mail.template`` to avoid writing the tokeinzed URL
  39. in the mail thread for the ``privacy.consent`` record,
  40. which would enable any reader of such thread to impersonate the
  41. subject and choose in its behalf.
  42. """
  43. result = super(MailMail, self)._send_prepare_body()
  44. # Avoid polluting other model mails
  45. if self.model != "privacy.consent":
  46. return result
  47. # Tokenize consent links
  48. consent = self.env["privacy.consent"] \
  49. .browse(self.mail_message_id.res_id) \
  50. .with_prefetch(self._prefetch)
  51. result = result.replace(
  52. "/privacy/consent/accept/",
  53. consent._url(True),
  54. )
  55. result = result.replace(
  56. "/privacy/consent/reject/",
  57. consent._url(False),
  58. )
  59. return result