Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add days left data binding to inactivity reminders #365

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions src/api/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""

import logging
import typing as t
from datetime import timedelta

from codeforlife.mail import send_mail
Expand Down Expand Up @@ -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()

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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},
)
66 changes: 49 additions & 17 deletions src/api/views/user_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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
Expand Down