Skip to content

Commit

Permalink
feat: Create cron job endpoints for incoming anonymisation warnings (#…
Browse files Browse the repository at this point in the history
…2333)

* feat: Create cron job endpoints for incoming anonymisation warnings

* Remove print

* Feedback

* Rename third to final

* Improve query operations

* Merge conflicts
  • Loading branch information
faucomte97 committed Aug 6, 2024
1 parent fedda1d commit 97344d5
Show file tree
Hide file tree
Showing 4 changed files with 305 additions and 21 deletions.
37 changes: 31 additions & 6 deletions cfl_common/common/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
"verify_new_user_second_reminder": 1557173,
"verify_new_user_via_parent": 1551587,
"verify_released_student": 1580574,
"inactive_users_on_website_first_reminder": 1604381,
"inactive_users_on_website_second_reminder": 1606208,
"inactive_users_on_website_final_reminder": 1606215,
}


Expand Down Expand Up @@ -65,7 +68,11 @@ def django_send_email(
plaintext = loader.get_template(plaintext_template)
html = loader.get_template(html_template)
plaintext_email_context = {"content": text_content}
html_email_context = {"content": text_content, "title": title, "url_prefix": domain()}
html_email_context = {
"content": text_content,
"title": title,
"url_prefix": domain(),
}

# render templates
plaintext_body = plaintext.render(plaintext_email_context)
Expand All @@ -74,11 +81,19 @@ def django_send_email(

if replace_url:
verify_url = replace_url["verify_url"]
verify_replace_url = re.sub(f"(.*/verify_email/)(.*)", f"\\1", verify_url)
html_body = re.sub(f"({verify_url})(.*){verify_url}", f"\\1\\2{verify_replace_url}", original_html_body)
verify_replace_url = re.sub(
f"(.*/verify_email/)(.*)", f"\\1", verify_url
)
html_body = re.sub(
f"({verify_url})(.*){verify_url}",
f"\\1\\2{verify_replace_url}",
original_html_body,
)

# make message using templates
message = EmailMultiAlternatives(subject, plaintext_body, sender, recipients)
message = EmailMultiAlternatives(
subject, plaintext_body, sender, recipients
)
message.attach_alternative(html_body, "text/html")

message.send()
Expand Down Expand Up @@ -123,7 +138,13 @@ def send_dotdigital_email(

# Dotdigital emails don't work locally, so if testing emails locally use Django to send a dummy email instead
if MODULE_NAME == "local":
django_send_email(from_address, to_addresses, "dummy_subject", "dummy_text_content", "dummy_title")
django_send_email(
from_address,
to_addresses,
"dummy_subject",
"dummy_text_content",
"dummy_title",
)
else:
if auth is None:
auth = app_settings.DOTDIGITAL_AUTH
Expand Down Expand Up @@ -168,4 +189,8 @@ def send_dotdigital_email(
timeout=timeout,
)

assert response.ok, "Failed to send email." f" Reason: {response.reason}." f" Text: {response.text}."
assert response.ok, (
"Failed to send email."
f" Reason: {response.reason}."
f" Text: {response.text}."
)
101 changes: 101 additions & 0 deletions portal/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
UserProfile,
UserSession,
)
from common.mail import campaign_ids
from common.tests.utils.classes import create_class_directly
from common.tests.utils.organisation import (
create_organisation_directly,
Expand Down Expand Up @@ -1197,3 +1198,103 @@ def anonymise_unverified_users(
is_verified=False,
assert_active=False,
)

@patch("portal.views.cron.user.send_dotdigital_email")
def send_inactivity_reminder(
self,
days: int,
view_name: str,
assert_called: bool,
campaign_name: str,
mock_send_dotdigital_email: Mock,
):
self.teacher_user.date_joined = timezone.now() - timedelta(
days=days, hours=12
)
self.teacher_user.save()
self.student_user.date_joined = timezone.now() - timedelta(
days=days, hours=12
)
self.student_user.save()
self.indy_user.last_login = timezone.now() - timedelta(
days=days, hours=12
)
self.indy_user.save()

self.client.get(reverse(view_name))

if assert_called:
mock_send_dotdigital_email.assert_any_call(
campaign_ids[campaign_name], [self.teacher_user.email]
)

mock_send_dotdigital_email.assert_any_call(
campaign_ids[campaign_name], [self.indy_user.email]
)

# Check only two emails are sent - the student should never be included.
assert mock_send_dotdigital_email.call_count == 2
else:
mock_send_dotdigital_email.assert_not_called()

mock_send_dotdigital_email.reset_mock()

def test_first_inactivity_reminder_view(self):
self.send_inactivity_reminder(
729,
"first-inactivity-reminder",
False,
"inactive_users_on_website_first_reminder",
)
self.send_inactivity_reminder(
730,
"first-inactivity-reminder",
True,
"inactive_users_on_website_first_reminder",
)
self.send_inactivity_reminder(
731,
"first-inactivity-reminder",
False,
"inactive_users_on_website_first_reminder",
)

def test_second_inactivity_reminder_view(self):
self.send_inactivity_reminder(
972,
"second-inactivity-reminder",
False,
"inactive_users_on_website_second_reminder",
)
self.send_inactivity_reminder(
973,
"second-inactivity-reminder",
True,
"inactive_users_on_website_second_reminder",
)
self.send_inactivity_reminder(
974,
"second-inactivity-reminder",
False,
"inactive_users_on_website_second_reminder",
)

def test_final_inactivity_reminder_view(self):
self.send_inactivity_reminder(
1064,
"final-inactivity-reminder",
False,
"inactive_users_on_website_final_reminder",
)
self.send_inactivity_reminder(
1065,
"final-inactivity-reminder",
True,
"inactive_users_on_website_final_reminder",
)
self.send_inactivity_reminder(
1066,
"final-inactivity-reminder",
False,
"inactive_users_on_website_final_reminder",
)
15 changes: 15 additions & 0 deletions portal/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,21 @@
cron.user.AnonymiseUnverifiedAccounts.as_view(),
name="anonymise-unverified-accounts",
),
path(
"inactive/send-first-reminder/",
cron.user.FirstInactivityReminderView.as_view(),
name="first-inactivity-reminder",
),
path(
"inactive/send-second-reminder/",
cron.user.SecondInactivityReminderView.as_view(),
name="second-inactivity-reminder",
),
path(
"inactive/send-final-reminder/",
cron.user.FinalInactivityReminderView.as_view(),
name="final-inactivity-reminder",
),
]
),
),
Expand Down
Loading

0 comments on commit 97344d5

Please sign in to comment.