From cae1c3977ff2777dcb77e4c2a51deb72ee57db22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= <tde@odoo.com> Date: Fri, 19 Jan 2018 11:17:11 +0100 Subject: [PATCH] [REF] mail: rename notification methods and make them private Purpose of this commit is to perform a light cleaning of the email notification process. It renames method by prefixing them by _notify in order to easily spot them. It also makes them private because they should not be called outside of other methods. Indeed those methods are technical and do not provide useful information to external people. Future commits will probably improve the notification process in order to clean it and try to reduce its query count. This commit perform the following renaming * message_get_email_values -> _notify_specific_email_values * message_get_recipient_values -> _notify_email_recipients * _message_notification_recipients -> _notify_classify_recipients * message_get_reply_to -> _notify_get_reply_to --- addons/crm/models/crm_lead.py | 4 +-- .../hr_recruitment/models/hr_recruitment.py | 4 +-- addons/mail/models/mail_channel.py | 18 ++++++------- addons/mail/models/mail_message.py | 12 ++++----- addons/mail/models/mail_thread.py | 26 ++++++++++++------- addons/mail/models/res_partner.py | 16 ++++++------ addons/mail/wizard/mail_compose_message.py | 8 +++--- addons/project/models/project.py | 18 ++++++------- .../models/mail_channel.py | 22 +++++++--------- 9 files changed, 63 insertions(+), 65 deletions(-) diff --git a/addons/crm/models/crm_lead.py b/addons/crm/models/crm_lead.py index 1bb62b0c50b6..adb6b9733aa9 100644 --- a/addons/crm/models/crm_lead.py +++ b/addons/crm/models/crm_lead.py @@ -1113,9 +1113,9 @@ class Lead(models.Model): return [new_group] + groups @api.model - def message_get_reply_to(self, res_ids, default=None): + def _notify_get_reply_to(self, res_ids, default=None): leads = self.sudo().browse(res_ids) - aliases = self.env['crm.team'].message_get_reply_to(leads.mapped('team_id').ids, default=default) + aliases = self.env['crm.team']._notify_get_reply_to(leads.mapped('team_id').ids, default=default) return {lead.id: aliases.get(lead.team_id.id or 0, False) for lead in leads} @api.multi diff --git a/addons/hr_recruitment/models/hr_recruitment.py b/addons/hr_recruitment/models/hr_recruitment.py index 6d41a839f365..7437d4ab65ed 100644 --- a/addons/hr_recruitment/models/hr_recruitment.py +++ b/addons/hr_recruitment/models/hr_recruitment.py @@ -336,10 +336,10 @@ class Applicant(models.Model): return super(Applicant, self)._track_subtype(init_values) @api.model - def message_get_reply_to(self, ids, default=None): + def _notify_get_reply_to(self, ids, default=None): """ Override to get the reply_to of the parent project. """ applicants = self.sudo().browse(ids) - aliases = self.env['hr.job'].message_get_reply_to(applicants.mapped('job_id').ids, default=default) + aliases = self.env['hr.job']._notify_get_reply_to(applicants.mapped('job_id').ids, default=default) return dict((applicant.id, aliases.get(applicant.job_id and applicant.job_id.id or 0, False)) for applicant in applicants) @api.multi diff --git a/addons/mail/models/mail_channel.py b/addons/mail/models/mail_channel.py index fefa20c53c1d..cde5924207da 100644 --- a/addons/mail/models/mail_channel.py +++ b/addons/mail/models/mail_channel.py @@ -197,15 +197,13 @@ class Channel(models.Model): return groups @api.multi - def message_get_email_values(self, notif_mail=None): + def _notify_specific_email_values(self, message): self.ensure_one() - res = super(Channel, self).message_get_email_values(notif_mail=notif_mail) - headers = {} - if res.get('headers'): - try: - headers.update(safe_eval(res['headers'])) - except Exception: - pass + res = super(Channel, self)._notify_specific_email_values(message) + try: + headers = safe_eval(res.get('headers', dict())) + except Exception: + headers = {} headers['Precedence'] = 'list' # avoid out-of-office replies from MS Exchange # http://blogs.technet.com/b/exchange/archive/2006/10/06/3395024.aspx @@ -229,14 +227,14 @@ class Channel(models.Model): return super(Channel, self).message_receive_bounce(email, partner, mail_id=mail_id) @api.multi - def message_get_recipient_values(self, notif_message=None, recipient_ids=None): + def _notify_email_recipients(self, message, recipient_ids): # real mailing list: multiple recipients (hidden by X-Forge-To) if self.alias_domain and self.alias_name: return { 'email_to': ','.join(formataddr((partner.name, partner.email)) for partner in self.env['res.partner'].sudo().browse(recipient_ids)), 'recipient_ids': [], } - return super(Channel, self).message_get_recipient_values(notif_message=notif_message, recipient_ids=recipient_ids) + return super(Channel, self)._notify_email_recipients(message, recipient_ids) @api.multi @api.returns('self', lambda value: value.id) diff --git a/addons/mail/models/mail_message.py b/addons/mail/models/mail_message.py index 1973c204b133..0f3665c4acb3 100644 --- a/addons/mail/models/mail_message.py +++ b/addons/mail/models/mail_message.py @@ -699,14 +699,14 @@ class Message(models.Model): @api.model def _get_reply_to(self, values): """ Return a specific reply_to: alias of the document through - message_get_reply_to or take the email_from """ + _notify_get_reply_to or take the email_from """ model, res_id, email_from = values.get('model', self._context.get('default_model')), values.get('res_id', self._context.get('default_res_id')), values.get('email_from') # ctx values / defualt_get res ? - if model and hasattr(self.env[model], 'message_get_reply_to'): - # return self.env[model].browse(res_id).message_get_reply_to([res_id], default=email_from)[res_id] - return self.env[model].message_get_reply_to([res_id], default=email_from)[res_id] + if model and hasattr(self.env[model], '_notify_get_reply_to'): + # return self.env[model].browse(res_id)._notify_get_reply_to([res_id], default=email_from)[res_id] + return self.env[model]._notify_get_reply_to([res_id], default=email_from)[res_id] else: - # return self.env['mail.thread'].message_get_reply_to(default=email_from)[None] - return self.env['mail.thread'].message_get_reply_to([None], default=email_from)[None] + # return self.env['mail.thread']._notify_get_reply_to(default=email_from)[None] + return self.env['mail.thread']._notify_get_reply_to([None], default=email_from)[None] @api.model def _get_message_id(self, values): diff --git a/addons/mail/models/mail_thread.py b/addons/mail/models/mail_thread.py index 1473953a91b1..dd2c40965f0d 100644 --- a/addons/mail/models/mail_thread.py +++ b/addons/mail/models/mail_thread.py @@ -661,7 +661,7 @@ class MailThread(models.AbstractModel): return groups @api.multi - def _message_notification_recipients(self, message, recipients): + def _notify_classify_recipients(self, message, recipients): # At this point, all access rights should be ok. We sudo everything to # access rights checks and speedup the computation. result = {} @@ -709,7 +709,7 @@ class MailThread(models.AbstractModel): return result @api.model - def message_get_reply_to(self, res_ids, default=None): + def _notify_get_reply_to(self, res_ids, default=None): """ Returns the preferred reply-to email address that is basically the alias of the document, if it exists. Override this method to implement a custom behavior about reply-to for generated emails. """ @@ -752,9 +752,12 @@ class MailThread(models.AbstractModel): return res @api.multi - def message_get_email_values(self, notif_mail=None): + def _notify_specific_email_values(self, message): """ Get specific notification email values to store on the notification - mail_mail. Void method, inherit it to add custom values. """ + mail.mail. Override to add values related to a specific model. + + :param MailMessage message: mail.message record being notified by email + """ self.ensure_one() database_uuid = self.env['ir.config_parameter'].sudo().get_param('database.uuid') return {'headers': repr({ @@ -763,12 +766,15 @@ class MailThread(models.AbstractModel): })} @api.multi - def message_get_recipient_values(self, notif_message=None, recipient_ids=None): - """ Get specific notification recipient values to store on the notification - mail_mail. Basic method just set the recipient partners as mail_mail - recipients. Inherit this method to add custom behavior like using - recipient email_to to bypass the recipint_ids heuristics in the - mail sending mechanism. """ + def _notify_email_recipients(self, message, recipient_ids): + """ Format email notification recipient values to store on the notification + mail.mail. Basic method just set the recipient partners as mail_mail + recipients. Override to generate other mail values like email_to or + email_cc. + + :param MailMessage message: mail.message record being notified by email + :param list recipient_ids: list of res.partner ids to notify + """ return { 'recipient_ids': [(4, pid) for pid in recipient_ids] } diff --git a/addons/mail/models/res_partner.py b/addons/mail/models/res_partner.py index 7f9149a92553..8004b653e50b 100644 --- a/addons/mail/models/res_partner.py +++ b/addons/mail/models/res_partner.py @@ -98,8 +98,8 @@ class Partner(models.Model): # custom values custom_values = dict() - if message.res_id and message.model in self.env and hasattr(self.env[message.model], 'message_get_email_values'): - custom_values = self.env[message.model].browse(message.res_id).message_get_email_values(message) + if message.res_id and message.model in self.env and hasattr(self.env[message.model], '_notify_specific_email_values'): + custom_values = self.env[message.model].browse(message.res_id)._notify_specific_email_values(message) mail_values = { 'mail_message_id': message.id, @@ -120,11 +120,11 @@ class Partner(models.Model): # cache so should not impact performances. mail_message_id = mail_values.get('mail_message_id') message = self.env['mail.message'].browse(mail_message_id) if mail_message_id else None - if message and message.model and message.res_id and message.model in self.env and hasattr(self.env[message.model], 'message_get_recipient_values'): + if message and message.model and message.res_id and message.model in self.env and hasattr(self.env[message.model], '_notify_email_recipients'): tig = self.env[message.model].browse(message.res_id) - recipient_values = tig.message_get_recipient_values(notif_message=message, recipient_ids=email_chunk) + recipient_values = tig._notify_email_recipients(message, email_chunk) else: - recipient_values = self.env['mail.thread'].message_get_recipient_values(notif_message=None, recipient_ids=email_chunk) + recipient_values = self.env['mail.thread']._notify_email_recipients(message, email_chunk) create_values = { 'body_html': body, 'subject': subject, @@ -177,10 +177,10 @@ class Partner(models.Model): base_mail_values = self._notify_prepare_email_values(message) # classify recipients: actions / no action - if message.model and message.res_id and hasattr(self.env[message.model], '_message_notification_recipients'): - recipients = self.env[message.model].browse(message.res_id)._message_notification_recipients(message, self) + if message.model and message.res_id and hasattr(self.env[message.model], '_notify_classify_recipients'): + recipients = self.env[message.model].browse(message.res_id)._notify_classify_recipients(message, self) else: - recipients = self.env['mail.thread']._message_notification_recipients(message, self) + recipients = self.env['mail.thread']._notify_classify_recipients(message, self) emails = self.env['mail.mail'] recipients_nbr, recipients_max = 0, 50 diff --git a/addons/mail/wizard/mail_compose_message.py b/addons/mail/wizard/mail_compose_message.py index db46e76d5c8a..de83584f282a 100644 --- a/addons/mail/wizard/mail_compose_message.py +++ b/addons/mail/wizard/mail_compose_message.py @@ -277,8 +277,8 @@ class MailComposer(models.TransientModel): # compute alias-based reply-to in batch reply_to_value = dict.fromkeys(res_ids, None) if mass_mail_mode and not self.no_auto_thread: - # reply_to_value = self.env['mail.thread'].with_context(thread_model=self.model).browse(res_ids).message_get_reply_to(default=self.email_from) - reply_to_value = self.env['mail.thread'].with_context(thread_model=self.model).message_get_reply_to(res_ids, default=self.email_from) + # reply_to_value = self.env['mail.thread'].with_context(thread_model=self.model).browse(res_ids)._notify_get_reply_to(default=self.email_from) + reply_to_value = self.env['mail.thread'].with_context(thread_model=self.model)._notify_get_reply_to(res_ids, default=self.email_from) for res_id in res_ids: # static wizard (mail.message) values @@ -298,8 +298,8 @@ class MailComposer(models.TransientModel): # mass mailing: rendering override wizard static values if mass_mail_mode and self.model: - if self.model in self.env and hasattr(self.env[self.model], 'message_get_email_values'): - mail_values.update(self.env[self.model].browse(res_id).message_get_email_values()) + if self.model in self.env and hasattr(self.env[self.model], '_notify_specific_email_values'): + mail_values.update(self.env[self.model].browse(res_id)._notify_specific_email_values(False)) # keep a copy unless specifically requested, reset record name (avoid browsing records) mail_values.update(notification=not self.auto_delete_message, model=self.model, res_id=res_id, record_name=False) # auto deletion of mail_mail diff --git a/addons/project/models/project.py b/addons/project/models/project.py index 34a3e5b5fa52..90630570b58a 100644 --- a/addons/project/models/project.py +++ b/addons/project/models/project.py @@ -888,11 +888,11 @@ class Task(models.Model): return groups @api.model - def message_get_reply_to(self, res_ids, default=None): + def _notify_get_reply_to(self, res_ids, default=None): """ Override to get the reply_to of the parent project. """ tasks = self.sudo().browse(res_ids) project_ids = tasks.mapped('project_id').ids - aliases = self.env['project.project'].message_get_reply_to(project_ids, default=default) + aliases = self.env['project.project']._notify_get_reply_to(project_ids, default=default) return {task.id: aliases.get(task.project_id.id, False) for task in tasks} @api.multi @@ -968,14 +968,12 @@ class Task(models.Model): return recipients @api.multi - def message_get_email_values(self, notif_mail=None): - res = super(Task, self).message_get_email_values(notif_mail=notif_mail) - headers = {} - if res.get('headers'): - try: - headers.update(safe_eval(res['headers'])) - except Exception: - pass + def _notify_specific_email_values(self, message): + res = super(Task, self)._notify_specific_email_values(message) + try: + headers = safe_eval(res.get('headers', dict())) + except Exception: + headers = {} if self.project_id: current_objects = [h for h in headers.get('X-Odoo-Objects', '').split(',') if h] current_objects.insert(0, 'project.project-%s, ' % self.project_id.id) diff --git a/addons/website_mail_channel/models/mail_channel.py b/addons/website_mail_channel/models/mail_channel.py index f0b0f3665e25..49d9139ec694 100644 --- a/addons/website_mail_channel/models/mail_channel.py +++ b/addons/website_mail_channel/models/mail_channel.py @@ -14,21 +14,17 @@ class MailGroup(models.Model): _inherit = 'mail.channel' @api.multi - def message_get_email_values(self, notif_mail=None): + def _notify_specific_email_values(self, message): self.ensure_one() - res = super(MailGroup, self).message_get_email_values(notif_mail=notif_mail) + res = super(MailGroup, self)._notify_specific_email_values(message) + try: + headers = safe_eval(res.get('headers', dict())) + except Exception: + headers = {} base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url') - headers = {} - if res.get('headers'): - try: - headers = safe_eval(res['headers']) - except Exception: - pass - headers.update({ - 'List-Archive': '<%s/groups/%s>' % (base_url, slug(self)), - 'List-Subscribe': '<%s/groups>' % (base_url), - 'List-Unsubscribe': '<%s/groups?unsubscribe>' % (base_url,), - }) + headers['List-Archive'] = '<%s/groups/%s>' % (base_url, slug(self)), + headers['List-Subscribe'] = '<%s/groups>' % (base_url), + headers['List-Unsubscribe'] = '<%s/groups?unsubscribe>' % (base_url,), res['headers'] = repr(headers) return res -- GitLab