[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
This commit is contained in:
Thibault Delavallée
2018-01-29 16:33:07 +01:00
parent 304e6cf90b
commit cae1c3977f
9 changed files with 63 additions and 65 deletions
+2 -2
View File
@@ -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
@@ -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
+8 -10
View File
@@ -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)
+6 -6
View File
@@ -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):
+16 -10
View File
@@ -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]
}
+8 -8
View File
@@ -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
+4 -4
View File
@@ -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
+8 -10
View File
@@ -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)
@@ -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