summaryrefslogtreecommitdiff
path: root/addons/link_tracker/models/link_tracker.py
diff options
context:
space:
mode:
authorThibault Delavallée <tde@odoo.com>2020-03-11 12:42:48 +0000
committerThibault Delavallée <tde@odoo.com>2020-03-24 10:25:04 +0000
commitaaefa35e29f7e87e8e914cf1d9eb68af7d26464a (patch)
tree0835ddf9cea2ffebfa0ca441411b32e3fa1db502 /addons/link_tracker/models/link_tracker.py
parent3d86a29aeb21c7e5812c988866117a495f20ebed (diff)
[MOV] link_tracker, various: more link shortening tools to mail.render.mixin
PURPOSE Move links shortening tools on mail.render.mixin to have rendering and post procesing tools available on that mixin. SPECIFICATIONS Move html and text links shortening methods on mail.render.mixin class. That way rendering and post processing is done in that mixin instead of being specific to link.tracker model. Indeed those methods are tools and not really business related methods. Future commits will improve the way mail and sms composer use the render mixin. This commit is therefore both a cleaning and preparing commit. LINKS Task ID 1963529 Community PR odoo/odoo#32397
Diffstat (limited to 'addons/link_tracker/models/link_tracker.py')
-rw-r--r--addons/link_tracker/models/link_tracker.py58
1 files changed, 5 insertions, 53 deletions
diff --git a/addons/link_tracker/models/link_tracker.py b/addons/link_tracker/models/link_tracker.py
index 3af4accb9ba..ec73046e1be 100644
--- a/addons/link_tracker/models/link_tracker.py
+++ b/addons/link_tracker/models/link_tracker.py
@@ -1,28 +1,14 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
-import base64
-import datetime
import random
-import re
import requests
import string
from lxml import html
-from werkzeug import urls, utils
+from werkzeug import urls
-from odoo import models, fields, api, _
-
-
-URL_REGEX = r'(\bhref=[\'"](?!mailto:|tel:|sms:)([^\'"]+)[\'"])'
-TEXT_URL_REGEX = r'https?://[a-zA-Z0-9@:%._\+~#=/-]+(?:\?\S+)?'
-
-
-def VALIDATE_URL(url):
- if urls.url_parse(url).scheme not in ('http', 'https', 'ftp', 'ftps'):
- return 'http://' + url
-
- return url
+from odoo import tools, models, fields, api, _
class LinkTracker(models.Model):
@@ -101,7 +87,7 @@ class LinkTracker(models.Model):
if 'url' not in create_vals:
raise ValueError('URL field required')
else:
- create_vals['url'] = VALIDATE_URL(vals['url'])
+ create_vals['url'] = tools.validate_url(vals['url'])
search_domain = []
for fname, value in create_vals.items():
@@ -129,44 +115,10 @@ class LinkTracker(models.Model):
@api.model
def convert_links(self, html, vals, blacklist=None):
- for match in re.findall(URL_REGEX, html):
-
- short_schema = self.env['ir.config_parameter'].sudo().get_param('web.base.url') + '/r/'
-
- href = match[0]
- long_url = match[1]
-
- vals['url'] = utils.unescape(long_url)
-
- if not blacklist or not [s for s in blacklist if s in long_url] and not long_url.startswith(short_schema):
- 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
+ raise NotImplementedError('Moved on mail.render.mixin')
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 original_url in re.findall(TEXT_URL_REGEX, body):
- # 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
+ raise NotImplementedError('Moved on mail.render.mixin')
def action_view_statistics(self):
action = self.env['ir.actions.act_window'].for_xml_id('link_tracker', 'link_tracker_click_action_statistics')