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.

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