Skip to content

Commit

Permalink
[FIX] mail: create partner from template in right company
Browse files Browse the repository at this point in the history
Before this commit, when creating a partner automatically when creating
a message with a template, the company on the partner was wrongly set

After this commit, we try to retrieve the company from the model on the template

OPW 1934392
  • Loading branch information
kebeclibre committed Feb 6, 2019
1 parent 530f364 commit ea2a4de
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
10 changes: 9 additions & 1 deletion addons/mail/models/mail_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,12 +385,20 @@ def generate_recipients(self, results, res_ids):
results[res_id].pop('partner_to', None)
results[res_id].update(recipients)

records_company = None
if self._context.get('tpl_partners_only') and self.model and results and 'company_id' in self.env[self.model]._fields:
records = self.env[self.model].browse(results.keys()).read(['company_id'])
records_company = {rec['id']: rec['company_id'][0] for rec in records}

for res_id, values in results.items():
partner_ids = values.get('partner_ids', list())
if self._context.get('tpl_partners_only'):
mails = tools.email_split(values.pop('email_to', '')) + tools.email_split(values.pop('email_cc', ''))
Partner = self.env['res.partner']
if records_company:
Partner = Partner.with_context(default_company_id=records_company[res_id])
for mail in mails:
partner_id = self.env['res.partner'].find_or_create(mail)
partner_id = Partner.find_or_create(mail)
partner_ids.append(partner_id)
partner_to = values.pop('partner_to', '')
if partner_to:
Expand Down
1 change: 1 addition & 0 deletions addons/test_mail/models/test_mail_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class MailTestStandard(models.Model):
email_from = fields.Char()
user_id = fields.Many2one('res.users', 'Responsible', track_visibility='onchange')
umbrella_id = fields.Many2one('mail.test', track_visibility='onchange')
company_id = fields.Many2one('res.company')


class MailTestActivity(models.Model):
Expand Down
34 changes: 34 additions & 0 deletions addons/test_mail/tests/test_mail_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import base64
from datetime import datetime, timedelta
from unittest.mock import patch

from odoo.addons.test_mail.tests.common import BaseFunctionalTest, MockEmails, TestRecipients
from odoo.addons.test_mail.tests.common import mail_new_test_user
Expand Down Expand Up @@ -202,3 +203,36 @@ def test_template_add_context_action(self):
# self.env['mail.mail'].process_email_queue(ids=[mail_in_2_days.id])
# self.assertEqual(mail_in_2_days.state, 'outgoing')
# self.assertEqual(mail_now.exists() | mail_in_2_days.exists(), mail_in_2_days)

def test_create_partner_from_tracking_multicompany(self):
company1 = self.env['res.company'].create({'name': 'company1'})
self.env.user.write({'company_ids': [(4, company1.id, False)]})
self.assertNotEqual(self.env.user.company_id, company1)

email_new_partner = "diamonds@rust.com"
Partner = self.env['res.partner']
self.assertFalse(Partner.search([('email', '=', email_new_partner)]))

template = self.env['mail.template'].create({
'model_id': self.env['ir.model']._get('mail.test.track').id,
'name': 'AutoTemplate',
'subject': 'autoresponse',
'email_from': self.env.user.email_formatted,
'email_to': "${object.email_from}",
'body_html': "<div>A nice body</div>",
})

def patched_message_track_post_template(*args, **kwargs):
args[0].message_post_with_template(template.id)
return True

with patch('odoo.addons.mail.models.mail_thread.MailThread._message_track_post_template', patched_message_track_post_template):
self.env['mail.test.track'].create({
'email_from': email_new_partner,
'company_id': company1.id,
'user_id': self.env.user.id, # trigger tracking,
})

new_partner = Partner.search([('email', '=', email_new_partner)])
self.assertTrue(new_partner)
self.assertEqual(new_partner.company_id, company1)

0 comments on commit ea2a4de

Please sign in to comment.