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

Create cron job endpoints for anonymisation warnings #358

Merged
merged 9 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
"Email change notification": 1551600,
"Verify changed user email": 1551594,
"Account deletion": 1567477,
"Inactive users on website - first reminder": 1604381,
"Inactive users on website - second reminder": 1606208,
"Inactive users on website - final reminder": 1606215,
}

# Build paths inside the project like this: BASE_DIR / 'subdir'.
Expand Down
118 changes: 117 additions & 1 deletion src/api/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from codeforlife.user.views import UserViewSet as _UserViewSet
from codeforlife.views import action, cron_job
from django.conf import settings
from django.db.models import F
from django.db.models import F, Q
from django.urls import reverse
from django.utils import timezone
from rest_framework import status
Expand Down Expand Up @@ -64,6 +64,9 @@
"send_1st_verify_email_reminder",
"send_2nd_verify_email_reminder",
"anonymize_unverified_accounts",
"send_1st_inactivity_reminder",
"send_2nd_inactivity_reminder",
"send_final_inactivity_reminder",
]:
return [IsCronRequestFromGoogle()]

Expand Down Expand Up @@ -340,3 +343,116 @@
)

return Response()

def _get_inactive_users(self, days: int, same_day: bool):
now = timezone.now()

Check warning on line 348 in src/api/views/user.py

View check run for this annotation

Codecov / codecov/patch

src/api/views/user.py#L348

Added line #L348 was not covered by tests

# All users who haven't logged in in X days OR who've never logged in
# and registered over X days ago.
user_queryset = User.objects.filter(

Check warning on line 352 in src/api/views/user.py

View check run for this annotation

Codecov / codecov/patch

src/api/views/user.py#L352

Added line #L352 was not covered by tests
Q(
last_login__isnull=False,
last_login__lte=now - timedelta(days=days),
)
| Q(
last_login__isnull=True,
date_joined__lte=now - timedelta(days=days),
)
)

if same_day:
user_queryset = user_queryset.filter(

Check warning on line 364 in src/api/views/user.py

View check run for this annotation

Codecov / codecov/patch

src/api/views/user.py#L363-L364

Added lines #L363 - L364 were not covered by tests
Q(
last_login__isnull=False,
last_login__gt=now - timedelta(days=days + 1),
)
| Q(
last_login__isnull=True,
date_joined__gt=now - timedelta(days=days + 1),
)
)

teacher_queryset = user_queryset.filter(

Check warning on line 375 in src/api/views/user.py

View check run for this annotation

Codecov / codecov/patch

src/api/views/user.py#L375

Added line #L375 was not covered by tests
new_teacher__isnull=False,
new_student__isnull=True,
)
independent_student_queryset = user_queryset.filter(

Check warning on line 379 in src/api/views/user.py

View check run for this annotation

Codecov / codecov/patch

src/api/views/user.py#L379

Added line #L379 was not covered by tests
new_teacher__isnull=True,
new_student__class_field__isnull=True,
)

return teacher_queryset, independent_student_queryset

Check warning on line 384 in src/api/views/user.py

View check run for this annotation

Codecov / codecov/patch

src/api/views/user.py#L384

Added line #L384 was not covered by tests

def _send_inactivity_reminder(self, days: int, campaign_name: str):
(

Check warning on line 387 in src/api/views/user.py

View check run for this annotation

Codecov / codecov/patch

src/api/views/user.py#L387

Added line #L387 was not covered by tests
teacher_queryset,
independent_student_queryset,
) = self._get_inactive_users(
days,
same_day=True,
)
user_queryset = teacher_queryset.union(independent_student_queryset)

Check warning on line 394 in src/api/views/user.py

View check run for this annotation

Codecov / codecov/patch

src/api/views/user.py#L394

Added line #L394 was not covered by tests

user_count = user_queryset.count()

Check warning on line 396 in src/api/views/user.py

View check run for this annotation

Codecov / codecov/patch

src/api/views/user.py#L396

Added line #L396 was not covered by tests

logging.info(

Check warning on line 398 in src/api/views/user.py

View check run for this annotation

Codecov / codecov/patch

src/api/views/user.py#L398

Added line #L398 was not covered by tests
"%d inactive users after %d days.",
user_count - user_queryset.count(),
days,
)

if user_count > 0:
sent_email_count = 0
for email in user_queryset.values_list("email", flat=True).iterator(

Check warning on line 406 in src/api/views/user.py

View check run for this annotation

Codecov / codecov/patch

src/api/views/user.py#L404-L406

Added lines #L404 - L406 were not covered by tests
chunk_size=500
):
try:
send_mail(

Check warning on line 410 in src/api/views/user.py

View check run for this annotation

Codecov / codecov/patch

src/api/views/user.py#L409-L410

Added lines #L409 - L410 were not covered by tests
campaign_id=settings.DOTDIGITAL_CAMPAIGN_IDS[
campaign_name
],
to_addresses=[email],
)

