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.

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