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.

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