[IMP] mass_mailing: improve composer invoke when sending

RATIONALE

Improve usage of composer in comment or email mode: support batch-posting in
comment, support more configuration from templates, improve global model.

SPECIFICATIONS

Use the new 'res_domain_user_id' field to use in combination to 'res_domain'
on composer to delegate res_ids computation to the composer instead of relying
on res_ids or active_ids. Records to mail are not stored inside a domain.

Set auto_delete values directly when invoking the composer instead of hacking
the generate mail values. That way we delegate more to the composer itself.

Task-3035101 (Mail: Support batch-posting from composer)

Part-of: odoo/odoo#99482
This commit is contained in:
Thibault Delavallée
2023-01-17 20:58:40 +01:00
parent 1a1acabd7b
commit f652f6a75c
+14 -9
View File
@@ -1046,16 +1046,19 @@ class MassMailing(models.Model):
def action_send_mail(self, res_ids=None):
author_id = self.env.user.partner_id.id
# If no recipient is passed, we don't want to use the recipients of the first
# mailing for all the others
initial_res_ids = res_ids
for mailing in self:
if not initial_res_ids:
res_ids = mailing._get_remaining_recipients()
if not res_ids:
context_user = mailing.user_id or mailing.write_uid or self.env.user
mailing = mailing.with_context(
**self.env['res.users'].with_user(context_user).context_get()
)
mailing_res_ids = res_ids or mailing._get_remaining_recipients()
if not mailing_res_ids:
raise UserError(_('There are no recipients selected.'))
composer_values = {
'auto_delete': not mailing.keep_archives,
# email-mode: keep original message for routing
'auto_delete_message': not mailing.reply_to_mode == 'update',
'author_id': author_id,
'attachment_ids': [(4, attachment.id) for attachment in mailing.attachment_ids],
'body': mailing._prepend_preview(mailing.body_html, mailing.preview),
@@ -1074,7 +1077,7 @@ class MassMailing(models.Model):
composer_values['reply_to'] = mailing.reply_to
composer = self.env['mail.compose.message'].with_context(
active_ids=res_ids,
active_ids=mailing_res_ids,
default_composition_mode='mass_mail',
**mailing._get_mass_mailing_context()
).create(composer_values)
@@ -1113,8 +1116,10 @@ class MassMailing(models.Model):
def _process_mass_mailing_queue(self):
mass_mailings = self.search([('state', 'in', ('in_queue', 'sending')), '|', ('schedule_date', '<', fields.Datetime.now()), ('schedule_date', '=', False)])
for mass_mailing in mass_mailings:
user = mass_mailing.write_uid or self.env.user
mass_mailing = mass_mailing.with_context(**user.with_user(user).context_get())
context_user = mass_mailing.user_id or mass_mailing.write_uid or self.env.user
mass_mailing = mass_mailing.with_context(
**self.env['res.users'].with_user(context_user).context_get()
)
if len(mass_mailing._get_remaining_recipients()) > 0:
mass_mailing.state = 'sending'
mass_mailing.action_send_mail()