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.

54 lines
2.0 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 Tecnativa - Jairo Llopis
  3. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  4. from odoo import models
  5. class MailMail(models.Model):
  6. _inherit = "mail.mail"
  7. def _postprocess_sent_message(self, mail_sent=True):
  8. """Write consent status after sending message."""
  9. if mail_sent:
  10. # Get all mails sent to consents
  11. consent_mails = self.filtered(
  12. lambda one: one.mail_message_id.model == "privacy.consent"
  13. )
  14. # Get related draft consents
  15. consents = self.env["privacy.consent"].browse(
  16. consent_mails.mapped("mail_message_id.res_id"),
  17. self._prefetch
  18. ).filtered(lambda one: one.state == "draft")
  19. # Set as sent
  20. consents.write({
  21. "state": "sent",
  22. })
  23. return super(MailMail, self)._postprocess_sent_message(mail_sent)
  24. def send_get_mail_body(self, partner=None):
  25. """Replace privacy consent magic links.
  26. This replacement is done here instead of directly writing it into
  27. the ``mail.template`` to avoid writing the tokeinzed URL
  28. in the mail thread for the ``privacy.consent`` record,
  29. which would enable any reader of such thread to impersonate the
  30. subject and choose in its behalf.
  31. """
  32. result = super(MailMail, self).send_get_mail_body(partner=partner)
  33. # Avoid polluting other model mails
  34. if self.model != "privacy.consent":
  35. return result
  36. # Tokenize consent links
  37. consent = self.env["privacy.consent"] \
  38. .browse(self.mail_message_id.res_id) \
  39. .with_prefetch(self._prefetch)
  40. result = result.replace(
  41. "/privacy/consent/accept/",
  42. consent._url(True),
  43. )
  44. result = result.replace(
  45. "/privacy/consent/reject/",
  46. consent._url(False),
  47. )
  48. return result