Skip to content

Commit 279fbb5

Browse files
committed
feat: add site_id parameter to enrollment batch processing
1 parent 6432306 commit 279fbb5

4 files changed

Lines changed: 23 additions & 5 deletions

File tree

lms/djangoapps/instructor/views/api.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
from openedx.core.djangoapps.course_groups.models import CourseUserGroup
124124
from openedx.core.djangoapps.django_comment_common.models import CourseDiscussionSettings, Role
125125
from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers
126+
from openedx.core.djangoapps.theming.helpers import get_current_site
126127
from openedx.core.djangoapps.user_api.preferences.api import get_user_preference
127128
from openedx.core.djangolib.markup import HTML, Text
128129
from openedx.core.lib.api.authentication import BearerAuthenticationAllowInactiveUser
@@ -779,6 +780,9 @@ def _process_student_enrollment(self, request, course_id, data, secure): # pyli
779780

780781
course_key = CourseKey.from_string(course_id)
781782

783+
site = get_current_site()
784+
site_id = site.id if site else None
785+
782786
if async_processing:
783787

784788
try:
@@ -791,6 +795,7 @@ def _process_student_enrollment(self, request, course_id, data, secure): # pyli
791795
email_students=email_students,
792796
reason=reason,
793797
secure=secure,
798+
site_id=site_id,
794799
)
795800

796801
return {

lms/djangoapps/instructor_task/api.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,7 @@ def submit_student_enrollment_batch(
622622
email_students: bool,
623623
reason: str | None,
624624
secure: bool,
625+
site_id: int | None = None,
625626
):
626627
"""
627628
Request to have student enrollment operations processed as a background task.
@@ -638,6 +639,7 @@ def submit_student_enrollment_batch(
638639
email_students (bool): Whether to send enrollment emails
639640
reason (str | None): Optional reason for enrollment change
640641
secure (bool): Whether the request is secure (HTTPS)
642+
site_id (int | None): Optional site ID for notification emails
641643
642644
Returns:
643645
InstructorTask object representing the submitted background task
@@ -655,6 +657,7 @@ def submit_student_enrollment_batch(
655657
"email_students": email_students,
656658
"reason": reason,
657659
"secure": secure,
660+
"site_id": site_id,
658661
}
659662

660663
task_key_stub = f"{course_key}_{action}_{json.dumps(sorted(identifiers))}"

lms/djangoapps/instructor_task/notifications.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,23 @@
2727
TASK_LOG = logging.getLogger("edx.celery.task")
2828

2929

30-
def _get_current_site() -> Site | None:
30+
def _get_current_site(site_id: int | None = None) -> Site | None:
3131
"""
3232
Get the current Django Site instance with fallback logic.
3333
34+
Args:
35+
site_id (int | None): Optional site ID to retrieve. If provided, attempts
36+
to get this specific site first before falling back.
37+
3438
Returns:
3539
Site | None: The current Site object or None if unavailable
3640
"""
41+
if site_id:
42+
try:
43+
return Site.objects.get(id=site_id)
44+
except Site.DoesNotExist:
45+
pass
46+
3747
# Try to get current site if method exists
3848
if hasattr(Site.objects, "get_current"):
3949
site = Site.objects.get_current()
@@ -46,7 +56,6 @@ def _get_current_site() -> Site | None:
4656
except Site.DoesNotExist:
4757
pass
4858

49-
# Last resort: get first site
5059
try:
5160
return Site.objects.first()
5261
except Exception: # pylint: disable=broad-except
@@ -144,11 +153,12 @@ def send_enrollment_task_completion_email(
144153
- failed: Number of failed operations
145154
"""
146155
requester = instructor_task.requester
147-
site = _get_current_site()
148156
task_input = _parse_task_input(instructor_task)
157+
158+
site_id = task_input.get("site_id")
159+
site = _get_current_site(site_id)
149160
user_language = get_user_preference(requester, LANGUAGE_KEY)
150161

151-
# Build email context
152162
user_context = _build_enrollment_email_context(
153163
course_key=course_key,
154164
requester=requester,
@@ -158,7 +168,6 @@ def send_enrollment_task_completion_email(
158168
task_input=task_input,
159169
)
160170

161-
# Create and send message
162171
message = BatchEnrollment().personalize(
163172
recipient=Recipient(lms_user_id=requester.id, email_address=requester.email),
164173
language=user_language,

lms/djangoapps/instructor_task/tasks_helper/enrollments.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ def process_student_enrollment_batch(
148148
- email_students: boolean to send enrollment emails
149149
- reason: optional reason for enrollment change
150150
- secure: boolean indicating if request was secure (HTTPS)
151+
- site_id: optional site ID for notification emails
151152
action_name: Name of the action being performed
152153
153154
Returns:

0 commit comments

Comments
 (0)