From 345a5a4aa816c146a30fe0bd07446eb9ee351a45 Mon Sep 17 00:00:00 2001 From: Nans Lefebvre Date: Thu, 23 May 2019 10:56:31 +0000 Subject: [PATCH] [FIX] crm: set the email_from from the kanban quick create When setting the partner of a lead, on the form view the email_from is automatically populated by the onchange on the partner_id. This is not the case when created programmatically, and this is not even the case when created from the kanban view. This can lead to an unexpected failure to send emails down the line. We apply the onchange in the create to fill all values consistently in all cases. In case the values were already set, this simply won't change anything. Note that we need to adapt the test from website_crm_partner_assign. It uses demo data that makes little sense. The opportunity crm.crm_case_21 is set in the UK, with a partner base.res_partner_4 in the US, in the state Escuintla of Guatemala. With the change from above, all the address fields on the opportunity are populated by the onchange, from the partner. opw 2000241 closes odoo/odoo#33622 Signed-off-by: Nans Lefebvre (len) --- addons/crm/models/crm_lead.py | 7 ++++--- .../tests/test_partner_assign.py | 15 ++++++++++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/addons/crm/models/crm_lead.py b/addons/crm/models/crm_lead.py index b704979de85..12bbac57459 100644 --- a/addons/crm/models/crm_lead.py +++ b/addons/crm/models/crm_lead.py @@ -312,9 +312,10 @@ class Lead(models.Model): if vals.get('user_id') and 'date_open' not in vals: vals['date_open'] = fields.Datetime.now() - if context.get('default_partner_id') and not vals.get('email_from'): - partner = self.env['res.partner'].browse(context['default_partner_id']) - vals['email_from'] = partner.email + partner_id = vals.get('partner_id') or context.get('default_partner_id') + onchange_values = self._onchange_partner_id_values(partner_id) + onchange_values.update(vals) # we don't want to overwrite any existing key + vals = onchange_values # context: no_log, because subtype already handle this return super(Lead, self.with_context(context, mail_create_nolog=True)).create(vals) diff --git a/addons/website_crm_partner_assign/tests/test_partner_assign.py b/addons/website_crm_partner_assign/tests/test_partner_assign.py index 137cd773c33..4bab904b3b8 100644 --- a/addons/website_crm_partner_assign/tests/test_partner_assign.py +++ b/addons/website_crm_partner_assign/tests/test_partner_assign.py @@ -16,6 +16,19 @@ class TestPartnerAssign(TransactionCase): def setUp(self): super(TestPartnerAssign, self).setUp() + self.customer_uk = self.env['res.partner'].create({ + 'name': 'Nigel', + 'country_id': self.env.ref('base.uk').id, + 'city': 'Birmingham', + 'zip': 'B46 3AG', + 'street': 'Cannon Hill Park', + }) + self.lead_uk = self.env['crm.lead'].create({ + 'type': 'opportunity', + 'name': 'Office Design and Architecture', + 'partner_id': self.customer_uk.id + }) + def geo_find(addr, apikey): return { 'Wavre, Belgium': (50.7158956, 4.6128075), @@ -51,7 +64,7 @@ class TestPartnerAssign(TransactionCase): "partner_weight": 10, }) - lead = self.env.ref('crm.crm_case_21') + lead = self.lead_uk # In order to test find nearest Partner functionality and assign to opportunity, # I Set Geo Lattitude and Longitude according to partner address.