[IMP] Unsubscribe links in themes
This commit is contained in:
committed by
Julien De Coster
parent
70c30fe5b1
commit
e2f19f0ed5
@@ -60,7 +60,7 @@ class MailMail(osv.Model):
|
||||
)
|
||||
return '<img src="%s" alt=""/>' % track_url
|
||||
|
||||
def _get_unsubscribe_url(self, cr, uid, mail, email_to, msg=None, context=None):
|
||||
def _get_unsubscribe_url(self, cr, uid, mail, email_to, context=None):
|
||||
base_url = self.pool.get('ir.config_parameter').get_param(cr, uid, 'web.base.url')
|
||||
url = urlparse.urljoin(
|
||||
base_url, 'mail/mailing/%(mailing_id)s/unsubscribe?%(params)s' % {
|
||||
@@ -68,7 +68,7 @@ class MailMail(osv.Model):
|
||||
'params': urllib.urlencode({'db': cr.dbname, 'res_id': mail.res_id, 'email': email_to})
|
||||
}
|
||||
)
|
||||
return '<small><a href="%s">%s</a></small>' % (url, msg or 'Click to unsubscribe')
|
||||
return url
|
||||
|
||||
def send_get_mail_body(self, cr, uid, mail, partner=None, context=None):
|
||||
""" Override to add the tracking URL to the body. """
|
||||
@@ -88,12 +88,14 @@ class MailMail(osv.Model):
|
||||
|
||||
def send_get_email_dict(self, cr, uid, mail, partner=None, context=None):
|
||||
res = super(MailMail, self).send_get_email_dict(cr, uid, mail, partner, context=context)
|
||||
base_url = self.pool.get('ir.config_parameter').get_param(cr, uid, 'web.base.url')
|
||||
if mail.mailing_id and res.get('body') and res.get('email_to'):
|
||||
emails = tools.email_split(res.get('email_to')[0])
|
||||
email_to = emails and emails[0] or False
|
||||
unsubscribe_url = self._get_unsubscribe_url(cr, uid, mail, email_to, context=context)
|
||||
if unsubscribe_url:
|
||||
res['body'] = tools.append_content_to_html(res['body'], unsubscribe_url, plaintext=False, container_tag='p')
|
||||
unsubscribe_url= self._get_unsubscribe_url(cr, uid, mail, email_to, context=context)
|
||||
link_to_replace = base_url+'/unsubscribe_from_list'
|
||||
if link_to_replace in res['body']:
|
||||
res['body'] = res['body'].replace(link_to_replace, unsubscribe_url if unsubscribe_url else '#')
|
||||
return res
|
||||
|
||||
def _postprocess_sent_message(self, cr, uid, mail, context=None, mail_sent=True):
|
||||
|
||||
@@ -772,7 +772,7 @@ class MassMailing(osv.Model):
|
||||
composer_values = {
|
||||
'author_id': author_id,
|
||||
'attachment_ids': [(4, attachment.id) for attachment in mailing.attachment_ids],
|
||||
'body': self.convert_link(cr, uid, [mailing.id], context=context)[mailing.id],
|
||||
'body': self.convert_links(cr, uid, [mailing.id], context=context)[mailing.id],
|
||||
'subject': mailing.name,
|
||||
'model': mailing.mailing_model,
|
||||
'email_from': mailing.email_from,
|
||||
@@ -790,33 +790,12 @@ class MassMailing(osv.Model):
|
||||
self.write(cr, uid, [mailing.id], {'sent_date': fields.datetime.now(), 'state': 'done'}, context=context)
|
||||
return True
|
||||
|
||||
def convert_link(self, cr, uid, ids, context=None):
|
||||
website_links = self.pool['website.links']
|
||||
def convert_links(self, cr, uid, ids, context=None):
|
||||
res = {}
|
||||
for mass_mailing in self.browse(cr, uid, ids, context=context):
|
||||
res[mass_mailing.id] = mass_mailing.body_html if mass_mailing.body_html else ''
|
||||
|
||||
for match in re.findall(URL_REGEX, res[mass_mailing.id]):
|
||||
href = match[0]
|
||||
long_url = match[1]
|
||||
|
||||
utm_object = mass_mailing.mass_mailing_campaign_id if mass_mailing.mass_mailing_campaign_id else mass_mailing
|
||||
|
||||
vals = {'url': long_url}
|
||||
|
||||
if utm_object.campaign_id:
|
||||
vals['campaign_id'] = utm_object.campaign_id.id
|
||||
if utm_object.source_id:
|
||||
vals['source_id'] = utm_object.source_id.id
|
||||
if utm_object.medium_id:
|
||||
vals['medium_id'] = utm_object.medium_id.id
|
||||
|
||||
link_id = website_links.create(cr, uid, vals, context=context)
|
||||
shorten_url = website_links.browse(cr, uid, link_id, context=context)[0].short_url
|
||||
|
||||
if shorten_url:
|
||||
new_href = href.replace(long_url, shorten_url)
|
||||
res[mass_mailing.id] = res[mass_mailing.id].replace(href, new_href)
|
||||
utm_mixin = mass_mailing.mass_mailing_campaign_id if mass_mailing.mass_mailing_campaign_id else mass_mailing
|
||||
res[mass_mailing.id] = self.pool['website.links'].convert_links(cr, uid, res[mass_mailing.id], utm_mixin, context=context)
|
||||
return res
|
||||
|
||||
def put_in_queue(self, cr, uid, ids, context=None):
|
||||
@@ -851,14 +830,18 @@ class MassMailing(osv.Model):
|
||||
class MailMail(models.Model):
|
||||
_inherit = ['mail.mail']
|
||||
|
||||
links_backlist = ['/unsubscribe_from_list']
|
||||
|
||||
@api.model
|
||||
def send_get_mail_body(self, mail, partner=None):
|
||||
"""Override to add Statistic_id in shorted urls """
|
||||
if mail.mailing_id and mail.body_html:
|
||||
if mail.mailing_id and mail.body_html and mail.statistics_ids:
|
||||
for match in re.findall(URL_REGEX, mail.body_html):
|
||||
|
||||
href = match[0]
|
||||
url = match[1]
|
||||
if mail.statistics_ids:
|
||||
|
||||
if not [s for s in self.links_backlist if s in href]:
|
||||
new_href = href.replace(url, url + '/m/' + str(mail.statistics_ids[0].id))
|
||||
mail.body_html = mail.body_html.replace(href, new_href)
|
||||
|
||||
|
||||
@@ -329,8 +329,8 @@
|
||||
<field name="arch" type="xml">
|
||||
<form string="Mass Mailing">
|
||||
<header style="min-height:31px;">
|
||||
<button name="action_test_mailing" type="object" class="oe_highlight" attrs="{'invisible': [('body_html', '=', False)]}" string="Test Mailing"/>
|
||||
<button name="put_in_queue" type="object" attrs="{'invisible': ['|', ('state', 'in', ('in_queue', 'done')), ('body_html', '=', False)]}" class="oe_highlight" string="Send to All"/>
|
||||
<button name="action_test_mailing" type="object" class="oe_highlight" string="Test Mailing"/>
|
||||
<button name="put_in_queue" type="object" attrs="{'invisible': ['|', ('state', 'in', ('in_queue', 'done'))]}" class="oe_highlight" string="Send to All"/>
|
||||
<button name="cancel_mass_mailing" type="object" attrs="{'invisible': [('state', '!=', 'in_queue')]}" class="oe_highlight" string="Cancel"/>
|
||||
<button name="retry_failed_mail" type="object" attrs="{'invisible': ['|', ('state', '!=', 'done'), ('failed', '=', 0)]}" class="oe_highlight" string="Retry"/>
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import datetime
|
||||
import random
|
||||
import re
|
||||
import string
|
||||
|
||||
from lxml.html import parse
|
||||
@@ -11,6 +12,7 @@ from urlparse import urlparse
|
||||
|
||||
from openerp import models, fields, api, _
|
||||
|
||||
URL_REGEX = r'(\bhref=[\'"]([^\'"]+)[\'"])'
|
||||
|
||||
def VALIDATE_URL(url):
|
||||
if urlparse(url).scheme not in ('http', 'https', 'ftp', 'ftps'):
|
||||
@@ -41,6 +43,33 @@ class website_links(models.Model):
|
||||
short_url_host = fields.Char(string='Host of the short URL', compute='_compute_short_url_host')
|
||||
icon_src = fields.Char(string='Favicon Source', compute='_compute_icon_src')
|
||||
|
||||
# List of link's href that will not be converted by the shortener
|
||||
links_backlist = ['/unsubscribe_from_list']
|
||||
|
||||
@api.model
|
||||
def convert_links(self, html, utm_mixin):
|
||||
for match in re.findall(URL_REGEX, html):
|
||||
href = match[0]
|
||||
long_url = match[1]
|
||||
|
||||
if not [s for s in self.links_backlist if s in long_url]:
|
||||
vals = {'url': long_url}
|
||||
|
||||
if utm_mixin.campaign_id:
|
||||
vals['campaign_id'] = utm_mixin.campaign_id.id
|
||||
if utm_mixin.source_id:
|
||||
vals['source_id'] = utm_mixin.source_id.id
|
||||
if utm_mixin.medium_id:
|
||||
vals['medium_id'] = utm_mixin.medium_id.id
|
||||
|
||||
link = self.create(vals)
|
||||
shorten_url = self.browse(link.id)[0].short_url
|
||||
|
||||
if shorten_url:
|
||||
new_href = href.replace(long_url, shorten_url)
|
||||
html = html.replace(href, new_href)
|
||||
return html
|
||||
|
||||
@api.one
|
||||
@api.depends('link_click_ids.link_id')
|
||||
def _compute_count(self):
|
||||
|
||||
Reference in New Issue
Block a user