From 9be2ec255bc9a25ccffbd22adefbecae02374258 Mon Sep 17 00:00:00 2001 From: Xavier ALT Date: Wed, 6 Dec 2023 18:37:06 +0100 Subject: [PATCH] [FIX] crm: handle customer creation on. lead -> oppor. w/ archived lang To reproduce: 1. enable 'debug' mode 2. go to menu "Settings / Translations / Languages" and enable a new language (ex: nl_NL) 3 create a new lead and set that language (IMPORTANT: do not set a customer) 4. go back to menu "Settings / Translations / Languages" and disable the language you previously enabled at step 2. 5. from the lead list view, selection the lead you created at step 3, click "Action" and choose "Convert to opportunities" 6. In the wizard choose for: * Conversion Action: Convert to opportunity * Customer: Create a new customer 7. Click button "Convert to opportunity" This crash with the following error: ``` Traceback (most recent call last): File "/data/build/odoo/odoo/http.py", line 654, in _handle_exception return super(JsonRequest, self)._handle_exception(exception) File "/data/build/odoo/odoo/http.py", line 301, in _handle_exception raise exception.with_traceback(None) from new_cause ValueError: Wrong value for res.partner.lang: 'nl_NL' ``` This commit ensure that when converting a lead to an opportunity, we don't crash if language is archived. OPW-3617359 closes odoo/odoo#147003 X-original-commit: 208d6ed021f62b1b53f75129a18e8a0d302f47b0 Signed-off-by: Thibault Delavallee (tde) Signed-off-by: Xavier Alt (xal) --- addons/crm/models/crm_lead.py | 2 +- addons/crm/tests/test_crm_lead_convert.py | 21 +++++++++++++++++++ .../crm/tests/test_crm_lead_notification.py | 4 ++-- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/addons/crm/models/crm_lead.py b/addons/crm/models/crm_lead.py index e95a3bfa1ed..0d64ebf943c 100644 --- a/addons/crm/models/crm_lead.py +++ b/addons/crm/models/crm_lead.py @@ -1902,7 +1902,7 @@ class Lead(models.Model): 'is_company': is_company, 'type': 'contact' } - if self.lang_id: + if self.lang_id.active: res['lang'] = self.lang_id.code return res diff --git a/addons/crm/tests/test_crm_lead_convert.py b/addons/crm/tests/test_crm_lead_convert.py index 1fd7ccdaf4b..f329e557c58 100644 --- a/addons/crm/tests/test_crm_lead_convert.py +++ b/addons/crm/tests/test_crm_lead_convert.py @@ -246,6 +246,27 @@ class TestLeadConvert(crm_common.TestLeadConvertCommon): # TDE FIXME: should take into account normalized email version, not encoded one # self.assertEqual(convert.partner_id, customer) + @users('user_sales_manager') + def test_lead_convert_no_lang(self): + """ Ensure converting a lead with an archived language correctly falls back on the default partner language. """ + inactive_lang = self.env["res.lang"].sudo().create({ + 'code': 'en_ZZ', + 'name': 'Inactive Lang', + 'active': False, + }) + + lead = self.lead_1.with_user(self.env.user) + lead.lang_id = inactive_lang + + convert = self.env['crm.lead2opportunity.partner'].with_context({ + 'active_model': 'crm.lead', + 'active_id': self.lead_1.id, + 'active_ids': self.lead_1.ids, + }).create({'action': 'create'}) + convert.action_apply() + self.assertTrue(lead.partner_id) + self.assertEqual(lead.partner_id.lang, 'en_US') + @users('user_sales_manager') def test_lead_convert_internals(self): """ Test internals of convert wizard """ diff --git a/addons/crm/tests/test_crm_lead_notification.py b/addons/crm/tests/test_crm_lead_notification.py index 1ecc805ad86..c31397b60f9 100644 --- a/addons/crm/tests/test_crm_lead_notification.py +++ b/addons/crm/tests/test_crm_lead_notification.py @@ -112,7 +112,7 @@ class NewLeadNotification(TestCrmCommon): } ]) expected_list = [ - (False, self.test_email, None, 'Customer Email', {'lang': 'fr_FR'}), + (False, self.test_email, None, 'Customer Email', {'lang': None}), (False, self.test_email, 'en_US', 'Customer Email', {'lang': 'en_US'}), ] for lead, expected in zip(leads, expected_list): @@ -121,7 +121,7 @@ class NewLeadNotification(TestCrmCommon): self.assertEqual(len(res), 1) self.assertEqual(res[0][:4], expected[:4]) for partner_fname in expected[4]: - found = res[0][4][partner_fname] + found = res[0][4].get(partner_fname) expected = expected[4][partner_fname] self.assertEqual( found, expected,