From 991eb9a17b76c38e574d14caa56cfdfe47546399 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Debauche=20St=C3=A9phane?= Date: Fri, 22 Nov 2019 10:48:35 +0000 Subject: [PATCH] [FIX] sms: ensure chatter / SMS have the same content When we send a SMS in the contact form view, exact body is sent by SMS and displayed in chatter. If some manual HTML is added chatter will display it as HTML while sms receive HTML tags. We want that the SMS content in the chatter is the same as the SMS sent and that HTML tags are removed to avoid being interpreted. To achieve that goal we call html2plaintext in ``_message_sms`` and in ``prepare_log_body_value`` that are two entry points to send SMS. We also update ``html2plaintext`` to strip result in order to avoid having unnecessary spaces left. Task ID 2126123 PR #40441 closes odoo/odoo#46362 X-original-commit: 4b7b14a5ea5e6ab521838c01813fa478154682fd Signed-off-by: Thibault Delavallee (tde) --- addons/sms/models/mail_thread.py | 6 +++--- addons/sms/wizard/sms_composer.py | 3 ++- odoo/tools/mail.py | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/addons/sms/models/mail_thread.py b/addons/sms/models/mail_thread.py index 1bbf9f20f40..add3e7f2dc0 100644 --- a/addons/sms/models/mail_thread.py +++ b/addons/sms/models/mail_thread.py @@ -5,7 +5,7 @@ import logging from odoo import api, models, fields from odoo.addons.phone_validation.tools import phone_validation -from odoo.tools import html2plaintext +from odoo.tools import html2plaintext, plaintext2html _logger = logging.getLogger(__name__) @@ -195,7 +195,7 @@ class MailThread(models.AbstractModel): subtype_id = self.env['ir.model.data'].xmlid_to_res_id('mail.mt_note') return self.message_post( - body=body, partner_ids=partner_ids or [], # TDE FIXME: temp fix otherwise crash mail_thread.py + body=plaintext2html(html2plaintext(body)), partner_ids=partner_ids or [], # TDE FIXME: temp fix otherwise crash mail_thread.py message_type='sms', subtype_id=subtype_id, sms_numbers=sms_numbers, sms_pid_to_number=sms_pid_to_number, **kwargs @@ -232,7 +232,7 @@ class MailThread(models.AbstractModel): # pre-compute SMS data body = msg_vals['body'] if msg_vals and msg_vals.get('body') else message.body sms_base_vals = { - 'body': html2plaintext(body).rstrip('\n'), + 'body': html2plaintext(body), 'mail_message_id': message.id, 'state': 'outgoing', } diff --git a/addons/sms/wizard/sms_composer.py b/addons/sms/wizard/sms_composer.py index 6ad9ea16de8..3e25a1e6018 100644 --- a/addons/sms/wizard/sms_composer.py +++ b/addons/sms/wizard/sms_composer.py @@ -6,6 +6,7 @@ from ast import literal_eval from odoo import api, fields, models, _ from odoo.addons.phone_validation.tools import phone_validation from odoo.exceptions import UserError +from odoo.tools import html2plaintext class SendSMS(models.TransientModel): @@ -275,7 +276,7 @@ class SendSMS(models.TransientModel): def _prepare_log_body_values(self, sms_records_values): result = {} for record_id, sms_values in sms_records_values.items(): - result[record_id] = sms_values['body'] + result[record_id] = html2plaintext(sms_values['body']) return result def _prepare_mass_log_values(self, records, sms_records_values): diff --git a/odoo/tools/mail.py b/odoo/tools/mail.py index 737afc8c39a..50467df606e 100644 --- a/odoo/tools/mail.py +++ b/odoo/tools/mail.py @@ -327,7 +327,7 @@ def html2plaintext(html, body_id=None, encoding='utf-8'): html += '\n\n' html += ustr('[%s] %s\n') % (i + 1, url) - return html + return html.strip() def plaintext2html(text, container_tag=False): """ Convert plaintext into html. Content of the text is escaped to manage