[IMP] mail_template_substitute: black, isort, prettier

pull/1314/head
Karthik, Sodexis 2023-03-31 12:43:41 +05:30 committed by Sakthivel
parent b6a8261389
commit 48053a686e
6 changed files with 64 additions and 64 deletions

View File

@ -8,8 +8,7 @@
""", """,
"version": "12.0.1.0.1", "version": "12.0.1.0.1",
"license": "AGPL-3", "license": "AGPL-3",
"author": "ACSONE SA/NV," "author": "ACSONE SA/NV," "Odoo Community Association (OCA)",
"Odoo Community Association (OCA)",
"website": "https://github.com/OCA/social", "website": "https://github.com/OCA/social",
"depends": ["base", "mail", "report_substitute"], "depends": ["base", "mail", "report_substitute"],
"data": [ "data": [

View File

@ -22,9 +22,7 @@ class MailTemplate(models.Model):
if isinstance(active_ids, pycompat.integer_types): if isinstance(active_ids, pycompat.integer_types):
active_ids = [active_ids] active_ids = [active_ids]
model = self.env[model_id.model] model = self.env[model_id.model]
for ( for substitution_template_rule in self.mail_template_substitution_rule_ids:
substitution_template_rule
) in self.mail_template_substitution_rule_ids:
domain = safe_eval(substitution_template_rule.domain) domain = safe_eval(substitution_template_rule.domain)
domain.append(("id", "in", active_ids)) domain.append(("id", "in", active_ids))
if set(model.search(domain).ids) == set(active_ids): if set(model.search(domain).ids) == set(active_ids):
@ -33,9 +31,7 @@ class MailTemplate(models.Model):
@api.multi @api.multi
def get_email_template(self, res_ids): def get_email_template(self, res_ids):
substitution_template = self._get_substitution_template( substitution_template = self._get_substitution_template(self.model_id, res_ids)
self.model_id, res_ids
)
if substitution_template: if substitution_template:
return substitution_template.get_email_template(res_ids) return substitution_template.get_email_template(res_ids)
return super().get_email_template(res_ids) return super().get_email_template(res_ids)

View File

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?> <?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2019 ACSONE SA/NV <!-- Copyright 2019 ACSONE SA/NV
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). --> License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo> <odoo>
<record model="ir.model.access" id="mail_template_substitution_rule_access"> <record model="ir.model.access" id="mail_template_substitution_rule_access">

View File

@ -7,53 +7,51 @@ from odoo.tests.common import TransactionCase
class TestMailTemplateSubstitute(TransactionCase): class TestMailTemplateSubstitute(TransactionCase):
def setUp(self): def setUp(self):
super(TestMailTemplateSubstitute, self).setUp() super(TestMailTemplateSubstitute, self).setUp()
self.smt2 = self.env['mail.template'].create( self.smt2 = self.env["mail.template"].create(
{ {
'name': 'substitute_template_2', "name": "substitute_template_2",
'model_id': self.env.ref('base.model_res_partner').id, "model_id": self.env.ref("base.model_res_partner").id,
} }
) )
self.smt1 = self.env['mail.template'].create( self.smt1 = self.env["mail.template"].create(
{ {
'name': 'substitute_template_1', "name": "substitute_template_1",
'model_id': self.env.ref('base.model_res_partner').id, "model_id": self.env.ref("base.model_res_partner").id,
'mail_template_substitution_rule_ids': [ "mail_template_substitution_rule_ids": [
( (
0, 0,
0, 0,
{ {
'substitution_mail_template_id': self.smt2.id, "substitution_mail_template_id": self.smt2.id,
'domain': "[('id', '=', False)]", "domain": "[('id', '=', False)]",
}, },
) )
], ],
} }
) )
self.mt = self.env['mail.template'].create( self.mt = self.env["mail.template"].create(
{ {
'name': 'base_template', "name": "base_template",
'model_id': self.env.ref('base.model_res_partner').id, "model_id": self.env.ref("base.model_res_partner").id,
'mail_template_substitution_rule_ids': [ "mail_template_substitution_rule_ids": [
(0, 0, {'substitution_mail_template_id': self.smt1.id}) (0, 0, {"substitution_mail_template_id": self.smt1.id})
], ],
} }
) )
self.mail_compose = self.env['mail.compose.message'].create( self.mail_compose = self.env["mail.compose.message"].create(
{'template_id': self.mt.id, 'composition_mode': 'mass_mail'} {"template_id": self.mt.id, "composition_mode": "mass_mail"}
) )
self.partners = self.env['res.partner'].search([]) self.partners = self.env["res.partner"].search([])
def test_get_email_template(self): def test_get_email_template(self):
self.assertEqual( self.assertEqual(
self.mt._get_substitution_template( self.mt._get_substitution_template(
self.env.ref('base.model_res_partner'), self.partners.ids self.env.ref("base.model_res_partner"), self.partners.ids
), ),
self.smt1, self.smt1,
) )
self.assertEqual( self.assertEqual(
self.mt.get_email_template(self.partners.ids).get( self.mt.get_email_template(self.partners.ids).get(self.partners.ids[0]),
self.partners.ids[0]
),
self.smt1, self.smt1,
) )
@ -61,13 +59,13 @@ class TestMailTemplateSubstitute(TransactionCase):
self.assertEqual( self.assertEqual(
self.mail_compose.with_context( self.mail_compose.with_context(
active_ids=self.partners.ids active_ids=self.partners.ids
)._get_substitution_template('mass_mail', self.mt, None), )._get_substitution_template("mass_mail", self.mt, None),
self.smt1, self.smt1,
) )
def test_onchange_template_id_wrapper(self): def test_onchange_template_id_wrapper(self):
self.assertEqual(self.mail_compose.template_id, self.mt) self.assertEqual(self.mail_compose.template_id, self.mt)
self.smt1.mail_template_substitution_rule_ids.domain = '[]' self.smt1.mail_template_substitution_rule_ids.domain = "[]"
self.mail_compose.with_context( self.mail_compose.with_context(
active_ids=self.partners.ids active_ids=self.partners.ids
).onchange_template_id_wrapper() ).onchange_template_id_wrapper()

View File

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?> <?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2019 ACSONE SA/NV <!-- Copyright 2019 ACSONE SA/NV
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). --> License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo> <odoo>
<record model="ir.ui.view" id="email_template_form"> <record model="ir.ui.view" id="email_template_form">
@ -11,7 +10,10 @@
<field name="inherit_id" ref="mail.email_template_form" /> <field name="inherit_id" ref="mail.email_template_form" />
<field name="arch" type="xml"> <field name="arch" type="xml">
<xpath expr="//notebook" position="inside"> <xpath expr="//notebook" position="inside">
<page name="mail_template_substitution_rule" string="Substitution Rules"> <page
name="mail_template_substitution_rule"
string="Substitution Rules"
>
<field name="mail_template_substitution_rule_ids"> <field name="mail_template_substitution_rule_ids">
<tree> <tree>
<field name="sequence" widget="handle" /> <field name="sequence" widget="handle" />
@ -21,16 +23,24 @@
<form> <form>
<sheet> <sheet>
<group> <group>
<field name="mail_template_id" <field
invisible="1" readonly="1" name="mail_template_id"
required="0"/> invisible="1"
readonly="1"
required="0"
/>
<field name="model" invisible="1" /> <field name="model" invisible="1" />
<field name="substitution_mail_template_id" <field
domain="[('model', '=', model), ('id', '!=', parent.id)]"/> name="substitution_mail_template_id"
domain="[('model', '=', model), ('id', '!=', parent.id)]"
/>
</group> </group>
<group> <group>
<field name="domain" widget="domain" <field
options="{'model': 'model'}"/> name="domain"
widget="domain"
options="{'model': 'model'}"
/>
</group> </group>
</sheet> </sheet>
</form> </form>

