Skip to content

Commit

Permalink
[IMP]mail_quoted_reply: Separate reply body
Browse files Browse the repository at this point in the history
For long replies the message composer gets extremely laggy and slow. To prevent that the reply body is stored in a separate field.
  • Loading branch information
ChristophAbenthungCibex committed Dec 3, 2024
1 parent 6217fff commit 7a8dee2
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 3 deletions.
4 changes: 3 additions & 1 deletion mail_quoted_reply/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"author": "Creu Blanca,Odoo Community Association (OCA)",
"website": "https://github.com/OCA/social",
"depends": ["mail"],
"data": [],
"data": [
"views/mail_compose_message_views.xml",
],
"assets": {
"web.assets_backend": [
"/mail_quoted_reply/static/src/models/*.js",
Expand Down
34 changes: 32 additions & 2 deletions mail_quoted_reply/models/mail_compose_message.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,49 @@
from markupsafe import Markup

from odoo import api, models, tools
from odoo import api, fields, models, tools


class MailComposeMessage(models.TransientModel):
_inherit = "mail.compose.message"

is_reply_readonly = fields.Boolean(default=True, string="Reply Readonly")
reply_body = fields.Html(default="", string="Reply body")
is_separate_body = fields.Boolean(compute="_compute_is_separate_body")

@api.onchange("template_id")
def _onchange_template_id_wrapper(self):
super()._onchange_template_id_wrapper()
context = self._context
if "is_quoted_reply" in context.keys() and context["is_quoted_reply"]:
self.body += Markup(context["quote_body"])
if self.is_separate_body:
self.reply_body = context["quote_body"]
else:
self.body += Markup(context["quote_body"])

Check warning on line 21 in mail_quoted_reply/models/mail_compose_message.py

View check run for this annotation

Codecov / codecov/patch

mail_quoted_reply/models/mail_compose_message.py#L21

Added line #L21 was not covered by tests
return

@api.onchange("is_reply_readonly")
def _onchange_is_reply_readonly(self):
if self.reply_body:
self.reply_body = Markup(self.reply_body)

Check warning on line 27 in mail_quoted_reply/models/mail_compose_message.py

View check run for this annotation

Codecov / codecov/patch

mail_quoted_reply/models/mail_compose_message.py#L27

Added line #L27 was not covered by tests

@api.depends("reply_body")
def _compute_is_separate_body(self):
parameter_string = (
self.env["ir.config_parameter"]
.sudo()
.get_param("mail_quoted_reply.separate_reply_body", "")
)
self.is_separate_body = parameter_string.lower() not in ["", "false", "0"]

def get_mail_values(self, res_ids):
results = super(MailComposeMessage, self).get_mail_values(res_ids)
if self.is_separate_body and self.reply_body:
for res_id in res_ids:
values = results.get(res_id)
reply_body = Markup(self.reply_body)
values.update({"body": values.get("body") + reply_body})
return results

@api.model
def get_record_data(self, values):
result = super().get_record_data(values)
Expand Down
29 changes: 29 additions & 0 deletions mail_quoted_reply/tests/test_reply.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,32 @@ def test_reply(self):
)
self.assertTrue(new_message)
self.assertEqual(1, len(new_message))

def test_reply_separate_body(self):
self.env["ir.config_parameter"].sudo().create(
{
"key": "mail_quoted_reply.separate_reply_body",
"value": "True",
}
)
partner = self.env["res.partner"].create({"name": "demo partner"})
message = partner.message_post(
body="demo message",
message_type="email",
partner_ids=self.env.ref("base.partner_demo").ids,
)
partner.invalidate_recordset()
action = message.reply_message()
wizard = (
self.env[action["res_model"]].with_context(**action["context"]).create({})
)
wizard._onchange_template_id_wrapper()
self.assertTrue("<p>demo message</p>" in wizard.reply_body)
wizard.action_send_mail()
new_message = partner.message_ids.filtered(
lambda r: r.message_type != "notification" and r != message
)
self.assertTrue(new_message)
self.assertEqual(1, len(new_message))
new_message = new_message[0]
self.assertTrue("<p>demo message</p>" in new_message.body)
26 changes: 26 additions & 0 deletions mail_quoted_reply/views/mail_compose_message_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="mail_quoted_reply_composer_view_form" model="ir.ui.view">
<field name="model">mail.compose.message</field>
<field name="inherit_id" ref="mail.email_compose_message_wizard_form" />
<field name="arch" type="xml">
<xpath expr="//field[@name='body']" position="after">
<field name="is_separate_body" invisible="1" />
<field
name="reply_body"
attrs="{'readonly': [('is_reply_readonly', '=', True)], 'invisible': [('is_separate_body', '=', False)]}"
force_save="1"
/>
<label
for="is_reply_readonly"
attrs="{'invisible': [('is_separate_body', '=', False)]}"
/>
<field
name="is_reply_readonly"
widget="boolean_toggle"
attrs="{'invisible': [('is_separate_body', '=', False)]}"
/>
</xpath>
</field>
</record>
</odoo>

0 comments on commit 7a8dee2

Please sign in to comment.