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.

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