View File

@ -6,15 +6,13 @@ from odoo import api, models
class MailComposeMessage(models.TransientModel): class MailComposeMessage(models.TransientModel):
_inherit = 'mail.compose.message' _inherit = "mail.compose.message"
@api.model @api.model
def _get_substitution_template(self, composition_mode, template, res_ids): def _get_substitution_template(self, composition_mode, template, res_ids):
if template: if template:
if composition_mode == 'mass_mail' and self.env.context.get( if composition_mode == "mass_mail" and self.env.context.get("active_ids"):
'active_ids' res_ids = self.env.context.get("active_ids")
):
res_ids = self.env.context.get('active_ids')
res_ids_to_templates = template.get_email_template(res_ids) res_ids_to_templates = template.get_email_template(res_ids)
if res_ids_to_templates: if res_ids_to_templates:
return list(res_ids_to_templates.values())[0] return list(res_ids_to_templates.values())[0]
@ -24,16 +22,16 @@ class MailComposeMessage(models.TransientModel):
def default_get(self, fields): def default_get(self, fields):
result = super(MailComposeMessage, self).default_get(fields) result = super(MailComposeMessage, self).default_get(fields)
substitution_template = self._get_substitution_template( substitution_template = self._get_substitution_template(
result.get('composition_mode'), result.get("composition_mode"),
self.env['mail.template'].browse(result.get('template_id')), self.env["mail.template"].browse(result.get("template_id")),
[result.get('res_id')], [result.get("res_id")],
) )
if substitution_template: if substitution_template:
result['template_id'] = substitution_template.id result["template_id"] = substitution_template.id
return result return result
@api.multi @api.multi
@api.onchange('template_id') @api.onchange("template_id")
def onchange_template_id_wrapper(self): def onchange_template_id_wrapper(self):
substitution_template = self._get_substitution_template( substitution_template = self._get_substitution_template(
self.composition_mode, self.template_id, [self.res_id] self.composition_mode, self.template_id, [self.res_id]