[REF] mail: cleanup context usage in composer

Purpose of this commit is to cleanup context usage in composer

  * remove now dead 'custom_layout' context keys that was present mainly for
    compatibility until Odoo v15. You should now correctly use default value
    for 'email_layout_xmlid' field like every field;
  * remove 'mail_auto_delete' context key, not used anywhere in codebase.
    Auto-delete field of composer will be improved soon so that setting it
    as every field will replace the context usage;
  * remove usage of self._context, to use self.env.context;

Task-2710804 (Mail: Clean MailThread Posting API)

Part-of: odoo/odoo#99482
This commit is contained in:
Thibault Delavallée
2023-01-17 20:58:38 +01:00
parent df6171ac38
commit 5b332e2d28
2 changed files with 8 additions and 32 deletions
+8 -12
View File
@@ -50,10 +50,6 @@ class MailComposer(models.TransientModel):
- active_ids: record IDs
- default_model or active_model
"""
# backward compatibility of context before addition of
# email_layout_xmlid field: to remove in 15.1+
if self._context.get('custom_layout') and 'default_email_layout_xmlid' not in self._context:
self = self.with_context(default_email_layout_xmlid=self._context['custom_layout'])
# support subtype xmlid, like ``message_post``, when easier than using ``ref``
if self.env.context.get('default_subtype_xmlid'):
self = self.with_context(
@@ -75,16 +71,16 @@ class MailComposer(models.TransientModel):
result['author_id'] = author_id
if 'model' in fields and 'model' not in result:
result['model'] = self._context.get('active_model')
result['model'] = self.env.context.get('active_model')
if 'res_id' in fields and 'res_id' not in result:
result['res_id'] = self._context.get('active_id')
result['res_id'] = self.env.context.get('active_id')
if 'reply_to_mode' in fields and 'reply_to_mode' not in result and result.get('model'):
# doesn't support threading
if result['model'] not in self.env or not hasattr(self.env[result['model']], 'message_post'):
result['reply_to_mode'] = 'new'
if 'active_domain' in self._context: # not context.get() because we want to keep global [] domains
result['active_domain'] = '%s' % self._context.get('active_domain')
if 'active_domain' in self.env.context: # not context.get() because we want to keep global [] domains
result['active_domain'] = '%r' % self.env.context.get('active_domain')
if result.get('composition_mode') == 'comment' and (set(fields) & set(['model', 'res_id', 'partner_ids', 'record_name', 'subject'])):
result.update(self.get_record_data(result))
@@ -367,8 +363,8 @@ class MailComposer(models.TransientModel):
# wizard works in batch mode: [res_id] or active_ids or active_domain
if mass_mode and wizard.use_active_domain and wizard.model:
res_ids = self.env[wizard.model].search(ast.literal_eval(wizard.active_domain)).ids
elif mass_mode and wizard.model and self._context.get('active_ids'):
res_ids = self._context['active_ids']
elif mass_mode and wizard.model and self.env.context.get('active_ids'):
res_ids = self.env.context['active_ids']
else:
res_ids = [wizard.res_id]
@@ -460,7 +456,7 @@ class MailComposer(models.TransientModel):
# generate the saved template
record.write({'template_id': template.id})
record._onchange_template_id_wrapper()
return _reopen(self, record.id, record.model, context=self._context)
return _reopen(self, record.id, record.model, context=self.env.context)
# ------------------------------------------------------------
# RENDERING / VALUES GENERATION
@@ -580,7 +576,7 @@ class MailComposer(models.TransientModel):
values.update(
email_add_signature=not bool(self.template_id) and self.email_add_signature,
email_layout_xmlid=self.email_layout_xmlid,
mail_auto_delete=self.template_id.auto_delete if self.template_id else self.env.context.get('mail_auto_delete', True),
mail_auto_delete=self.template_id.auto_delete if self.template_id else True,
model_description=model_description,
)
return values
@@ -1056,26 +1056,6 @@ class TestComposerResultsComment(TestMailComposer):
self.assertEqual(len(self._new_mails), 2, 'Should have created 2 mail.mail (1 for users, 1 for customers)')
self.assertEqual(len(self._new_mails.exists()), 0, 'To fix: does not respect auto_delete')
# ensure ``mail_auto_delete`` context key allow to override this behavior
composer = self.env['mail.compose.message'].with_context(
self._get_web_context(self.test_record),
mail_auto_delete=False,
).create({
'body': '<p>Test Body</p>',
'partner_ids': [(4, self.partner_1.id), (4, self.partner_2.id)]
})
with self.mock_mail_gateway(mail_unlink_sent=True):
composer._action_send_mail()
# notifications
message = self.test_record.message_ids[0]
self.assertEqual(message.notified_partner_ids, self.partner_employee_2 + self.partner_1 + self.partner_2)
# global outgoing
self.assertEqual(len(self._mails), 3, 'Should have sent an email each recipient')
self.assertEqual(len(self._new_mails), 2, 'Should have created 2 mail.mail (1 for users, 1 for customers)')
self.assertEqual(len(self._new_mails.exists()), 2, 'Should not have deleted mail.mail records')
@users('employee')
@mute_logger('odoo.tests', 'odoo.addons.mail.models.mail_mail', 'odoo.models.unlink')
def test_mail_composer_post_parameters(self):