PURPOSE Visitors are useful in terms of marketing analysis but they can bloat the database really quickly if you have a lot of traffic on your website. This commit aims to relieve the database by fully deleting inactive visitors. SPECS On an active website, you can easily reach hundreds or even thousands of visitors per day. While active visitors are useful for marketing purposes, inactive ones were archived after a period of inactivity (i.e: not connected for X days in a row, where X is configurable as a parameter and 30 by default). But archiving visitors is not really useful either. We don't see any specific cases where you would want to restore some visitors. That means we are better off unlinking the visitors completely to save database storage space. We want to make exceptions and avoid deletion of inactive visitors in some cases: - When they are linked to a partner (meaning most likely linked to a registered user) - When they are linked to leads - When they are registered to events Several tests were added to ensure that leads matching these conditions are not unlinked. We also took this opportunity to enforce the "_link_to_visitor" rules in those tests to make sure that: - When visitors are linked, the leads are merged into the main visitor - When visitors are linked, the event tickets are merged into the main visitor - When visitors are linked, the wishlisted tracks are merged into the main visitor This is also preliminary work for a commit that will move the push_token of website.visitors to a separate table. That will in turn allow us to link the push_tokens within the "_link_to_visitor" method and delete the linked visitor instead of archiving it. LINKS Task-2410217 Part-of: odoo/odoo#65113
67 lines
2.5 KiB
Python
67 lines
2.5 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
from odoo import fields
|
|
from odoo.addons.website.tests.test_website_visitor import WebsiteVisitorTests
|
|
from odoo.addons.website_event.tests.common import TestEventOnlineCommon
|
|
from odoo.tests import tagged
|
|
|
|
|
|
@tagged('website_visitor')
|
|
class TestEventVisitor(TestEventOnlineCommon, WebsiteVisitorTests):
|
|
|
|
def test_clean_inactive_visitors_event(self):
|
|
""" Visitors registered to events should not be deleted even if not connected recently. """
|
|
active_visitors = self.env['website.visitor'].create([{
|
|
'name': 'Lead Douglas',
|
|
'lang_id': self.env.ref('base.lang_en').id,
|
|
'country_id': self.env.ref('base.be').id,
|
|
'website_id': 1,
|
|
'last_connection_datetime': datetime.now() - timedelta(days=8),
|
|
'event_registration_ids': [(0, 0, {
|
|
'event_id': self.event_0.id
|
|
})]
|
|
}])
|
|
|
|
self._test_unlink_old_visitors(self.env['website.visitor'], active_visitors)
|
|
|
|
def test_link_to_visitor_event(self):
|
|
""" Same as parent's 'test_link_to_visitor' except we also test that event
|
|
registrations are merged into main visitor. """
|
|
[main_visitor, linked_visitor] = self.env['website.visitor'].create([
|
|
self._prepare_main_visitor_data(),
|
|
self._prepare_linked_visitor_data()
|
|
])
|
|
|
|
event_1 = self.env['event.event'].create({
|
|
'name': 'OtherEvent',
|
|
'auto_confirm': True,
|
|
'date_begin': fields.Datetime.to_string(datetime.today() + timedelta(days=1)),
|
|
'date_end': fields.Datetime.to_string(datetime.today() + timedelta(days=15)),
|
|
})
|
|
linked_visitor.write({
|
|
'event_registration_ids': [(0, 0, {
|
|
'event_id': event_1.id
|
|
})]
|
|
})
|
|
|
|
self.assertEqual(self.event_0, main_visitor.event_registered_ids)
|
|
self.assertEqual(event_1, linked_visitor.event_registered_ids)
|
|
|
|
linked_visitor._link_to_visitor(main_visitor)
|
|
self.assertVisitorDeactivated(linked_visitor, main_visitor)
|
|
|
|
# main_visitor is now attending both events
|
|
self.assertEqual(self.event_0 | event_1, main_visitor.event_registered_ids)
|
|
|
|
def _prepare_main_visitor_data(self):
|
|
values = super()._prepare_main_visitor_data()
|
|
values.update({
|
|
'event_registration_ids': [(0, 0, {
|
|
'event_id': self.event_0.id
|
|
})]
|
|
})
|
|
return values
|