[FIX] website(_sale): stop creating message manually

In website(_sale) messages are created from website forms. However those
are technical models, you should always use the MailThread API notably to
ensure values coherency. In our case using message_log seems to be what
original committers wanted to do (even creating a message as a comment
which has no effect as the notification process is not called that way).

Task-2710804 (Mail: Clean MailThread Posting API)

Part-of: odoo/odoo#99482
This commit is contained in:
Thibault Delavallée
2023-01-17 20:58:31 +01:00
parent 7b96e7cb22
commit eccac1ce7b
3 changed files with 21 additions and 30 deletions
+14 -21
View File
@@ -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': _('<p>Attached files : </p>'),
'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(_('<p>Attached files : </p>')),
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:
+6 -8
View File
@@ -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'])
+1 -1
View File
@@ -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