forked from HHS/TANF-app
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into sprint-92-summary
- Loading branch information
Showing
8 changed files
with
263 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
"""Shared celery email tasks for beat.""" | ||
|
||
from __future__ import absolute_import | ||
from tdpservice.users.models import User, AccountApprovalStatusChoices | ||
from django.contrib.auth.models import Group | ||
from django.conf import settings | ||
from django.urls import reverse | ||
from django.utils import timezone | ||
from celery import shared_task | ||
from datetime import datetime, timedelta | ||
import logging | ||
from tdpservice.email.helpers.account_access_requests import send_num_access_requests_email | ||
from tdpservice.email.helpers.account_deactivation_warning import send_deactivation_warning_email | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@shared_task | ||
def check_for_accounts_needing_deactivation_warning(): | ||
"""Check for accounts that need deactivation warning emails.""" | ||
deactivate_in_10_days = users_to_deactivate(10) | ||
deactivate_in_3_days = users_to_deactivate(3) | ||
deactivate_in_1_day = users_to_deactivate(1) | ||
|
||
if deactivate_in_10_days: | ||
send_deactivation_warning_email(deactivate_in_10_days, 10) | ||
if deactivate_in_3_days: | ||
send_deactivation_warning_email(deactivate_in_3_days, 3) | ||
if deactivate_in_1_day: | ||
send_deactivation_warning_email(deactivate_in_1_day, 1) | ||
|
||
def users_to_deactivate(days): | ||
"""Return a list of users that have not logged in in the last {180 - days} days.""" | ||
days = 180 - days | ||
return User.objects.filter( | ||
last_login__lte=datetime.now(tz=timezone.utc) - timedelta(days=days), | ||
last_login__gte=datetime.now(tz=timezone.utc) - timedelta(days=days+1), | ||
account_approval_status=AccountApprovalStatusChoices.APPROVED, | ||
) | ||
|
||
def get_ofa_admin_user_emails(): | ||
"""Return a list of OFA System Admin and OFA Admin users.""" | ||
return User.objects.filter( | ||
groups__in=Group.objects.filter(name__in=('OFA Admin', 'OFA System Admin')) | ||
).values_list('email', flat=True).distinct() | ||
|
||
def get_num_access_requests(): | ||
"""Return the number of users requesting access.""" | ||
return User.objects.filter( | ||
account_approval_status=AccountApprovalStatusChoices.ACCESS_REQUEST, | ||
).count() | ||
|
||
@shared_task | ||
def email_admin_num_access_requests(): | ||
"""Send all OFA System Admins an email with how many users have requested access.""" | ||
recipient_email = get_ofa_admin_user_emails() | ||
text_message = '' | ||
subject = 'Number of Active Access Requests' | ||
url = f'{settings.FRONTEND_BASE_URL}{reverse("admin:users_user_changelist")}?o=-2' | ||
email_context = { | ||
'date': datetime.today(), | ||
'num_requests': get_num_access_requests(), | ||
'admin_user_pg': url, | ||
} | ||
|
||
send_num_access_requests_email(recipient_email, | ||
text_message, | ||
subject, | ||
email_context, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.