Browse Source

[ADD][mail_parent_recipient][10] Add module to use mail of first partner

pull/332/head
Tonow-c2c 6 years ago
parent
commit
fb20523977
  1. 3
      mail_parent_recipient/__init__.py
  2. 22
      mail_parent_recipient/__manifest__.py
  3. 3
      mail_parent_recipient/models/__init__.py
  4. 35
      mail_parent_recipient/models/mail_template.py
  5. 1
      mail_parent_recipient/readme/CONTRIBUTORS.rst
  6. 4
      mail_parent_recipient/readme/CREDITS.rst
  7. 3
      mail_parent_recipient/readme/DESCRIPTION.rst
  8. 0
      mail_parent_recipient/readme/HISTORY.rst
  9. 3
      mail_parent_recipient/tests/__init__.py
  10. 101
      mail_parent_recipient/tests/test_mail_parent_recipient.py

3
mail_parent_recipient/__init__.py

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

22
mail_parent_recipient/__manifest__.py

@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
# Copyright 2018 Camptocamp
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
{
"name": "Mail parent recipient",
"summary": "Sending the email in any case by the main partner",
"category": "Mail",
"license": "LGPL-3",
"version": "10.0.1.0.0",
"website": "https://github.com/OCA/social",
"author": "Camptocamp, Odoo Community Association (OCA)",
"license": "AGPL-3",
"application": False,
"installable": True,
"pre_init_hook": "pre_init_hook",
"post_init_hook": "post_init_hook",
"post_load": "post_load",
"uninstall_hook": "uninstall_hook",
"depends": [
'mail',
]
}

3
mail_parent_recipient/models/__init__.py

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

35
mail_parent_recipient/models/mail_template.py

@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
# Copyright 2018 Camptocamp
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import models, api
class MailTemplate(models.Model):
_inherit = 'mail.template'
@api.multi
def generate_recipients(self, results, res_ids):
"""
Modify the code that set the default recipient in an email to the first
partner having an email set following the parents tree.
If no parent with an email is found,
it reverts to the popup asking to set an email for the current partner
"""
results = super(MailTemplate, self).generate_recipients(
results,
res_ids
)
for res_id, values in results.iteritems():
partner_ids = values.get('partner_ids', [])
partners_with_emails = []
for partner in self.env['res.partner'].sudo().browse(partner_ids):
current = partner
while current:
if current.email:
break
current = current.parent_id
partners_with_emails.append(current.id or partner.id)
results[res_id]['partner_ids'] = partners_with_emails
return results

1
mail_parent_recipient/readme/CONTRIBUTORS.rst

@ -0,0 +1 @@
* Thomas Nowicki <thomas.nowicki@camptocamp.com>

4
mail_parent_recipient/readme/CREDITS.rst

@ -0,0 +1,4 @@
The development of this module has been financially supported by:
* Camptocamp Thomas Nowicki

3
mail_parent_recipient/readme/DESCRIPTION.rst

@ -0,0 +1,3 @@
This modules set the default recipient in an email to the first partner having
an email set following the parents tree. If no parent with an email is found,
it reverts to the popup asking to set an email for the current partner.

0
mail_parent_recipient/readme/HISTORY.rst

3
mail_parent_recipient/tests/__init__.py

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

101
mail_parent_recipient/tests/test_mail_parent_recipient.py

@ -0,0 +1,101 @@
# -*- coding: utf-8 -*-
# Copyright 2018 Camptocamp
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo.addons.mail.tests.common import TestMail
class TestMailTemplate(TestMail):
def setUp(self):
super(TestMailTemplate, self).setUp()
self.Users = self.env['res.users']
self.res_parter = self.env['res.partner']
self.mail_template = self.env['mail.template']
self.mail_comp_msg = self.env['mail.compose.message']
# Company
company_vals = {
'name': 'company_name_test',
'email': 'company.mail.test@company'
}
self.company_test = self.res_parter.create(company_vals)
# Partners test 1 without email
partner_no_mail_vals = {
'name': 'partner_1',
'parent_id': self.company_test.id,
}
# Partners test 2 with email
partner_with_mail_vals = {
'name': 'partner_2',
'email': 'partner.2.mail.test@company',
'parent_id': self.company_test.id,
}
self.partner_no_mail = self.res_parter.create(partner_no_mail_vals)
self.partner_with_mail = self.res_parter.create(partner_with_mail_vals)
self.email_template = self.env['mail.template'].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': '${object.partner_id.id}', # TODO change
})
self.composer = self.mail_comp_msg.with_context({
'default_composition_mode': 'comment',
'default_model': 'mail.channel',
'default_use_template': True,
}).create({
'subject': 'Forget me subject',
'body': 'Dummy body',
'template_id': self.email_template.id
})
# def test_1_mail_send_to_partner_no_mail(self):
# """
# Mail should only send with company mail
# even if is sended to partner_no_mail
# """
# import pdb; pdb.set_trace()
# mail_id = self.email_template.send_mail(self.partner_no_mail.id)
# mail = self.env['mail.mail'].browse(mail_id)
#
# self.assertEqual(mail.recipient_ids, self.company_test.id)
# self.assertNotEqual(mail.recipient_ids, self.partner_no_mail.id)
# self.assertNotEqual(mail.recipient_ids, self.partner_with_mail.id)
# self.assertEqual(len(mail.recipient_ids), 1)
#
# def test_2_mail_send_to_partner_with_mail(self):
# """
# Mail should only send with company mail
# even if is sended to partner_with_mail
# """
# mail_id = self.email_template.send_mail(self.partner_with_mail.id)
# mail = self.env['mail.mail'].browse(mail_id)
#
# self.assertEqual(mail.recipient_ids, self.company_test.id)
# self.assertNotEqual(mail.recipient_ids, self.partner_no_mail.id)
# self.assertNotEqual(mail.recipient_ids, self.partner_with_mail.id)
# self.assertEqual(len(mail.recipient_ids), 1)
#
# def test_3_mail_send_to_company_test(self):
# """
# Mail should only send with company mail
# """
# mail_id = self.email_template.send_mail(self.company_test.id)
# mail = self.env['mail.mail'].browse(mail_id)
#
# self.assertEqual(mail.recipient_ids, self.company_test.id)
# self.assertNotEqual(mail.recipient_ids, self.partner_no_mail.id)
# self.assertNotEqual(mail.recipient_ids, self.partner_with_mail.id)
# self.assertEqual(len(mail.recipient_ids), 1)
Loading…
Cancel
Save