Migrate mail_parent_recipient to 15.0

This module comes from a PR in v10 but has never been merged.
This is the related PR:

* https://github.com/OCA/social/pull/963
This commit is contained in:
Thierry Ducrest 2022-11-07 09:27:21 +01:00
parent 985840a348
commit 2a3e8daaae
13 changed files with 116 additions and 177 deletions

View File

@ -0,0 +1 @@
wait for the bot

View File

@ -1,3 +1 @@
# -*- coding: utf-8 -*-
from . import models

View File

@ -1,18 +1,17 @@
# -*- coding: utf-8 -*-
# Copyright 2018 Camptocamp
# Copyright 2018-2022 Camptocamp
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
{
"name": "Mail parent recipient",
"description": "Send email to parent partner if partner's email is empty",
"summary": "Send email to parent partner if partner's email is empty",
"category": "Mail",
"license": "AGPL-3",
"version": "10.0.1.0.0",
"version": "15.0.1.0.0",
"website": "https://github.com/OCA/social",
"author": "Camptocamp, Odoo Community Association (OCA)",
"application": False,
"installable": True,
"depends": [
'mail',
"mail",
],
"data": ["views/res_config_views.xml"]
"data": ["views/res_config_views.xml"],
}

View File

@ -1,5 +1,2 @@
# -*- coding: utf-8 -*-
from . import company
from . import mail_template
from . import res_config

View File

@ -1,16 +0,0 @@
# -*- coding: utf-8 -*-
# Copyright 2022 Camptocamp SA
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
from odoo import fields, models
class ResCompany(models.Model):
_inherit = "res.company"
use_parent_mail_address = fields.Boolean(
string="Use Parent Mail Address",
help="When an email is sent, fallback to partner's parent email if no "
"email is set on the recipient partner."
)

View File

