From 75ba9e6bfa64f7b166e0797b2002953342eb3a80 Mon Sep 17 00:00:00 2001 From: Renaud Thiry Date: Thu, 9 Nov 2023 07:10:05 +0100 Subject: [PATCH] [FIX] website_event: set utm values for event registrations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The utm parameters i.e. utm_source, utm_campaign, utm_medium, are only set when a sale order is attached to an event registration. If no sale order is generated the event registration won't have any source despite it being accessed via a campaign. Reproduction steps: 1. Create an event and copy its website link. 2. Go to link tracker and create a tracked link with utm values filled. 3. Use this tracked link in incognito preferably to register for the event. 4. Check the attendees of the event and check the marketing utm values. The registration has no campaign. When it should have one. This is because event.registration mistakenly does not inherit from the utm mixin. The fix is to call the default_get of the mixin as it is independant from any field defined in the mixin. OPW-3222179 task-3458877 closes odoo/odoo#151047 X-original-commit: 06803bc12115ccb2afb7ea7b8dd101e3a75bfb67 Signed-off-by: Warnon Aurélien (awa) Signed-off-by: Renaud Thiry (reth) --- addons/event/models/event_registration.py | 12 +++++ .../website_event/tests/test_website_event.py | 44 +++++++++++++++++-- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/addons/event/models/event_registration.py b/addons/event/models/event_registration.py index 73e229a1265..b258ab6973e 100644 --- a/addons/event/models/event_registration.py +++ b/addons/event/models/event_registration.py @@ -86,6 +86,18 @@ class EventRegistration(models.Model): registrations_confirmed.event_id._check_seats_availability() registrations_confirmed.event_ticket_id._check_seats_availability() + def default_get(self, fields): + ret_vals = super().default_get(fields) + utm_mixin_fields = ("campaign_id", "medium_id", "source_id") + utm_fields = ("utm_campaign_id", "utm_medium_id", "utm_source_id") + if not any(field in utm_fields for field in fields): + return ret_vals + utm_mixin_defaults = self.env['utm.mixin'].default_get(utm_mixin_fields) + for (mixin_field, field) in zip(utm_mixin_fields, utm_fields): + if field in fields and utm_mixin_defaults.get(mixin_field): + ret_vals[field] = utm_mixin_defaults[mixin_field] + return ret_vals + @api.depends('partner_id') def _compute_name(self): for registration in self: diff --git a/addons/website_event/tests/test_website_event.py b/addons/website_event/tests/test_website_event.py index 6513a2892d9..d646531fbd3 100644 --- a/addons/website_event/tests/test_website_event.py +++ b/addons/website_event/tests/test_website_event.py @@ -4,16 +4,54 @@ from datetime import datetime, timedelta from dateutil.relativedelta import relativedelta -from odoo import fields +from odoo import fields, http from odoo.addons.base.tests.common import HttpCaseWithUserDemo, HttpCaseWithUserPortal from odoo.addons.mail.tests.common import mail_new_test_user from odoo.addons.website.tests.test_base_url import TestUrlCommon -from odoo.addons.website_event.tests.common import OnlineEventCase +from odoo.addons.website_event.tests.common import TestEventOnlineCommon, OnlineEventCase from odoo.exceptions import AccessError -from odoo.tests import tagged +from odoo.tests import HttpCase, tagged from odoo.tools import mute_logger from odoo.tests.common import users +class TestEventRegisterUTM(HttpCase, TestEventOnlineCommon): + def test_event_registration_utm_values(self): + self.event_0.registration_ids.unlink() + self.event_0.write({ + 'event_ticket_ids': [ + (5, 0), + (0, 0, { + 'name': 'First Ticket', + }), + ], + 'is_published': True + }) + event_campaign = self.env['utm.campaign'].create({'name': 'utm event test'}) + + self.authenticate(None, None) + self.opener.cookies.update({ + 'odoo_utm_campaign': event_campaign.name, + 'odoo_utm_source': self.env.ref('utm.utm_source_newsletter').name, + 'odoo_utm_medium': self.env.ref('utm.utm_medium_email').name + }) + event_questions = self.event_0.question_ids + name_question = event_questions.filtered(lambda q: q.question_type == 'name') + email_question = event_questions.filtered(lambda q: q.question_type == 'email') + self.assertTrue(name_question and email_question) + # get 1 free ticket + self.url_open(f'/event/{self.event_0.id}/registration/confirm', data={ + f'1-name-{name_question.id}': 'Bob', + f'1-email-{email_question.id}': 'bob@test.lan', + '1-event_ticket_id': self.event_0.event_ticket_ids[0].id, + 'csrf_token': http.Request.csrf_token(self), + }) + new_registration = self.event_0.registration_ids + self.assertEqual(len(new_registration), 1) + self.assertEqual(new_registration.utm_campaign_id, event_campaign) + self.assertEqual(new_registration.utm_source_id, self.env.ref('utm.utm_source_newsletter')) + self.assertEqual(new_registration.utm_medium_id, self.env.ref('utm.utm_medium_email')) + + @tagged('post_install', '-at_install') class TestUi(HttpCaseWithUserDemo, HttpCaseWithUserPortal):