diff --git a/addons/website/controllers/form.py b/addons/website/controllers/form.py index 5c87a5b68b0..3303671a4d8 100644 --- a/addons/website/controllers/form.py +++ b/addons/website/controllers/form.py @@ -4,14 +4,15 @@ import base64 import json +from markupsafe import Markup from psycopg2 import IntegrityError from werkzeug.exceptions import BadRequest from odoo import http, SUPERUSER_ID, _ +from odoo.addons.base.models.ir_qweb_fields import nl2br from odoo.http import request from odoo.tools import plaintext2html from odoo.exceptions import ValidationError, UserError -from odoo.addons.base.models.ir_qweb_fields import nl2br class WebsiteForm(http.Controller): @@ -234,14 +235,11 @@ class WebsiteForm(http.Controller): if default_field.ttype == 'html' or model_name == 'mail.mail': custom_content = nl2br(custom_content) record.update({default_field.name: custom_content}) - else: - values = { - 'body': nl2br(custom_content), - 'model': model_name, - 'message_type': 'comment', - 'res_id': record.id, - } - request.env['mail.message'].with_user(SUPERUSER_ID).create(values) + elif hasattr(record, '_message_log'): + record._message_log( + body=custom_content, + message_type='comment', + ) return record.id @@ -269,20 +267,15 @@ class WebsiteForm(http.Controller): else: orphan_attachment_ids.append(attachment_id.id) - if model_name != 'mail.mail': + if model_name != 'mail.mail' and hasattr(record, '_message_log') and orphan_attachment_ids: # If some attachments didn't match a field on the model, # we create a mail.message to link them to the record - if orphan_attachment_ids: - values = { - 'body': _('
Attached files :
'), - 'model': model_name, - 'message_type': 'comment', - 'res_id': id_record, - 'attachment_ids': [(6, 0, orphan_attachment_ids)], - 'subtype_id': request.env['ir.model.data']._xmlid_to_res_id('mail.mt_comment'), - } - request.env['mail.message'].with_user(SUPERUSER_ID).create(values) - else: + record._message_log( + attachment_ids=[(6, 0, orphan_attachment_ids)], + body=Markup(_('Attached files :
')), + message_type='comment', + ) + elif model_name == 'mail.mail' and orphan_attachment_ids: # If the model is mail.mail then we have no other choice but to # attach the custom binary field files on the attachment_ids field. for attachment_id_id in orphan_attachment_ids: diff --git a/addons/website_sale/controllers/main.py b/addons/website_sale/controllers/main.py index 04f840eca29..74a7da3dbbf 100644 --- a/addons/website_sale/controllers/main.py +++ b/addons/website_sale/controllers/main.py @@ -4,14 +4,15 @@ from collections import defaultdict from itertools import product as cartesian_product import json import logging + from datetime import datetime +from markupsafe import Markup from werkzeug.exceptions import Forbidden, NotFound from werkzeug.urls import url_decode, url_encode, url_parse from odoo import fields, http, SUPERUSER_ID, tools, _ from odoo.fields import Command from odoo.http import request -from odoo.addons.base.models.ir_qweb_fields import nl2br from odoo.addons.http_routing.models.ir_http import slug from odoo.addons.payment import utils as payment_utils from odoo.addons.payment.controllers import portal as payment_portal @@ -113,13 +114,10 @@ class WebsiteSaleForm(WebsiteForm): order.write(data['record']) if data['custom']: - values = { - 'body': nl2br(data['custom']), - 'model': 'sale.order', - 'message_type': 'comment', - 'res_id': order.id, - } - request.env['mail.message'].with_user(SUPERUSER_ID).create(values) + order._message_log( + body=data['custom'], + message_type='comment', + ) if data['attachments']: self.insert_attachment(model_record, order.id, data['attachments']) diff --git a/odoo/addons/base/tests/test_mail.py b/odoo/addons/base/tests/test_mail.py index e3be6e0d812..b4e91e0fa2b 100644 --- a/odoo/addons/base/tests/test_mail.py +++ b/odoo/addons/base/tests/test_mail.py @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. - from unittest.mock import patch + import email.policy import email.message import re