[IMP] mail: add message_log with view helper

This commit allows to log a note and bypass notification process using a
template. The method _message_log_with_view has been added. This method
render the template using the given view and values (in kwargs) to make the
body before calling _message_log().

This process is done by the intermediary method _message_compose_with_view that
is now common to message_post_with_view and _message_log_with_view has it
follows the same template preparation process.

Task ID: 2459416
Linked ENT PR: odoo/enterprise#17553
This commit is contained in:
David Beguin
2021-06-01 13:34:54 +00:00
committed by Thibault Delavallée
parent 46d6c09c75
commit 0ea27f430c
5 changed files with 37 additions and 10 deletions
+2 -1
View File
@@ -351,7 +351,8 @@
<a href="/web#action=mail.action_discuss&amp;active_id=mail.box_moderation" style="background-color: #1abc9c; padding: 20px; text-decoration: none; color: #fff; border-radius: 5px; font-size: 16px;" class="o_default_snippet_text">Moderate Messages</a>
<br/><br/><br/>
</div>
<p>Thank you!</p</div>
<p>Thank you!</p>
</div>
</template>
<template id="mail_channel_send_guidelines">
+19 -8
View File
@@ -1887,12 +1887,12 @@ class MailThread(models.AbstractModel):
# MESSAGE POST TOOLS
# ------------------------------------------------------
def message_post_with_view(self, views_or_xmlid, **kwargs):
""" Helper method to send a mail / post a message using a view_id to
render using the ir.qweb engine. This method is stand alone, because
there is nothing in template and composer that allows to handle
views in batch. This method should probably disappear when templates
handle ir ui views. """
def _message_compose_with_view(self, views_or_xmlid, message_log=False, **kwargs):
""" Helper method to send a mail / post a message / log a note using
a view_id to render using the ir.qweb engine. This method is stand
alone, because there is nothing in template and composer that allows
to handle views in batch. This method should probably disappear when
templates handle ir ui views. """
values = kwargs.pop('values', None) or dict()
try:
from odoo.addons.http_routing.models.ir_http import slug
@@ -1908,8 +1908,15 @@ class MailThread(models.AbstractModel):
for record in self:
values['object'] = record
rendered_template = views._render(values, engine='ir.qweb', minimal_qcontext=True)
kwargs['body'] = rendered_template
record.message_post_with_template(False, **kwargs)
if message_log:
return record._message_log(body=rendered_template, **kwargs)
else:
kwargs['body'] = rendered_template
return record.message_post_with_template(False, **kwargs)
def message_post_with_view(self, views_or_xmlid, **kwargs):
""" Helper method to send a mail / post a message using a view_id """
self._message_compose_with_view(views_or_xmlid, **kwargs)
def message_post_with_template(self, template_id, email_layout_xmlid=None, auto_commit=False, **kwargs):
""" Helper method to send a mail with a template
@@ -1986,6 +1993,10 @@ class MailThread(models.AbstractModel):
MailThread._notify_thread(new_message, values, **notif_kwargs)
return new_message
def _message_log_with_view(self, views_or_xmlid, **kwargs):
""" Helper method to log a note using a view_id without notifying followers. """
return self._message_compose_with_view(views_or_xmlid, message_log=True, **kwargs)
def _message_log(self, *, body='', author_id=None, email_from=None, subject=False, message_type='notification', **kwargs):
""" Shortcut allowing to post note on a document. It does not perform
any notification and pre-computes some values to have a short code
+1 -1
View File
@@ -14,8 +14,8 @@ tests independently to functional aspects of other models. """,
'security/ir.model.access.csv',
'security/test_mail_security.xml',
'data/data.xml',
'data/mail_template_data.xml',
'data/subtype_data.xml',
'data/template_data.xml',
],
'demo': [
],
@@ -33,4 +33,8 @@
</t>
</template>
<template id="mail_template_simple_test">
<p>Hello <t t-esc="partner.name"/>,</p>
</template>
</odoo>
@@ -280,6 +280,17 @@ class TestMessagePost(TestMailCommon, TestRecipients):
self.assertEqual(new_note.email_from, formataddr((self.user_employee.name, self.user_employee.email)))
self.assertEqual(new_note.notified_partner_ids, self.env['res.partner'])
def test_post_log_with_view(self):
new_note = self.test_record.with_user(self.user_employee)._message_log_with_view(
'test_mail.mail_template_simple_test', values={'partner': self.user_employee.partner_id}
)
self.assertEqual(new_note.subtype_id, self.env.ref('mail.mt_note'))
self.assertTrue('<p>Hello %s,</p>' % self.user_employee.name in new_note.body)
self.assertEqual(new_note.author_id, self.user_employee.partner_id)
self.assertEqual(new_note.email_from, formataddr((self.user_employee.name, self.user_employee.email)))
self.assertEqual(new_note.notified_partner_ids, self.env['res.partner'])
@mute_logger('odoo.addons.mail.models.mail_mail')
def test_post_notify(self):
self.user_employee.write({'notification_type': 'inbox'})