[IMP] mail: return generated records in mail composer

We now return generated records when calling ``_action_send_mail`` on mail
composer. This allows to have access to generated emails (in mass mode) or
posted messages (in comment mode). Otherwise we have to access sub-fields
like activity_ids / message_ids, which may be inefficient or generate extra
ACLs checks.

Also replace some |= with += when usage of OrderedSet is not necessary. To
keep ordering, ordered sets are used when doing an or between record sets.
However in some cases we know we are simply adding records not already in
a previous record set, meaning we can just use +.

Task-2883589 (Activity performance and cleaning)

Part-of: odoo/odoo#93682
This commit is contained in:
Thibault Delavallée
2022-08-26 03:42:58 +02:00
parent 3cc931ef58
commit 6bea3c7605
5 changed files with 18 additions and 8 deletions
+1 -1
View File
@@ -557,7 +557,7 @@ class MailActivity(models.Model):
'res_model': activity_message._name,
})
activity_message.attachment_ids = message_attachments
messages |= activity_message
messages += activity_message
next_activities = self.env['mail.activity'].create(next_activities_values)
self.unlink() # will unlink activity, dont access `self` after that
+1 -1
View File
@@ -443,7 +443,7 @@ class MailActivityMixin(models.AbstractModel):
for record in self:
render_context['object'] = record
note = self.env['ir.qweb']._render(view_ref, render_context, minimal_qcontext=True, raise_if_not_found=False)
activities |= record.activity_schedule(act_type_xmlid=act_type_xmlid, date_deadline=date_deadline, summary=summary, note=note, **act_values)
activities += record.activity_schedule(act_type_xmlid=act_type_xmlid, date_deadline=date_deadline, summary=summary, note=note, **act_values)
return activities
def activity_reschedule(self, act_type_xmlids, user_id=None, date_deadline=None, new_user_id=None):
+1 -1
View File
@@ -997,7 +997,7 @@ class Message(models.Model):
except AccessError:
continue
else:
messages |= message
messages += message
messages_per_partner = defaultdict(lambda: self.env['mail.message'])
for message in messages:
if not self.env.user._is_public():
+2 -2
View File
@@ -2045,7 +2045,7 @@ class MailThread(models.AbstractModel):
def message_post_with_view(self, views_or_xmlid, **kwargs):
""" Helper method to send a mail / post a message using a view_id """
self._message_compose_with_view(views_or_xmlid, **kwargs)
return self._message_compose_with_view(views_or_xmlid, **kwargs)
def message_post_with_template(self, template_id, email_layout_xmlid=None, auto_commit=False, **kwargs):
""" Helper method to send a mail with a template
@@ -2518,7 +2518,7 @@ class MailThread(models.AbstractModel):
'is_read': True, # discard Inbox notification
'notification_status': 'ready',
} for recipient_id in tocreate_recipient_ids]
emails |= new_email
emails += new_email
if notif_create_values:
SafeNotification.create(notif_create_values)
+13 -3
View File
@@ -240,7 +240,13 @@ class MailComposer(models.TransientModel):
def _action_send_mail(self, auto_commit=False):
""" Process the wizard content and proceed with sending the related
email(s), rendering any template patterns on the fly if needed. """
email(s), rendering any template patterns on the fly if needed.
:return tuple: (
result_mails_su: in mass mode, sent emails (as sudo),
result_messages: in comment mode, posted messages
)
"""
# Several custom layouts make use of the model description at rendering, e.g. in the
# 'View <document>' button. Some models are used for different business concepts, such as
# 'purchase.order' which is used for a RFQ and and PO. To avoid confusion, we must use a
@@ -248,6 +254,7 @@ class MailComposer(models.TransientModel):
# Therefore, we can set the description in the context from the beginning to avoid falling
# back on the regular display_name retrieved in ``_notify_by_email_prepare_rendering_context()``.
model_description = self._context.get('model_description')
result_mails_su, result_messages = self.env['mail.mail'].sudo(), self.env['mail.message']
for wizard in self:
# Duplicate attachments linked to the email.template.
@@ -299,7 +306,7 @@ class MailComposer(models.TransientModel):
all_mail_values = wizard.get_mail_values(res_ids)
for res_id, mail_values in all_mail_values.items():
if wizard.composition_mode == 'mass_mail':
batch_mails_sudo |= self.env['mail.mail'].sudo().create(mail_values)
batch_mails_sudo += self.env['mail.mail'].sudo().create(mail_values)
else:
post_params = dict(
message_type=wizard.message_type,
@@ -317,11 +324,14 @@ class MailComposer(models.TransientModel):
# if message_notify returns an empty record set, no recipients where found.
raise UserError(_("No recipient found."))
else:
ActiveModel.browse(res_id).message_post(**post_params)
result_messages += ActiveModel.browse(res_id).message_post(**post_params)
result_mails_su += batch_mails_sudo
if wizard.composition_mode == 'mass_mail':
batch_mails_sudo.send(auto_commit=auto_commit)
return result_mails_su, result_messages
def action_save_as_template(self):
""" hit save as template button: current form value will be a new
template attached to the current document. """