Skip to content
Draft
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
86 changes: 83 additions & 3 deletions common/djangoapps/student/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Celery task for course enrollment email
"""
import logging
from datetime import datetime

from celery import shared_task
from django.conf import settings
from django.contrib.auth import get_user_model
Expand Down Expand Up @@ -29,6 +31,76 @@
MAX_RETRIES = 3


def _build_enrollment_email_image_urls(language='en'):
"""
Build absolute URLs for enrollment email images.

This function constructs full image URLs for SES email delivery. When Braze renders
the email through SES, it needs absolute URLs because email clients cannot resolve
relative paths or Django static file paths.

Args:
language (str): Language code ('en' or 'es') to select correct image variants

Returns:
dict: Mapping of image variable names to absolute URLs

How it works:
1. settings.LMS_ROOT_URL is evaluated at runtime (e.g., 'https://courses.edx.org')
2. f-string interpolates this value into the path
3. Email client receives complete URL: 'https://courses.edx.org/static/images/enrollment_email/...'
4. Browser/email client can fetch image directly without context about Django

Example flow:
- Python (settings): LMS_ROOT_URL = 'https://courses.edx.org'
- f-string: f"{settings.LMS_ROOT_URL}/static/images/enrollment_email/person_icon_{language}.png"
- Result: 'https://courses.edx.org/static/images/enrollment_email/person_icon_en.png'
- Email client: Makes HTTP GET to that URL, renders image
"""
lms_root = configuration_helpers.get_value(
"LMS_ROOT_URL", settings.LMS_ROOT_URL
).rstrip('/')

# NOTE: Image URLs for SES templates.
# Currently not used in Braze flow — will be enabled during SES migration.
# Construct image URLs based on language
image_urls = {
'logo_url': f"{lms_root}/static/images/edx_logo.png",
'you_are_enrolled_en': f"{lms_root}/static/images/enrollment_email/you_are_enrolled_en.png",
'banner_default': f"{lms_root}/static/images/enrollment_email/banner_default.png",
'timer_icon_en': f"{lms_root}/static/images/enrollment_email/timer_icon_en.png",
'person_icon_en': f"{lms_root}/static/images/enrollment_email/person_icon_en.png",
'dollar_icon_en': f"{lms_root}/static/images/enrollment_email/dollar_icon_en.png",
'goal_idea_icon_en': f"{lms_root}/static/images/enrollment_email/goal_idea_icon_en.png",
'flag_icon_pink_en': f"{lms_root}/static/images/enrollment_email/flag_icon_pink_en.png",
'flag_icon_black_en_es': f"{lms_root}/static/images/enrollment_email/flag_icon_black_en_es.png",
'flag_icon_orange_en': f"{lms_root}/static/images/enrollment_email/flag_icon_orange_en.png",
'vertical_line_white_en': f"{lms_root}/static/images/enrollment_email/vertical_line_white_en.png",
'vertical_line_orange_en': f"{lms_root}/static/images/enrollment_email/vertical_line_orange_en.png",
'vertical_line_black_en': f"{lms_root}/static/images/enrollment_email/vertical_line_black_en.png",
'community_illustration_en': f"{lms_root}/static/images/enrollment_email/community_illustration_en.png",
}

# If Spanish, override with Spanish variants
if language == 'es':
spanish_overrides = {
'banner_default': f"{lms_root}/static/images/enrollment_email/banner_default.png", # Same for both languages
'arrow_icon_es': f"{lms_root}/static/images/enrollment_email/arrow_icon_es.png",
'timer_icon_es': f"{lms_root}/static/images/enrollment_email/timer_icon_es.png",
'person_icon_es': f"{lms_root}/static/images/enrollment_email/person_icon_es.png",
'dollar_icon_es': f"{lms_root}/static/images/enrollment_email/dollar_icon_es.png",
'flag_icon_white_es': f"{lms_root}/static/images/enrollment_email/flag_icon_white_es.png",
'flag_icon_grey_es': f"{lms_root}/static/images/enrollment_email/flag_icon_grey_es.png",
'flag_icon_black_en_es': f"{lms_root}/static/images/enrollment_email/flag_icon_black_en_es.png", # Same for both
'calendar_icon_es': f"{lms_root}/static/images/enrollment_email/calendar_icon_es.png",
'community_icon_es': f"{lms_root}/static/images/enrollment_email/community_icon_es.png",
'slant_line_es': f"{lms_root}/static/images/enrollment_email/slant_line_es.png",
}
image_urls.update(spanish_overrides)

return image_urls


@shared_task(bind=True, ignore_result=True)
@set_code_owner_attribute
def send_course_enrollment_email(
Expand Down Expand Up @@ -64,13 +136,16 @@ def send_course_enrollment_email(
"course_price": CourseMode.min_course_price_for_currency(
course_id=course_id, currency="USD"
),
# Strip trailing slashes so template concatenation like
# "{{ lms_base_url }}/courses/..." never produces a double slash.
"lms_base_url": configuration_helpers.get_value(
"LMS_ROOT_URL", settings.LMS_ROOT_URL
),
).rstrip('/'),
"learning_base_url": configuration_helpers.get_value(
"LEARNING_MICROFRONTEND_URL", settings.LEARNING_MICROFRONTEND_URL
),
"track_mode": track_mode
).rstrip('/'),
"track_mode": track_mode,
"current_year": datetime.now().year,
}

try:
Expand All @@ -86,9 +161,14 @@ def send_course_enrollment_email(
{
"course_date_blocks": course_date_blocks,
"goals_enabled": ENABLE_COURSE_GOALS.is_enabled(course_key),
"user_name": user.get_full_name() or user.first_name or user.username,
}
)

# Inject absolute image URLs for SES rendering. Braze ignores these extra
# keys; they're consumed only when the SES path is enabled.
canvas_entry_properties.update(_build_enrollment_email_image_urls(language='en'))

try:
course_uuid = get_course_uuid_for_course(course_id)
if course_uuid is None:
Expand Down
Loading
Loading