Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
import datetime
import traceback

from django.core.management.base import BaseCommand

Expand All @@ -13,6 +13,8 @@
send_allocation_customer_email,
)

logger = logging.getLogger(__name__)

GENERAL_RESOURCE_NAME = import_from_settings("GENERAL_RESOURCE_NAME")
CENTER_BASE_URL = import_from_settings("CENTER_BASE_URL")

Expand All @@ -23,13 +25,11 @@ class Command(BaseCommand):

def handle(self, *args, **options):
try:
allocations = Allocation.objects.filter(
resources__name=GENERAL_RESOURCE_NAME, status__name="New"
)
allocations = Allocation.objects.filter(resources__name=GENERAL_RESOURCE_NAME, status__name="New")
active = AllocationStatusChoice.objects.get(name="Active")

for allocation_obj in allocations:
print(f"Approving allocation number {allocation_obj.pk}")
logger.info(f"Approving allocation number {allocation_obj.pk}")
allocation_obj.status = active
if not allocation_obj.start_date:
allocation_obj.start_date = datetime.datetime.now()
Expand All @@ -43,17 +43,14 @@ def handle(self, *args, **options):
status__name__in=["Removed", "Error", "DeclinedEULA", "PendingEULA"]
)
for allocation_user in allocation_users:
allocation_activate_user.send(
sender=None, allocation_user_pk=allocation_user.pk
)
allocation_activate_user.send(sender=None, allocation_user_pk=allocation_user.pk)

send_allocation_customer_email(
allocation_obj,
"Allocation Activated",
"email/allocation_activated.txt",
domain_url=CENTER_BASE_URL,
)
print(f"Approved allocation request: {allocation_obj.pk}")
except Exception as e:
print("Exception occured with traceback:")
traceback.print_exception(e)
logger.info(f"Approved allocation request: {allocation_obj.pk}")
except Exception:
logger.debug("Exception occured with traceback:", exc_info=True)
40 changes: 10 additions & 30 deletions coldfront/core/utils/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ def send_email(subject, body, sender, receiver_list, cc=[]):
)


def send_email_template(
subject, template_name, template_context, sender, receiver_list
):
def send_email_template(subject, template_name, template_context, sender, receiver_list):
"""Helper function for sending emails from a template"""
if not EMAIL_ENABLED:
return
Expand All @@ -89,23 +87,17 @@ def build_link(url_path, domain_url=""):
return f"{domain_url}{url_path}"


def send_admin_email_template(
subject, template_name, template_context, receiver_list=None
):
def send_admin_email_template(subject, template_name, template_context, receiver_list=None):
"""Helper function for sending admin emails using a template"""
if receiver_list == None:
receiver_list = [
EMAIL_TICKET_SYSTEM_ADDRESS,
]
logger.info(f"Sending admin email to {receiver_list}")
send_email_template(
subject, template_name, template_context, EMAIL_SENDER, receiver_list
)
send_email_template(subject, template_name, template_context, EMAIL_SENDER, receiver_list)


def send_allocation_admin_email(
allocation_obj, subject, template_name, url_path="", domain_url=""
):
def send_allocation_admin_email(allocation_obj, subject, template_name, url_path="", domain_url=""):
"""Send allocation admin emails to approvers whose schools match the allocation's project school."""
if not url_path:
url_path = reverse("allocation-request-list")
Expand All @@ -122,9 +114,7 @@ def send_allocation_admin_email(
ctx["url"] = url

# Get all approvers whose schools include this project's school
approvers = User.objects.filter(
userprofile__approver_profile__schools=project_school, is_active=True
).distinct()
approvers = User.objects.filter(userprofile__approver_profile__schools=project_school, is_active=True).distinct()

# Extract valid email addresses
recipient_list = [approver.email for approver in approvers if approver.email]
Expand All @@ -146,18 +136,12 @@ def send_allocation_admin_email(
ctx,
receiver_list=recipient_list, # Send only to matched approvers
)
logger.info(
f"Sent admin allocation email for request to access {resource_name} to approvers: {recipient_list}"
)
logger.info(f"Sent admin allocation email for request to access {resource_name} to approvers: {recipient_list}")
else:
logger.warning(
f'No approvers found for school "{project_school}" to send allocation email.'
)
logger.warning(f'No approvers found for school "{project_school}" to send allocation email.')


def send_allocation_customer_email(
allocation_obj, subject, template_name, url_path="", domain_url=""
):
def send_allocation_customer_email(allocation_obj, subject, template_name, url_path="", domain_url=""):
"""Send allocation customer emails"""
if not url_path:
url_path = reverse("allocation-detail", kwargs={"pk": allocation_obj.pk})
Expand All @@ -167,14 +151,10 @@ def send_allocation_customer_email(
ctx["resource"] = allocation_obj.get_parent_resource
ctx["url"] = url

allocation_users = allocation_obj.allocationuser_set.exclude(
status__name__in=["Removed", "Error"]
)
allocation_users = allocation_obj.allocationuser_set.exclude(status__name__in=["Removed", "Error"])
email_receiver_list = []
for allocation_user in allocation_users:
if allocation_user.allocation.project.projectuser_set.get(
user=allocation_user.user
).enable_notifications:
if allocation_user.allocation.project.projectuser_set.get(user=allocation_user.user).enable_notifications:
email_receiver_list.append(allocation_user.user.email)

send_email_template(subject, template_name, ctx, EMAIL_SENDER, email_receiver_list)
Loading