diff options
| author | Thibault Delavallée <tde@odoo.com> | 2018-04-27 17:44:28 +0200 |
|---|---|---|
| committer | Thibault Delavallée <tde@odoo.com> | 2018-08-07 15:43:06 +0200 |
| commit | 689a496f1c60eb662dec7bbf7079f33b070a2cee (patch) | |
| tree | 06d7887d9ccd1bcaab673aa8cd296c39fd92c794 /addons/mail | |
| parent | 89051058f4ab7069024f7f6cf2caac05bd68b499 (diff) | |
[REF] mail: propagate recipient data through notification process
Purpose of this commit is to avoid browsing and prefetching data about
recipients when notifying a message to partners and channels. Mail message
_notify computes all necessary data in a single query. This commit allow
to re-use this data by propagating it through the call chain.
Addons inheriting from classification methods used when sending notification
emails are updated accordingly to the API and data update.
This commit is linked to task ID 47934 and PR #24033. No functional change
should occur with this commit.
Diffstat (limited to 'addons/mail')
| -rw-r--r-- | addons/mail/models/mail_message.py | 26 | ||||
| -rw-r--r-- | addons/mail/models/mail_thread.py | 22 | ||||
| -rw-r--r-- | addons/mail/models/res_partner.py | 45 | ||||
| -rw-r--r-- | addons/mail/wizard/invite.py | 7 | ||||
| -rw-r--r-- | addons/mail/wizard/mail_resend_message.py | 21 |
5 files changed, 77 insertions, 44 deletions
diff --git a/addons/mail/models/mail_message.py b/addons/mail/models/mail_message.py index 78487f35af7..07ec041050b 100644 --- a/addons/mail/models/mail_message.py +++ b/addons/mail/models/mail_message.py @@ -1054,7 +1054,6 @@ class Message(models.Model): self.ensure_one() email_cids = [r['id'] for r in rdata['channels'] if r['notif'] == 'email'] - email_pids = [r['id'] for r in rdata['partners'] if r['notif'] == 'email'] inbox_pids = [r['id'] for r in rdata['partners'] if r['notif'] == 'inbox'] message_values = {} @@ -1062,24 +1061,27 @@ class Message(models.Model): message_values['channel_ids'] = [(6, 0, [r['id'] for r in rdata['channels']])] if rdata['partners']: message_values['needaction_partner_ids'] = [(6, 0, [r['id'] for r in rdata['partners']])] - if message_values and record and hasattr(record, 'message_get_message_notify_values'): - message_values.update(record.message_get_message_notify_values(self, message_values)) + if message_values and record and hasattr(record, '_notify_customize_recipients'): + message_values.update(record._notify_customize_recipients(self, message_values, rdata)) if message_values: self.write(message_values) # notify partners and channels - # those methods are called as SUPERUSER because portal users posting messages - # have no access to partner model. Maybe propagating a real uid could be necessary. - if email_cids or email_pids: - self.env['res.partner'].sudo().search([ - '|', - ('id', 'in', email_pids), + if email_cids: + new_pids = self.env['res.partner'].sudo().search([ + ('id', 'not in', [r['id'] for r in rdata['partners']]), ('channel_ids', 'in', email_cids), - ('email', '!=', self.author_id.email or self.email_from), - ])._notify(self, record, force_send=force_send, send_after_commit=send_after_commit, model_description=model_description, mail_auto_delete=mail_auto_delete) + ('email', 'not in', [self.author_id.email, self.email_from]), + ]) + for partner in new_pids: + rdata['partners'].append({'id': partner.id, 'share': True, 'notif': 'email', 'type': 'customer', 'groups': []}) + + partner_email_rdata = [r for r in rdata['partners'] if r['notif'] == 'email'] + if partner_email_rdata: + self.env['res.partner']._notify(self, partner_email_rdata, record, force_send=force_send, send_after_commit=send_after_commit, model_description=model_description, mail_auto_delete=mail_auto_delete) if inbox_pids: - self.env['res.partner'].sudo().browse(inbox_pids)._notify_by_chat(self) + self.env['res.partner'].browse(inbox_pids)._notify_by_chat(self) if rdata['channels']: self.env['mail.channel'].sudo().browse([r['id'] for r in rdata['channels']])._notify(self) diff --git a/addons/mail/models/mail_thread.py b/addons/mail/models/mail_thread.py index 1fe466d164a..fbdfcbd42e9 100644 --- a/addons/mail/models/mail_thread.py +++ b/addons/mail/models/mail_thread.py @@ -707,7 +707,7 @@ class MailThread(models.AbstractModel): return groups @api.multi - def _notify_classify_recipients(self, message, recipients): + def _notify_classify_recipients(self, message, recipient_data): """ Classify recipients to be notified of a message in groups to have specific rendering depending on their group. For example users could have access to buttons customers should not have in their emails. @@ -716,7 +716,7 @@ class MailThread(models.AbstractModel): method defined here-under. :param message: mail.message record about to be notified - :param recipients: res.partner recordset to notify + :param recipients: res.partner recordset to notify UPDATE ME """ result = {} @@ -729,11 +729,11 @@ class MailThread(models.AbstractModel): view_title = _('View') default_groups = [ - ('user', lambda partner: bool(partner.user_ids) and not any(user.share for user in partner.user_ids), {}), - ('portal', lambda partner: bool(partner.user_ids) and all(user.share for user in partner.user_ids), { + ('user', lambda pdata: pdata['type'] == 'user', {}), + ('portal', lambda pdata: pdata['type'] == 'portal', { 'has_button_access': False, }), - ('customer', lambda partner: True, { + ('customer', lambda pdata: True, { 'has_button_access': False, }) ] @@ -746,12 +746,12 @@ class MailThread(models.AbstractModel): 'url': access_link, 'title': view_title}) group_data.setdefault('actions', list()) - group_data.setdefault('recipients', self.env['res.partner']) + group_data.setdefault('recipients', list()) - for recipient in recipients: + for recipient in recipient_data: for group_name, group_func, group_data in groups: if group_func(recipient): - group_data['recipients'] |= recipient + group_data['recipients'].append(recipient['id']) break for group_name, group_method, group_data in groups: @@ -759,13 +759,13 @@ class MailThread(models.AbstractModel): return result - def _notify_classify_recipients_on_records(self, message, recipients, records=None): + def _notify_classify_recipients_on_records(self, message, recipient_data, records=None): """ Generic wrapper on ``_notify_classify_recipients`` checking mail.thread inheritance and allowing to call model-specific implementation in a one liner. This method should not be overridden. """ if records and hasattr(records, '_notify_classify_recipients'): - return records._notify_classify_recipients(message, recipients) - return self._notify_classify_recipients(message, recipients) + return records._notify_classify_recipients(message, recipient_data) + return self._notify_classify_recipients(message, recipient_data) @api.multi def _notify_get_reply_to(self, default=None, records=None, company=None, doc_names=None): diff --git a/addons/mail/models/res_partner.py b/addons/mail/models/res_partner.py index aa52ca8d339..b0bdf1e09b2 100644 --- a/addons/mail/models/res_partner.py +++ b/addons/mail/models/res_partner.py @@ -79,12 +79,13 @@ class Partner(models.Model): 'subtype': message.subtype_id, } - @api.multi - def _notify(self, message, record, force_send=False, send_after_commit=True, model_description=False, mail_auto_delete=True): + @api.model + def _notify(self, message, rdata, record, force_send=False, send_after_commit=True, model_description=False, mail_auto_delete=True): """ Method to send email linked to notified messages. The recipients are the recordset on which this method is called. :param message: mail.message record to notify; + :param rdata: recipient data (see mail.message _notify); :param record: optional record on which the message was posted; :param force_send: tells whether to send notification emails within the current transaction or to use the email queue; @@ -94,7 +95,7 @@ class Partner(models.Model): notification templates); :param mail_auto_delete: delete notification emails once sent; """ - if not self.ids: + if not rdata: return True template_xmlid = message.layout if message.layout else 'mail.message_notification_email' @@ -117,9 +118,11 @@ class Partner(models.Model): base_mail_values.update(self.env['mail.thread']._notify_specific_email_values_on_records(message, records=record)) # classify recipients: actions / no action - recipients = self.env['mail.thread']._notify_classify_recipients_on_records(message, self, records=record) + recipients = self.env['mail.thread']._notify_classify_recipients_on_records(message, rdata, records=record) - emails = self.env['mail.mail'] + Mail = self.env['mail.mail'].sudo() + emails = self.env['mail.mail'].sudo() + email_pids = set() recipients_nbr, recipients_max = 0, 50 for group_tpl_values in [group for group in recipients.values() if group['recipients']]: # generate notification email content @@ -129,7 +132,7 @@ class Partner(models.Model): mail_subject = message.subject or (message.record_name and 'Re: %s' % message.record_name) # send email - for email_chunk in split_every(50, group_tpl_values['recipients'].ids): + for email_chunk in split_every(50, group_tpl_values['recipients']): recipient_values = self.env['mail.thread']._notify_email_recipients_on_records(message, email_chunk, records=record) create_values = { 'body_html': mail_body, @@ -137,19 +140,23 @@ class Partner(models.Model): } create_values.update(base_mail_values) create_values.update(recipient_values) - emails |= self.env['mail.mail'].create(create_values) - - # update notifications - for email in emails: - notifications = self.env['mail.notification'].sudo().search([ - ('mail_message_id', '=', email.mail_message_id.id), - ('res_partner_id', 'in', email.recipient_ids.ids)]) - notifications.write({ - 'mail_id': email.id, - 'is_email': True, - 'is_read': True, # handle by email discards Inbox notification - 'email_status': 'ready', - }) + recipient_ids = [r[1] for r in create_values.get('recipient_ids', [])] + email = Mail.create(create_values) + + if email and recipient_ids: + notifications = self.env['mail.notification'].sudo().search([ + ('mail_message_id', '=', email.mail_message_id.id), + ('res_partner_id', 'in', list(recipient_ids)) + ]) + notifications.write({ + 'is_email': True, + 'mail_id': email.id, + 'is_read': True, # handle by email discards Inbox notification + 'email_status': 'ready', + }) + + emails |= email + email_pids.update(recipient_ids) # NOTE: # 1. for more than 50 followers, use the queue system diff --git a/addons/mail/wizard/invite.py b/addons/mail/wizard/invite.py index 541d3c1ea4f..39d6c0bdeeb 100644 --- a/addons/mail/wizard/invite.py +++ b/addons/mail/wizard/invite.py @@ -60,6 +60,11 @@ class Invite(models.TransientModel): 'no_auto_thread': True, 'add_sign': True, }) - new_partners.with_context(auto_delete=True)._notify(message, document, force_send=True, send_after_commit=False) + self.env['res.partner'].with_context(auto_delete=True)._notify( + message, + [{'id': pid, 'share': True, 'notif': 'email', 'type': 'customer', 'groups': []} for pid in new_partners.ids], + document, + force_send=True, + send_after_commit=False) message.unlink() return {'type': 'ir.actions.act_window_close'} diff --git a/addons/mail/wizard/mail_resend_message.py b/addons/mail/wizard/mail_resend_message.py index bf56128ad61..77b1a2a102d 100644 --- a/addons/mail/wizard/mail_resend_message.py +++ b/addons/mail/wizard/mail_resend_message.py @@ -60,7 +60,26 @@ class MailResendMessage(models.TransientModel): if to_send: message = wizard.mail_message_id record = self.env[message.model].browse(message.res_id) if message.model and message.res_id else None - to_send.sudo()._notify(self.mail_message_id, record, force_send=True, send_after_commit=False) + + rdata = [] + for pid, cid, active, pshare, ctype, notif, groups in self.env['mail.followers']._get_recipient_data(None, False, pids=to_send.ids): + if pid and notif == 'email' or not notif: + pdata = {'id': pid, 'share': pshare, 'active': active, 'notif': 'email', 'groups': groups} + if not pshare and notif: # has an user and is not shared, is therefore user + rdata.append(dict(pdata, type='user')) + elif pshare and notif: # has an user and is shared, is therefore portal + rdata.append(dict(pdata, type='portal')) + else: # has no user, is therefore customer + rdata.append(dict(pdata, type='customer')) + + self.env['res.partner']._notify( + message, + rdata, + record, + force_send=True, + send_after_commit=False + ) + self.mail_message_id._notify_failure_update() return {'type': 'ir.actions.act_window_close'} |