@ -1,38 +1,35 @@
# -*- coding: utf-8 -*-
# Copyright 2018 Camptocamp
# Copyright 2018-2022 Camptocamp
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import models, api
from odoo import models
class MailTemplate(models.Model):
_inherit = 'mail.template'
_inherit = "mail.template"
@api.multi
def generate_recipients(self, results, res_ids):
"""Use partner's parent email as recipient.
Walk up the hierarchy of recipient partners via `parent_id`
and pick the 1st one having an email.
"""
results = super(MailTemplate, self).generate_recipients(
results,
res_ids
results = super().generate_recipients(results, res_ids)
use_parent_address = (
self.env["ir.config_parameter"].sudo().get_param("mail.use_parent_address")
)
disabled = self.env.context.get('no_parent_mail_recipient')
if self.env.user.company_id.use_parent_mail_address and not disabled:
for res_id, values in results.iteritems():
partner_ids = values.get('partner_ids', [])
disabled = self.env.context.get("no_parent_mail_recipient")
if use_parent_address and not disabled:
for res_id, values in results.items():
partner_ids = values.get("partner_ids", [])
partners_with_emails = []
partners = self.env['res.partner'].sudo().browse(partner_ids)
partners = self.env["res.partner"].sudo().browse(partner_ids)
for partner in partners:
if partner.email:
partners_with_emails.append(partner.id)
elif partner.commercial_partner_id.email:
if partner.commercial_partner_id.id not in partners.ids:
partners_with_emails.append(
partner.commercial_partner_id.id
)
# else:
# partner_with_emails.append(partner.id)
results[res_id]['partner_ids'] = partners_with_emails
results[res_id]["partner_ids"] = partners_with_emails
return results

View File

@ -1,17 +1,16 @@
# -*- coding: utf-8 -*-
# Copyright 2022 Camptocamp SA
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
from odoo import fields, models
class BaseConfiguration(models.TransientModel):
_inherit = "base.config.settings"
class ResConfigSettings(models.TransientModel):
_inherit = "res.config.settings"
use_parent_mail_address = fields.Boolean(
related="company_id.use_parent_mail_address",
config_parameter="mail.use_parent_address",
help="""
When checked, for partner without eamil the system will try
to use the email address of the partner's parent.
"""
""",
)

View File

@ -1,2 +1,6 @@
This module modifies how Odoo get email recipients from partners.
Partner's closest parent email will be used as recipient if partner has no email address.
This module will check the recipients of an outgoing email.
If a recipient has no email address set, it will use the parent's partner
(if it has an email address) as recipient.
A setting named `Use Parent Mail Address` is available in
the `Discuss` section of the general settings page.

View File

@ -1,3 +1 @@
# -*- coding: utf-8 -*-
from . import test_mail_parent_recipient

View File

@ -1,122 +1,79 @@
# -*- coding: utf-8 -*-
# Copyright 2018 Camptocamp
# Copyright 2018-2022 Camptocamp
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo.addons.mail.tests.common import TestMail
from odoo.addons.mail.tests.common import MailCommon
class TestMailTemplate(TestMail):
class TestMailTemplate(MailCommon):
@classmethod
def setUpClass(cls):
super(TestMailTemplate, cls).setUpClass()
super().setUpClass()
cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True))
cls.Users = cls.env['res.users']
cls.res_partner = cls.env['res.partner']
cls.mail_template = cls.env['mail.template']
cls.mail_comp_msg = cls.env['mail.compose.message']
cls.env["ir.config_parameter"].set_param("mail.use_parent_address", True)
cls.env.user.company_id.use_parent_mail_address = True
# Company
company_vals = {
'name': 'company_name_test',
'email': 'company.mail.test@company'
cls.res_partner = cls.env["res.partner"]
cls.company_test = cls.res_partner.create(
{
"name": "company_name_test",
"email": "company.mail.test@company",
}
cls.company_test = cls.res_partner.create(company_vals)
# Partners test 1 without email
partner_no_mail_vals = {
'name': 'partner_1',
'parent_id': cls.company_test.id,
)
cls.partner_no_mail = cls.res_partner.create(
{
"name": "partner_1",
"parent_id": cls.company_test.id,
}
# Partners test 2 with email
partner_with_mail_vals = {
'name': 'partner_2',
'email': 'partner.2.mail.test@company',
'parent_id': cls.company_test.id,
}
cls.partner_no_mail = cls.res_partner.create(partner_no_mail_vals)
)
cls.partner_with_mail = cls.res_partner.create(
partner_with_mail_vals
{
"name": "partner_2",
"email": "partner.2.mail.test@company",
"parent_id": cls.company_test.id,
}
)
cls.record = cls.env.ref("base.partner_demo")
cls.email_template = (
cls.env["mail.template"]
.with_context(test_parent_mail_recipient=True)
.create(
{
"model_id": cls.env["ir.model"]
.search([("model", "=", "mail.channel")], limit=1)
.id,
"name": "Pigs Template",
"subject": "${object.name}",
"body_html": "${object.description}",
"partner_to": "",
}
)
)
def create_mail_composer(self, partner_to_send_ids):
email_template = self.env[
'mail.template'
].with_context(test_parent_mail_recipient=True).create({
'model_id': self.env['ir.model'].search([
('model', '=', 'mail.channel')
], limit=1).id,
'name': 'Pigs Template',
'subject': '${object.name}',
'body_html': '${object.description}',
'user_signature': False,
'partner_to': ",".join(partner_to_send_ids),
})
composer = self.mail_comp_msg.with_context({
'test_parent_mail_recipient': True,
'default_composition_mode': 'comment',
'default_model': 'mail.channel',
'default_use_template': True,
}).create({
'subject': 'Forget me subject',
'body': 'Dummy body',
'template_id': email_template.id
})
values = composer.onchange_template_id(
email_template.id,
'comment', 'mail.channel', self.group_pigs.id
)['value']
# use _convert_to_cache to return a browse record list from command
# list or id list for x2many fields
values = composer._convert_to_record(
composer._convert_to_cache(values)
def _get_email_recipient_for(self, partner_to_send_ids):
self.email_template.partner_to = ",".join(
[str(partner_id) for partner_id in partner_to_send_ids]
)
recipients = values['partner_ids']
values = self.email_template.generate_email(self.record.id, ["partner_to"])
return values["partner_ids"]
return recipients
def test_mail_send_to_partner_no_mail(self):
"""Check recipient without email, comapny email is used."""
recipients = self._get_email_recipient_for(self.partner_no_mail.ids)
self.assertEqual(recipients, self.company_test.ids)
def test_1_mail_send_to_partner_no_mail(self):
"""Use company mail if recipient partner has no email."""
recipients = self.create_mail_composer([str(self.partner_no_mail.id)])
def test_mail_send_to_partner_with_mail(self):
"""Check recipient has email, nothing is changed."""
recipients = self._get_email_recipient_for(self.partner_with_mail.ids)
self.assertEqual(recipients, self.partner_with_mail.ids)
self.assertEqual(recipients.email, self.company_test.email)
self.assertNotEqual(recipients.email, self.partner_no_mail.email)
self.assertNotEqual(recipients.email, self.partner_with_mail.email)
def test_mail_send_to_company_test(self):
"""Check company email is used."""
recipients = self._get_email_recipient_for(self.company_test.ids)
self.assertEqual(recipients, self.company_test.ids)
def test_2_mail_send_to_partner_with_mail(self):
"""Use partner mail if recipient partner has an email."""
recipients = self.create_mail_composer(
[str(self.partner_with_mail.id)]
def test_mail_send_to_company_and_partner_no_mail(self):
"""Check a partner is not add twice in recipient list."""
recipients = self._get_email_recipient_for(
[self.partner_no_mail.id, self.company_test.id]
)
self.assertNotEqual(recipients.email, self.company_test.email)
self.assertNotEqual(recipients.email, self.partner_no_mail.email)
self.assertEqual(recipients.email, self.partner_with_mail.email)
def test_3_mail_send_to_company_test(self):
"""Use company mail if recipient is the company."""
recipients = self.create_mail_composer([str(self.company_test.id)])
self.assertEqual(recipients.email, self.company_test.email)
self.assertNotEqual(recipients.email, self.partner_no_mail.email)
self.assertNotEqual(recipients.email, self.partner_with_mail.email)
def test_4_mail_send_to_company_and_partner_no_mail(self):
""" Use only one time company mail if recipient is the company
and partner without mail.
"""
recipients = self.create_mail_composer([
str(self.partner_no_mail.id),
str(self.company_test.id)
])
self.assertEqual(recipients.email, self.company_test.email)
self.assertNotEqual(recipients.email, self.partner_no_mail.email)
self.assertNotEqual(recipients.email, self.partner_with_mail.email)
self.assertEqual(recipients, self.company_test.ids)

View File

@ -1,18 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<data>
<record id="view_configuration_mail_parent_recipient" model="ir.ui.view">
<field name="name">mail.parent.recipient.config.settings</field>
<field name="model">base.config.settings</field>
<field name="inherit_id" ref="mail.view_general_configuration_mail_alias_domain"/>
<field name="model">res.config.settings</field>
<field name="inherit_id" ref="mail.res_config_settings_view_form" />
<field name="arch" type="xml">
<xpath expr="//div[@name='email']" position='inside'>
<div>
<field name="use_parent_mail_address"/>
<label for="use_parent_mail_address"/>
<xpath expr="//div[@id='emails']" position='inside'>
<div class="col-12 col-lg-6 o_setting_box" id="mail_parent_recipient_setting">
<div class="o_setting_left_pane">
<field name="use_parent_mail_address" />
</div>
<div class="o_setting_right_pane">
<label for="use_parent_mail_address" />
<div class="text-muted" id="mail_parent_recipient">
Use partner's parent email if recipient partner has not email set.
</div>
</div>
</div>
</xpath>
</field>
</record>
</data>
</odoo>

View File

@ -1 +0,0 @@
__import__('pkg_resources').declare_namespace(__name__)

View File

@ -1 +0,0 @@
__import__('pkg_resources').declare_namespace(__name__)