From 49085a122816fd889c34ba1e96dfad78cdff7059 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Wed, 10 Nov 2021 15:18:30 +0000 Subject: [PATCH] [FIX] digest: improve tone down Currently digest sending is toned down when being sent if subscribed users did not connect since more than 3 days. It allows to avoid spamming people not being active or present anymore. This is currently limited to daily digests that are set to weekly. However it may continues to send digests each week until the digest is de-activated. In this commit we tone down the digest from daily to quarterly depending on users activity. It is still based on overall activity and not individual users to avoid complex computation on heavy digests. This will be done in further cleaning of digests. Task-2688856 (Digest tone down improvement) X-original-commit: f916354baec5bb69511069a829ab1648a5ebaccd Part-of: odoo/odoo#79877 --- addons/digest/models/digest.py | 31 +++++++++++++++++++---- addons/digest/tests/test_digest.py | 40 ++++++++++++++++++++++++++++-- 2 files changed, 64 insertions(+), 7 deletions(-) diff --git a/addons/digest/models/digest.py b/addons/digest/models/digest.py index 658e00e4eac..f03c1d2dac0 100644 --- a/addons/digest/models/digest.py +++ b/addons/digest/models/digest.py @@ -121,7 +121,7 @@ class Digest(models.Model): lang=user.lang )._action_send_to_user(user, tips_count=1) if digest in to_slowdown: - digest.write({'periodicity': 'weekly'}) + digest.write({'periodicity': self._get_next_periodicity()[0]}) digest.next_run_date = digest._get_next_run_date() def _action_send_to_user(self, user, tips_count=1, consum_tips=True): @@ -276,7 +276,11 @@ class Digest(models.Model): """ preferences = [] if self._context.get('digest_slowdown'): - preferences.append(_("We have noticed you did not connect these last few days so we've automatically switched your preference to weekly Digests.")) + _dummy, new_perioridicy_str = self._get_next_periodicity() + preferences.append( + _("We have noticed you did not connect these last few days. We have automatically switched your preference to %(new_perioridicy_str)s Digests.", + new_perioridicy_str=new_perioridicy_str) + ) elif self.periodicity == 'daily' and user.has_group('base.group_erp_manager'): preferences.append(Markup('

%s
%s

') % ( _('Prefer a broader overview ?'), @@ -338,17 +342,34 @@ class Digest(models.Model): return margin def _check_daily_logs(self): - three_days_ago = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) - relativedelta(days=3) + """ Badly named method that checks user logs and slowdown the sending + of digest emails based on recipients being away. """ + today = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) to_slowdown = self.env['digest.digest'] - for digest in self.filtered(lambda digest: digest.periodicity == 'daily'): + for digest in self: + if digest.periodicity == 'daily': # 3 days ago + limit_dt = today - relativedelta(days=3) + elif digest.periodicity == 'weekly': # 2 weeks ago + limit_dt = today - relativedelta(days=14) + elif digest.periodicity == 'monthly': # 1 month ago + limit_dt = today - relativedelta(months=1) + elif digest.periodicity == 'quarterly': # 3 month ago + limit_dt = today - relativedelta(months=3) users_logs = self.env['res.users.log'].sudo().search_count([ ('create_uid', 'in', digest.user_ids.ids), - ('create_date', '>=', three_days_ago) + ('create_date', '>=', limit_dt) ]) if not users_logs: to_slowdown += digest return to_slowdown + def _get_next_periodicity(self): + if self.periodicity == 'weekly': + return 'monthly', _('monthly') + if self.periodicity == 'monthly': + return 'quarterly', _('quarterly') + return 'weekly', _('weekly') + def _format_currency_amount(self, amount, currency_id): pre = currency_id.position == 'before' symbol = u'{symbol}'.format(symbol=currency_id.symbol or '') diff --git a/addons/digest/tests/test_digest.py b/addons/digest/tests/test_digest.py index 1c403433da4..196550b1bd1 100644 --- a/addons/digest/tests/test_digest.py +++ b/addons/digest/tests/test_digest.py @@ -128,6 +128,20 @@ class TestDigest(mail_test.MailCommon): self.assertEqual(digest.periodicity, 'weekly') + # no logs for employee -> should tone down periodicity + digest.flush() + with self.mock_mail_gateway(): + digest.action_send() + + self.assertEqual(digest.periodicity, 'monthly') + + # no logs for employee -> should tone down periodicity + digest.flush() + with self.mock_mail_gateway(): + digest.action_send() + + self.assertEqual(digest.periodicity, 'quarterly') + @users('admin') def test_digest_tone_down_wlogs(self): digest = self.env['digest.digest'].browse(self.test_digest.ids) @@ -137,12 +151,34 @@ class TestDigest(mail_test.MailCommon): self.assertEqual(digest.periodicity, 'daily') # logs for employee -> should not tone down - self.env['res.users.log'].with_user(SUPERUSER_ID).create({'create_uid': self.user_employee.id}) + logs = self.env['res.users.log'].with_user(SUPERUSER_ID).create({'create_uid': self.user_employee.id}) digest.flush() with self.mock_mail_gateway(): digest.action_send() - self.assertEqual(digest.periodicity, 'daily') + logs.unlink() + logs = self.env['res.users.log'].with_user(SUPERUSER_ID).create({ + 'create_uid': self.user_employee.id, + 'create_date': fields.Datetime.now() - relativedelta(days=20), + }) + + # logs for employee are more than 3 days old -> should tone down + digest.flush() + with self.mock_mail_gateway(): + digest.action_send() + self.assertEqual(digest.periodicity, 'weekly') + + # logs for employee are more than 2 weeks old -> should tone down + digest.flush() + with self.mock_mail_gateway(): + digest.action_send() + self.assertEqual(digest.periodicity, 'monthly') + + # logs for employee are less than 1 month old -> should not tone down + digest.flush() + with self.mock_mail_gateway(): + digest.action_send() + self.assertEqual(digest.periodicity, 'monthly') @tagged('-at_install', 'post_install')