From 304e6cf90b9ef969c809030b3e65ac6b667eea0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= <tde@odoo.com> Date: Fri, 12 Jan 2018 11:38:12 +0100 Subject: [PATCH] [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. --- addons/crm/models/crm_lead.py | 4 ++-- addons/event/models/event.py | 4 ++-- addons/hr_recruitment/models/hr_recruitment.py | 4 ++-- addons/mail/models/mail_message.py | 4 ++-- addons/mail/models/mail_thread.py | 12 +++++++----- addons/mail/models/res_partner.py | 16 +++++++++++----- addons/project/models/project.py | 4 ++-- addons/website_event_track/models/event_track.py | 4 ++-- 8 files changed, 30 insertions(+), 22 deletions(-) diff --git a/addons/crm/models/crm_lead.py b/addons/crm/models/crm_lead.py index 3d50eb10d75e..1bb62b0c50b6 100644 --- a/addons/crm/models/crm_lead.py +++ b/addons/crm/models/crm_lead.py @@ -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): diff --git a/addons/event/models/event.py b/addons/event/models/event.py index 70a71080650a..8551c6ec3891 100644 --- a/addons/event/models/event.py +++ b/addons/event/models/event.py @@ -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): diff --git a/addons/hr_recruitment/models/hr_recruitment.py b/addons/hr_recruitment/models/hr_recruitment.py index 765d13a2d525..6d41a839f365 100644 --- a/addons/hr_recruitment/models/hr_recruitment.py +++ b/addons/hr_recruitment/models/hr_recruitment.py @@ -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): diff --git a/addons/mail/models/mail_message.py b/addons/mail/models/mail_message.py index 4c51c4e635e1..1973c204b133 100644 --- a/addons/mail/models/mail_message.py +++ b/addons/mail/models/mail_message.py @@ -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) diff --git a/addons/mail/models/mail_thread.py b/addons/mail/models/mail_thread.py index 4d9ff5f3369b..1473953a91b1 100644 --- a/addons/mail/models/mail_thread.py +++ b/addons/mail/models/mail_thread.py @@ -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) ) diff --git a/addons/mail/models/res_partner.py b/addons/mail/models/res_partner.py index 77d61185ed03..7f9149a92553 100644 --- a/addons/mail/models/res_partner.py +++ b/addons/mail/models/res_partner.py @@ -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: diff --git a/addons/project/models/project.py b/addons/project/models/project.py index 548013fae463..34a3e5b5fa52 100644 --- a/addons/project/models/project.py +++ b/addons/project/models/project.py @@ -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}) diff --git a/addons/website_event_track/models/event_track.py b/addons/website_event_track/models/event_track.py index 3aeda823ced8..ae02a039c4ea 100644 --- a/addons/website_event_track/models/event_track.py +++ b/addons/website_event_track/models/event_track.py @@ -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): -- GitLab