From f9154bb6db9987d4e8780063abfbcd98d7ad4a1e Mon Sep 17 00:00:00 2001 From: Guillaume-gdi Date: Fri, 1 Dec 2023 13:39:05 +0100 Subject: [PATCH] [FIX] website: make email_to of contactus form dynamic again The email_to field of the contactus form have to be dynamic except if the user manually set it via the form options. Unfortunately, the website signature task broke this feature. This commit fixes it and adds a test to prevent this regression in the future. Steps to reproduce the bug: - Go to the contactus page - Edit the page - Add a field on the form - Save the page - Change the company email address via the settings => The contactus form still send the email to the old email address. Related to task-3382195 X-original-commit: 10b4db60fbd077936b5c9586485fa40c3c890d3f Part-of: odoo/odoo#147933 --- .../static/tests/tours/website_form_editor.js | 12 ++++++++++++ .../tests/tours/website_form_editor_frontend.js | 10 ++++++++++ addons/website/tests/test_website_form_editor.py | 8 ++++++++ addons/website/tools.py | 13 ++++++++++--- 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/addons/website/static/tests/tours/website_form_editor.js b/addons/website/static/tests/tours/website_form_editor.js index dd4ac97428a..48f8715e2a8 100644 --- a/addons/website/static/tests/tours/website_form_editor.js +++ b/addons/website/static/tests/tours/website_form_editor.js @@ -707,4 +707,16 @@ } ]); + wTourUtils.registerWebsitePreviewTour('website_form_contactus_change_random_option', { + test: true, + url: '/contactus', + edition: true, + }, () => editContactUs([ + { + content: "Change a random option", + trigger: '[data-set-mark] input', + run: 'text_blur **', + }, + ])); + export default {}; diff --git a/addons/website/static/tests/tours/website_form_editor_frontend.js b/addons/website/static/tests/tours/website_form_editor_frontend.js index cd2ebb28083..3fb82bc8f0d 100644 --- a/addons/website/static/tests/tours/website_form_editor_frontend.js +++ b/addons/website/static/tests/tours/website_form_editor_frontend.js @@ -217,3 +217,13 @@ registry.category("web_tour.tours").add('website_form_contactus_submit', { isCheck: true, }, ]}); +registry.category("web_tour.tours").add('website_form_contactus_check_changed_email', { + test: true, + url: '/contactus', + steps: () => [ + { + content: "Check that the recipient email is updated", + trigger: 'form:has(input[name="email_to"][value="after.change@mail.com"])', + isCheck: true, + }, +]}); diff --git a/addons/website/tests/test_website_form_editor.py b/addons/website/tests/test_website_form_editor.py index 1fc8c4760b7..445cbb6191c 100644 --- a/addons/website/tests/test_website_form_editor.py +++ b/addons/website/tests/test_website_form_editor.py @@ -41,3 +41,11 @@ class TestWebsiteFormEditor(HttpCaseWithUserPortal): def test_website_form_conditional_required_checkboxes(self): self.start_tour('/', 'website_form_conditional_required_checkboxes', login="admin") + + def test_contactus_form_email_stay_dynamic(self): + # The contactus form should always be sent to the company email except + # if the user explicitly changed it in the options. + self.env.company.email = 'before.change@mail.com' + self.start_tour('/contactus', 'website_form_contactus_change_random_option', login="admin") + self.env.company.email = 'after.change@mail.com' + self.start_tour('/contactus', 'website_form_contactus_check_changed_email', login="portal") diff --git a/addons/website/tools.py b/addons/website/tools.py index 952ab0d5578..debfb113134 100644 --- a/addons/website/tools.py +++ b/addons/website/tools.py @@ -203,10 +203,17 @@ def add_form_signature(html_fragment, env_sudo): # the value of email_to can still be None in case of default value if 'email_to' not in form_values: continue - elif not form_values['email_to'].attrib.get('value'): - form_values['email_to'].attrib['value'] = env_sudo.company.email or '' + + email_to_value = form_values['email_to'].attrib.get('value') + if (not email_to_value + or (email_to_value == 'info@yourcompany.example.com' + and html_fragment.xpath('//span[@data-for="contactus_form"]'))): + # This means that the mail will be sent to the value of the dataFor + # which is the company email. + email_to_value = env_sudo.company.email or '' + has_cc = {'email_cc', 'email_bcc'} & form_values.keys() - value = form_values['email_to'].attrib['value'] + (':email_cc' if has_cc else '') + value = email_to_value + (':email_cc' if has_cc else '') hash_value = hmac(env_sudo, 'website_form_signature', value) if has_cc: hash_value += ':email_cc'