[IMP] mail_notification_custom_subject: black, isort, prettier

This commit is contained in:
Carlos Roca 2021-02-15 14:28:18 +01:00 committed by Víctor Martínez
parent 586be98e65
commit 1bb5aee507
5 changed files with 46 additions and 114 deletions

View File

@ -7,13 +7,11 @@
"summary": "Apply a custom subject to mail notifications",
"version": "12.0.1.0.0",
"category": "Social Network",
"website": "https://github.com/OCA/social/",
"website": "https://github.com/OCA/social",
"author": "Tecnativa, Odoo Community Association (OCA)",
"license": "AGPL-3",
"installable": True,
"depends": [
"mail",
],
"depends": ["mail"],
"data": [
"security/ir.model.access.csv",
"views/mail_notification_custom_subject_views.xml",

View File

@ -16,9 +16,7 @@ class MailMessageCustomSubject(models.Model):
help="Model where this template applies",
)
subtype_ids = fields.Many2many(
comodel_name="mail.message.subtype",
string="Applied Subtypes",
required=True,
comodel_name="mail.message.subtype", string="Applied Subtypes", required=True,
)
subject_template = fields.Char(
string="Subject Template",

View File

@ -23,25 +23,21 @@ class MailThread(models.AbstractModel):
mail_auto_delete=True,
**kwargs
):
subtype_id = kwargs.get('subtype_id', False)
subtype_id = kwargs.get("subtype_id", False)
if not subtype_id:
subtype = subtype or 'mt_note'
if '.' not in subtype:
subtype = 'mail.%s' % subtype
subtype_id = self.env['ir.model.data'].xmlid_to_res_id(
subtype = subtype or "mt_note"
if "." not in subtype:
subtype = "mail.%s" % subtype
subtype_id = self.env["ir.model.data"].xmlid_to_res_id(
subtype, raise_if_not_found=False,
)
if subtype_id:
custom_subjects = self.env["mail.message.custom.subject"].search(
[
("model_id.model", "=", self._name),
("subtype_ids", "=", subtype_id),
]
[("model_id.model", "=", self._name), ("subtype_ids", "=", subtype_id)]
)
if not subject:
subject = 'Re: %s' % self.env["mail.message"].with_context(
default_model=self._name,
default_res_id=self.id,
subject = "Re: %s" % self.env["mail.message"].with_context(
default_model=self._name, default_res_id=self.id,
)._get_record_name({})
for template in custom_subjects:
try:

View File

@ -1,6 +1,6 @@
# Copyright 2020 Tecnativa - João Marques
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
# pylint: disable=C8107
from odoo.tests import common
@ -27,15 +27,7 @@ class TestMailNotificationCustomSubject(common.TransactionCase):
{
"name": "Test template 1",
"model_id": self.env.ref("base.model_res_partner").id,
"subtype_ids": [
(
6,
0,
[
self.env.ref("mail.mt_comment").id,
],
)
],
"subtype_ids": [(6, 0, [self.env.ref("mail.mt_comment").id])],
"subject_template": "${object.name or 'n/a'} and something more",
}
)
@ -65,15 +57,7 @@ class TestMailNotificationCustomSubject(common.TransactionCase):
{
"name": "Test template 1",
"model_id": self.env.ref("base.model_res_partner").id,
"subtype_ids": [
(
6,
0,
[
self.env.ref("mail.mt_comment").id,
],
)
],
"subtype_ids": [(6, 0, [self.env.ref("mail.mt_comment").id])],
"subject_template": "${object.name or 'n/a'} and something more",
}
)
@ -89,15 +73,7 @@ class TestMailNotificationCustomSubject(common.TransactionCase):
{
"name": "Test template 1",
"model_id": self.env.ref("base.model_res_partner").id,
"subtype_ids": [
(
6,
0,
[
self.env.ref("mail.mt_comment").id,
],
)
],
"subtype_ids": [(6, 0, [self.env.ref("mail.mt_comment").id])],
"subject_template": "${object.name or 'n/a'} and something more",
}
)
@ -105,15 +81,7 @@ class TestMailNotificationCustomSubject(common.TransactionCase):
{
"name": "Test template 2",
"model_id": self.env.ref("base.model_res_partner").id,
"subtype_ids": [
(
6,
0,
[
self.env.ref("mail.mt_comment").id,
],
)
],
"subtype_ids": [(6, 0, [self.env.ref("mail.mt_comment").id])],
"subject_template": "${object.name or 'n/a'} and something different",
}
)
@ -129,15 +97,7 @@ class TestMailNotificationCustomSubject(common.TransactionCase):
{
"name": "Test template 3",
"model_id": self.env.ref("base.model_res_partner").id,
"subtype_ids": [
(
6,
0,
[
self.env.ref("mail.mt_comment").id,
],
)
],
"subtype_ids": [(6, 0, [self.env.ref("mail.mt_comment").id])],
"subject_template": "${' and yet something else'}",
"position": "append_after",
}
@ -155,15 +115,7 @@ class TestMailNotificationCustomSubject(common.TransactionCase):
{
"name": "Test template 4",
"model_id": self.env.ref("base.model_res_partner").id,
"subtype_ids": [
(
6,
0,
[
self.env.ref("mail.mt_comment").id,
],
)
],
"subtype_ids": [(6, 0, [self.env.ref("mail.mt_comment").id])],
"subject_template": "${'Re: '}",
"position": "append_before",
}
@ -183,15 +135,7 @@ class TestMailNotificationCustomSubject(common.TransactionCase):
{
"name": "Test template 1",
"model_id": self.env.ref("base.model_res_partner").id,
"subtype_ids": [
(
6,
0,
[
self.env.ref("mail.mt_comment").id,
],
)
],
"subtype_ids": [(6, 0, [self.env.ref("mail.mt_comment").id])],
"subject_template": "${' and something more'}",
"position": "append_after",
}
@ -201,24 +145,14 @@ class TestMailNotificationCustomSubject(common.TransactionCase):
body="Test", subtype="mail.mt_comment", subject="Test",
)
# Get message and check subject
self.assertEquals(
mail_message_1.subject, "Test and something more"
)
self.assertEquals(mail_message_1.subject, "Test and something more")
def test_bad_template_does_not_break(self):
self.env["mail.message.custom.subject"].create(
{
"name": "Test bad template 1",
"model_id": self.env.ref("base.model_res_partner").id,
"subtype_ids": [
(
6,
0,
[
self.env.ref("mail.mt_comment").id,
],
)
],
"subtype_ids": [(6, 0, [self.env.ref("mail.mt_comment").id])],
"subject_template": "${obaject.number_a} and something",
"position": "append_after",
}
@ -229,9 +163,7 @@ class TestMailNotificationCustomSubject(common.TransactionCase):
)
# Get message and check subject
# No exception should be raised but subject should remain as original.
self.assertEquals(
mail_message_1.subject, "Test"
)
self.assertEquals(mail_message_1.subject, "Test")
def test_no_template_default_result(self):
# Send message in partner
@ -240,6 +172,4 @@ class TestMailNotificationCustomSubject(common.TransactionCase):
)
# Get message and check subject
# No exception should be raised but subject should remain as original.
self.assertEquals(
mail_message_1.subject, "Test partner 1"
)
self.assertEquals(mail_message_1.subject, "Test partner 1")

View File

@ -12,7 +12,10 @@
<h1 name="name"><field name="name" /></h1>
</div>
<group>
<field name="subject_template" placeholder="Subject (placeholders may be used here)"/>
<field
name="subject_template"
placeholder="Subject (placeholders may be used here)"
/>
<field name="model_id" />
<field name="subtype_ids" widget="many2many_tags" />
<field name="position" />
@ -35,7 +38,10 @@
</field>
</record>
<record model="ir.actions.act_window" id="action_mail_notification_custom_subject_tree_all">
<record
model="ir.actions.act_window"
id="action_mail_notification_custom_subject_tree_all"
>
<field name="name">Subject Replacement Templates</field>
<field name="res_model">mail.message.custom.subject</field>
<field name="view_type">form</field>
@ -43,8 +49,12 @@
<field name="view_id" ref="mail_notification_custom_subject_tree" />
</record>
<menuitem id="menu_mail_notification_custom_subject" parent="base.menu_email" action="action_mail_notification_custom_subject_tree_all"
sequence="21"/>
<menuitem
id="menu_mail_notification_custom_subject"
parent="base.menu_email"
action="action_mail_notification_custom_subject_tree_all"
sequence="21"
/>
</data>
</odoo>