[MOV] mass_mailing_sms,link_tracker: Move convert_links_text to link_tracker
This method being needed in the new social app, it made sense to move it to the link_tracker module to make it available for mass_mailing_sms and social to avoid unnecessary duplicated code. LINKS: closes odoo/odoo#34973 Taskid: 2045210 Pr: 37143 Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
This commit is contained in:
committed by
Aurélien Warnon
parent
6c531b3afb
commit
79bfdbf30c
@@ -15,6 +15,7 @@ from odoo import models, fields, api, _
|
||||
|
||||
|
||||
URL_REGEX = r'(\bhref=[\'"](?!mailto:|tel:|sms:)([^\'"]+)[\'"])'
|
||||
TEXT_URL_REGEX = r'(https?:\/\/(www\.)?[a-zA-Z0-9@:%._\+~#=/-]{1,64})'
|
||||
|
||||
|
||||
def VALIDATE_URL(url):
|
||||
@@ -164,6 +165,27 @@ class LinkTracker(models.Model):
|
||||
|
||||
return html
|
||||
|
||||
def _convert_links_text(self, body, vals, blacklist=None):
|
||||
shortened_schema = self.env['ir.config_parameter'].sudo().get_param('web.base.url') + '/r/'
|
||||
unsubscribe_schema = self.env['ir.config_parameter'].sudo().get_param('web.base.url') + '/sms/'
|
||||
for match in re.findall(TEXT_URL_REGEX, body):
|
||||
original_url = match[0]
|
||||
# don't shorten already-shortened links or links towards unsubscribe page
|
||||
if original_url.startswith(shortened_schema) or original_url.startswith(unsubscribe_schema):
|
||||
continue
|
||||
# support blacklist items in path, like /u/
|
||||
parsed = urls.url_parse(original_url, scheme='http')
|
||||
if blacklist and any(item in parsed.path for item in blacklist):
|
||||
continue
|
||||
|
||||
vals['url'] = utils.unescape(original_url)
|
||||
link = self.create(vals)
|
||||
shortened_url = link.short_url
|
||||
if shortened_url:
|
||||
body = body.replace(original_url, shortened_url, 1)
|
||||
|
||||
return body
|
||||
|
||||
def action_view_statistics(self):
|
||||
action = self.env['ir.actions.act_window'].for_xml_id('link_tracker', 'link_tracker_click_action_statistics')
|
||||
action['domain'] = [('link_id', '=', self.id)]
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from . import link_tracker
|
||||
from . import mailing_contact
|
||||
from . import mailing_list
|
||||
from . import mailing_mailing
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
import re
|
||||
import werkzeug
|
||||
|
||||
from odoo import models
|
||||
from odoo.addons.mass_mailing_sms.models.sms_sms import TEXT_URL_REGEX
|
||||
|
||||
|
||||
class LinkTracker(models.Model):
|
||||
_inherit = "link.tracker"
|
||||
|
||||
def _convert_links_text(self, body, vals, blacklist=None):
|
||||
shortened_schema = self.env['ir.config_parameter'].sudo().get_param('web.base.url') + '/r/'
|
||||
unsubscribe_schema = self.env['ir.config_parameter'].sudo().get_param('web.base.url') + '/sms/'
|
||||
for match in re.findall(TEXT_URL_REGEX, body):
|
||||
original_url = match[0]
|
||||
# don't shorten already-shortened links or links towards unsubscribe page
|
||||
if original_url.startswith(shortened_schema) or original_url.startswith(unsubscribe_schema):
|
||||
continue
|
||||
# support blacklist items in path, like /u/
|
||||
parsed = werkzeug.urls.url_parse(original_url, scheme='http')
|
||||
if blacklist and any(item in parsed.path for item in blacklist):
|
||||
continue
|
||||
|
||||
vals['url'] = werkzeug.utils.unescape(original_url)
|
||||
link = self.create(vals)
|
||||
shortened_url = link.short_url
|
||||
if shortened_url:
|
||||
body = body.replace(original_url, shortened_url)
|
||||
|
||||
return body
|
||||
Reference in New Issue
Block a user