From aa1b02ef38fb73a40891a99c48875fa7347e8195 Mon Sep 17 00:00:00 2001 From: faucomte97 Date: Thu, 26 Sep 2024 17:36:37 +0100 Subject: [PATCH] Add days left data binding to inactivity reminders --- src/api/views/user.py | 31 ++++++++++++++---- src/api/views/user_test.py | 66 ++++++++++++++++++++++++++++---------- 2 files changed, 73 insertions(+), 24 deletions(-) diff --git a/src/api/views/user.py b/src/api/views/user.py index 883c1101..d70ffb40 100644 --- a/src/api/views/user.py +++ b/src/api/views/user.py @@ -4,6 +4,7 @@ """ import logging +import typing as t from datetime import timedelta from codeforlife.mail import send_mail @@ -368,7 +369,12 @@ def _get_inactive_users(self, days: int): return user_queryset.exclude(email__isnull=True).exclude(email="") - def _send_inactivity_reminder(self, days: int, campaign_name: str): + def _send_inactivity_reminder( + self, + days: int, + campaign_name: str, + personalization_values: t.Optional[dict] = None, + ): user_queryset = self._get_inactive_users(days) user_count = user_queryset.count() @@ -380,12 +386,21 @@ def _send_inactivity_reminder(self, days: int, campaign_name: str): chunk_size=500 ): try: - send_mail( - campaign_id=settings.DOTDIGITAL_CAMPAIGN_IDS[ - campaign_name - ], - to_addresses=[email], - ) + if personalization_values is not None: + send_mail( + campaign_id=settings.DOTDIGITAL_CAMPAIGN_IDS[ + campaign_name + ], + to_addresses=[email], + personalization_values=personalization_values, + ) + else: + send_mail( + campaign_id=settings.DOTDIGITAL_CAMPAIGN_IDS[ + campaign_name + ], + to_addresses=[email], + ) sent_email_count += 1 # pylint: disable-next=broad-exception-caught @@ -417,6 +432,7 @@ def send_2nd_inactivity_reminder(self, request: Request): return self._send_inactivity_reminder( days=973, campaign_name="Inactive users on website - second reminder", + personalization_values={"DAYS_LEFT": 123}, ) @cron_job @@ -428,4 +444,5 @@ def send_final_inactivity_reminder(self, request: Request): return self._send_inactivity_reminder( days=1065, campaign_name="Inactive users on website - final reminder", + personalization_values={"DAYS_LEFT": 31}, ) diff --git a/src/api/views/user_test.py b/src/api/views/user_test.py index 2e2debd5..abdf096c 100644 --- a/src/api/views/user_test.py +++ b/src/api/views/user_test.py @@ -746,9 +746,17 @@ def anonymize_unverified_users( ) def _test_send_inactivity_reminder( - self, action: str, days: int, campaign_name: str + self, + action: str, + days: int, + campaign_name: str, + personalization_values: t.Optional[dict] = None, ): - def test_send_inactivity_reminder(days: int, mail_sent: bool): + def test_send_inactivity_reminder( + days: int, + mail_sent: bool, + personalization_values: t.Optional[dict] = None, + ): date_joined = timezone.now() - timedelta(days, hours=12) last_login = timezone.now() - timedelta(days, hours=12) @@ -766,25 +774,47 @@ def test_send_inactivity_reminder(days: int, mail_sent: bool): self.client.cron_job(action) if mail_sent: - send_mail_mock.assert_has_calls( - [ - call( - campaign_id=( - settings.DOTDIGITAL_CAMPAIGN_IDS[ - campaign_name - ] - ), - to_addresses=[user.email], - ) - for user in teacher_users + indy_users - ], - any_order=True, - ) + if personalization_values is not None: + send_mail_mock.assert_has_calls( + [ + call( + campaign_id=( + settings.DOTDIGITAL_CAMPAIGN_IDS[ + campaign_name + ] + ), + to_addresses=[user.email], + # pylint: disable-next=line-too-long + personalization_values=personalization_values, + ) + for user in teacher_users + indy_users + ], + any_order=True, + ) + else: + send_mail_mock.assert_has_calls( + [ + call( + campaign_id=( + settings.DOTDIGITAL_CAMPAIGN_IDS[ + campaign_name + ] + ), + to_addresses=[user.email], + ) + for user in teacher_users + indy_users + ], + any_order=True, + ) else: send_mail_mock.assert_not_called() test_send_inactivity_reminder(days=days - 1, mail_sent=False) - test_send_inactivity_reminder(days=days, mail_sent=True) + test_send_inactivity_reminder( + days=days, + mail_sent=True, + personalization_values=personalization_values, + ) test_send_inactivity_reminder(days=days + 1, mail_sent=False) def test_send_1st_inactivity_reminder(self): @@ -801,6 +831,7 @@ def test_send_2nd_inactivity_reminder(self): action="send_2nd_inactivity_reminder", days=973, campaign_name="Inactive users on website - second reminder", + personalization_values={"DAYS_LEFT": 123}, ) def test_send_final_inactivity_reminder(self): @@ -809,6 +840,7 @@ def test_send_final_inactivity_reminder(self): action="send_final_inactivity_reminder", days=1065, campaign_name="Inactive users on website - final reminder", + personalization_values={"DAYS_LEFT": 31}, ) # test: other actions