mail_mail.py 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. import re
  4. import werkzeug.urls
  5. from odoo import api, fields, models, tools
  6. class MailMail(models.Model):
  7. """Add the mass mailing campaign data to mail"""
  8. _inherit = ['mail.mail']
  9. mailing_id = fields.Many2one('mailing.mailing', string='Mass Mailing')
  10. mailing_trace_ids = fields.One2many('mailing.trace', 'mail_mail_id', string='Statistics')
  11. @api.model_create_multi
  12. def create(self, values_list):
  13. """ Override mail_mail creation to create an entry in mail.mail.statistics """
  14. # TDE note: should be after 'all values computed', to have values (FIXME after merging other branch holding create refactoring)
  15. mails = super(MailMail, self).create(values_list)
  16. for mail, values in zip(mails, values_list):
  17. if values.get('mailing_trace_ids'):
  18. mail.mailing_trace_ids.write({'message_id': mail.message_id})
  19. return mails
  20. def _get_tracking_url(self):
  21. token = tools.hmac(self.env(su=True), 'mass_mailing-mail_mail-open', self.id)
  22. return werkzeug.urls.url_join(self.get_base_url(), 'mail/track/%s/%s/blank.gif' % (self.id, token))
  23. def _send_prepare_body(self):
  24. """ Override to add the tracking URL to the body and to add
  25. trace ID in shortened urls """
  26. # TDE: temporary addition (mail was parameter) due to semi-new-API
  27. self.ensure_one()
  28. body = super(MailMail, self)._send_prepare_body()
  29. if self.mailing_id and body and self.mailing_trace_ids:
  30. for match in set(re.findall(tools.URL_REGEX, self.body_html)):
  31. href = match[0]
  32. url = match[1]
  33. parsed = werkzeug.urls.url_parse(url, scheme='http')
  34. if parsed.scheme.startswith('http') and parsed.path.startswith('/r/'):
  35. new_href = href.replace(url, url + '/m/' + str(self.mailing_trace_ids[0].id))
  36. body = body.replace(href, new_href)
  37. # generate tracking URL
  38. tracking_url = self._get_tracking_url()
  39. body = tools.append_content_to_html(
  40. body,
  41. '<img src="%s"/>' % tracking_url,
  42. plaintext=False,
  43. )
  44. body = self.env['mail.render.mixin']._replace_local_links(body)
  45. return body
  46. def _send_prepare_values(self, partner=None):
  47. # TDE: temporary addition (mail was parameter) due to semi-new-API
  48. res = super(MailMail, self)._send_prepare_values(partner)
  49. if self.mailing_id and res.get('body') and res.get('email_to'):
  50. base_url = self.mailing_id.get_base_url()
  51. emails = tools.email_split(res.get('email_to')[0])
  52. email_to = emails and emails[0] or False
  53. urls_to_replace = [
  54. (base_url + '/unsubscribe_from_list', self.mailing_id._get_unsubscribe_url(email_to, self.res_id)),
  55. (base_url + '/view', self.mailing_id._get_view_url(email_to, self.res_id))
  56. ]
  57. for url_to_replace, new_url in urls_to_replace:
  58. if url_to_replace in res['body']:
  59. res['body'] = res['body'].replace(url_to_replace, new_url if new_url else '#')
  60. return res
  61. def _postprocess_sent_message(self, success_pids, failure_reason=False, failure_type=None):
  62. mail_sent = not failure_type # we consider that a recipient error is a failure with mass mailling and show them as failed
  63. for mail in self:
  64. if mail.mailing_id:
  65. if mail_sent is True and mail.mailing_trace_ids:
  66. mail.mailing_trace_ids.set_sent()
  67. elif mail_sent is False and mail.mailing_trace_ids:
  68. mail.mailing_trace_ids.set_failed(failure_type=failure_type)
  69. return super(MailMail, self)._postprocess_sent_message(success_pids, failure_reason=failure_reason, failure_type=failure_type)