sent_email_count += 1

Check warning on line 417 in src/api/views/user.py

View check run for this annotation

Codecov / codecov/patch

src/api/views/user.py#L417

Added line #L417 was not covered by tests
# pylint: disable-next=broad-exception-caught
except Exception as ex:
logging.exception(ex)

Check warning on line 420 in src/api/views/user.py

View check run for this annotation

Codecov / codecov/patch

src/api/views/user.py#L419-L420

Added lines #L419 - L420 were not covered by tests

logging.info(

Check warning on line 422 in src/api/views/user.py

View check run for this annotation

Codecov / codecov/patch

src/api/views/user.py#L422

Added line #L422 was not covered by tests
"Reminded %d/%d inactive users.", sent_email_count, user_count
)

return Response()

Check warning on line 426 in src/api/views/user.py

View check run for this annotation

Codecov / codecov/patch

src/api/views/user.py#L426

Added line #L426 was not covered by tests

@cron_job
def send_1st_inactivity_reminder(self, request: Request):
"""
Send the first reminder email to teachers and independent users who
haven't been active in a while.
"""
return self._send_verify_email_reminder(

Check warning on line 434 in src/api/views/user.py

View check run for this annotation

Codecov / codecov/patch

src/api/views/user.py#L434

Added line #L434 was not covered by tests
days=730, campaign_name="Inactive users on website - first reminder"
)

@cron_job
def send_2nd_inactivity_reminder(self, request: Request):
"""
Send the second reminder email to teachers and independent users who
haven't been active in a while.
"""
return self._send_verify_email_reminder(

Check warning on line 444 in src/api/views/user.py

View check run for this annotation

Codecov / codecov/patch

src/api/views/user.py#L444

Added line #L444 was not covered by tests
days=973,
campaign_name="Inactive users on website - second reminder",
)

@cron_job
def send_final_inactivity_reminder(self, request: Request):
"""
Send the final reminder email to teachers and independent users who
haven't been active in a while.
"""
return self._send_verify_email_reminder(

Check warning on line 455 in src/api/views/user.py

View check run for this annotation

Codecov / codecov/patch

src/api/views/user.py#L455

Added line #L455 was not covered by tests
days=1065,
campaign_name="Inactive users on website - final reminder",
)
68 changes: 68 additions & 0 deletions src/api/views/user_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,27 @@ def test_get_permissions__anonymize_unverified_accounts(self):
action="anonymize_unverified_accounts",
)

def test_get_permissions__send_1st_inactivity_reminder(self):
"""Only Google can send the 1st inactivity reminder."""
self.assert_get_permissions(
permissions=[IsCronRequestFromGoogle()],
action="send_1st_inactivity_reminder",
)

def test_get_permissions__send_2nd_inactivity_reminder(self):
"""Only Google can send the 2nd inactivity reminder."""
self.assert_get_permissions(
permissions=[IsCronRequestFromGoogle()],
action="send_2nd_inactivity_reminder",
)

def test_get_permissions__send_final_inactivity_reminder(self):
"""Only Google can send the final inactivity reminder."""
self.assert_get_permissions(
permissions=[IsCronRequestFromGoogle()],
action="send_final_inactivity_reminder",
)

def test_get_permissions__register_to_newsletter(self):
"""Any one can register to our newsletter."""
self.assert_get_permissions(
Expand Down Expand Up @@ -723,6 +744,53 @@ def anonymize_unverified_users(
is_anonymized=True,
)

def _test_send_inactivity_reminder(
self, action: str, days: int, campaign_name: str
):
def test_send_inactivity_reminder(days: int, mail_sent: bool):
date_joined = timezone.now() - timedelta(days, hours=12)
last_login = timezone.now() - timedelta(days, hours=12)

assert StudentUser.objects.update(date_joined=date_joined)

teacher_users = list(TeacherUser.objects.all())
assert teacher_users
indy_users = list(IndependentUser.objects.all())
assert indy_users

for teacher_user in teacher_users:
teacher_user.date_joined = date_joined
teacher_user.save()

for indy_user in indy_users:
indy_user.last_login = last_login
indy_user.save()

with patch("src.api.views.user.send_mail") as send_mail_mock:
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,
)
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 + 1, mail_sent=False)

# test: other actions

def test_register_to_newsletter(self):
Expand Down