[IMP] mail: make notification layout a real parameter of message post

Purpose of this commit is to add a new parameter to message post that
is the layout used when sending notification emails. It will soon
replace the context use of custom_layout in some case. It will also
allow to control a bit the notification process from the message_post
API in more or less planned future mail updates.

This commit also slightly changes the way notification process handles
deleted or unknown rendering templates. Before this commit any missing
custom template was replaced by the base template. A missing base
template was leading to a crash. Now it simply logs a warning and avoid
crashing.
This commit is contained in:
Thibault Delavallée
2018-01-29 14:53:54 +01:00
parent 951cb8bf40
commit 304e6cf90b
8 changed files with 30 additions and 22 deletions
+2 -2
View File
@@ -1197,7 +1197,7 @@ class Lead(models.Model):
update_vals[key] = res.group(2).lower()
return super(Lead, self).message_update(msg_dict, update_vals=update_vals)
def _message_post_after_hook(self, message, values):
def _message_post_after_hook(self, message, values, notif_layout):
if self.email_from and not self.partner_id:
# we consider that posting a message with a specified recipient (not a follower, a specific one)
# on a document without customer means that it was created through the chatter using
@@ -1208,7 +1208,7 @@ class Lead(models.Model):
('partner_id', '=', False),
('email_from', '=', new_partner.email),
('stage_id.fold', '=', False)]).write({'partner_id': new_partner.id})
return super(Lead, self)._message_post_after_hook(message, values)
return super(Lead, self)._message_post_after_hook(message, values, notif_layout)
@api.multi
def message_partner_info_from_emails(self, emails, link_mail=False):
+2 -2
View File
@@ -448,7 +448,7 @@ class EventRegistration(models.Model):
pass
return recipients
def _message_post_after_hook(self, message, values):
def _message_post_after_hook(self, message, values, notif_layout):
if self.email and not self.partner_id:
# we consider that posting a message with a specified recipient (not a follower, a specific one)
# on a document without customer means that it was created through the chatter using
@@ -460,7 +460,7 @@ class EventRegistration(models.Model):
('email', '=', new_partner.email),
('state', 'not in', ['cancel']),
]).write({'partner_id': new_partner.id})
return super(EventRegistration, self)._message_post_after_hook(message, values)
return super(EventRegistration, self)._message_post_after_hook(message, values, notif_layout)
@api.multi
def action_send_badge_email(self):
@@ -377,7 +377,7 @@ class Applicant(models.Model):
defaults.update(custom_values)
return super(Applicant, self).message_new(msg, custom_values=defaults)
def _message_post_after_hook(self, message, values):
def _message_post_after_hook(self, message, values, notif_layout):
if self.email_from and not self.partner_id:
# we consider that posting a message with a specified recipient (not a follower, a specific one)
# on a document without customer means that it was created through the chatter using
@@ -388,7 +388,7 @@ class Applicant(models.Model):
('partner_id', '=', False),
('email_from', '=', new_partner.email),
('stage_id.fold', '=', False)]).write({'partner_id': new_partner.id})
return super(Applicant, self)._message_post_after_hook(message, values)
return super(Applicant, self)._message_post_after_hook(message, values, notif_layout)
@api.multi
def create_employee_from_applicant(self):
+2 -2
View File
@@ -803,7 +803,7 @@ class Message(models.Model):
#------------------------------------------------------
@api.multi
def _notify(self, force_send=False, send_after_commit=True, user_signature=True):
def _notify(self, layout=False, force_send=False, send_after_commit=True, user_signature=True):
""" Compute recipients to notify based on specified recipients and document
followers. Delegate notification to partners to send emails and bus notifications
and to channels to broadcast messages on channels """
@@ -854,7 +854,7 @@ class Message(models.Model):
('id', 'in', (partners_sudo - notif_partners).ids),
('channel_ids', 'in', email_channels.ids),
('email', '!=', self_sudo.author_id.email or self_sudo.email_from),
])._notify(self, force_send=force_send, send_after_commit=send_after_commit, user_signature=user_signature)
])._notify(self, layout=layout, force_send=force_send, send_after_commit=send_after_commit, user_signature=user_signature)
notif_partners._notify_by_chat(self)
+7 -5
View File
@@ -1759,9 +1759,10 @@ class MailThread(models.AbstractModel):
@api.multi
@api.returns('self', lambda value: value.id)
def message_post(self, body='', subject=None, message_type='notification',
subtype=None, parent_id=False, attachments=None,
content_subtype='html', **kwargs):
def message_post(self, body='', subject=None,
message_type='notification', subtype=None,
parent_id=False, attachments=None, content_subtype='html',
notif_layout=False, **kwargs):
""" Post a new message in an existing thread, returning the new
mail.message ID.
:param int thread_id: thread ID to post into, or list with one ID;
@@ -1886,15 +1887,16 @@ class MailThread(models.AbstractModel):
# Post the message
new_message = MailMessage.create(values)
self._message_post_after_hook(new_message, values)
self._message_post_after_hook(new_message, values, notif_layout)
return new_message
def _message_post_after_hook(self, message, values):
def _message_post_after_hook(self, message, values, notif_layout):
""" Hook to add custom behavior after having posted the message. Both
message and computed value are given, to try to lessen query count by
using already-computed values instead of having to rebrowse things. """
# Notify recipients of the newly-created message (Inbox / Email + channels)
message._notify(
layout=notif_layout,
force_send=self.env.context.get('mail_notify_force_send', True),
user_signature=self.env.context.get('mail_notify_user_signature', True)
)
+11 -5
View File
@@ -147,7 +147,7 @@ class Partner(models.Model):
})
@api.multi
def _notify(self, message, force_send=False, send_after_commit=True, user_signature=True):
def _notify(self, message, layout=False, force_send=False, send_after_commit=True, user_signature=True):
""" Method to send email linked to notified messages. The recipients are
the recordset on which this method is called.
@@ -159,11 +159,17 @@ class Partner(models.Model):
return True
# existing custom notification email
base_template = None
if message.model and self._context.get('custom_layout', False):
base_template = self.env.ref(self._context['custom_layout'], raise_if_not_found=False)
if not base_template:
base_template = self.env.ref('mail.mail_template_data_notification_email_default')
template_xmlid = self._context['custom_layout']
elif layout:
template_xmlid = layout
else:
template_xmlid = 'mail.mail_template_data_notification_email_default'
try:
base_template = self.env.ref(template_xmlid, raise_if_not_found=True)
except ValueError:
_logger.warning('QWeb template %s not found when sending notification emails. Skipping.' % (template_xmlid))
return False
base_template_ctx = self._notify_prepare_template_context(message)
if not user_signature:
+2 -2
View File
@@ -985,7 +985,7 @@ class Task(models.Model):
res['headers'] = repr(headers)
return res
def _message_post_after_hook(self, message, values):
def _message_post_after_hook(self, message, values, notif_layout):
if self.email_from and not self.partner_id:
# we consider that posting a message with a specified recipient (not a follower, a specific one)
# on a document without customer means that it was created through the chatter using
@@ -996,7 +996,7 @@ class Task(models.Model):
('partner_id', '=', False),
('email_from', '=', new_partner.email),
('stage_id.fold', '=', False)]).write({'partner_id': new_partner.id})
return super(Task, self)._message_post_after_hook(message, values)
return super(Task, self)._message_post_after_hook(message, values, notif_layout)
def action_assign_to_me(self):
self.write({'user_id': self.env.user.id})
@@ -159,7 +159,7 @@ class Track(models.Model):
track._message_add_suggested_recipient(recipients, email=track.partner_email, reason=_('Speaker Email'))
return recipients
def _message_post_after_hook(self, message, values):
def _message_post_after_hook(self, message, values, notif_layout):
if self.partner_email and not self.partner_id:
# we consider that posting a message with a specified recipient (not a follower, a specific one)
# on a document without customer means that it was created through the chatter using
@@ -171,7 +171,7 @@ class Track(models.Model):
('partner_email', '=', new_partner.email),
('stage_id.is_cancel', '=', False),
]).write({'partner_id': new_partner.id})
return super(Track, self)._message_post_after_hook(message, values)
return super(Track, self)._message_post_after_hook(message, values, notif_layout)
@api.multi
def open_track_speakers_list(self):