Skip to content

Commit

Permalink
fix: respect allow_enrollment_in_invite_only_courses
Browse files Browse the repository at this point in the history
Fixes `enroll_learners_in_courses` endpoint (and
`enroll_subsidy_users_in_courses` utility function) to respect
enterprise customer's `allow_enrollment_in_invite_only_courses` flag.
  • Loading branch information
0x29a committed Mar 1, 2024
1 parent 4eadf9d commit 75ea83c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions enterprise/api/v1/views/enterprise_customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from enterprise.utils import (
enroll_subsidy_users_in_courses,
get_best_mode_from_course_key,
get_course_details_from_course_keys,
track_enrollment,
validate_email_to_link,
)
Expand Down Expand Up @@ -241,6 +242,8 @@ def enroll_learners_in_courses(self, request, pk):
for course_run in course_runs_modes:
course_runs_modes[course_run] = get_best_mode_from_course_key(course_run)

course_details = get_course_details_from_course_keys(course_runs_modes.keys())

emails = set()

for info in enrollments_info:
Expand All @@ -254,6 +257,7 @@ def enroll_learners_in_courses(self, request, pk):
else:
emails.add(info['email'])
info['course_mode'] = course_runs_modes[info['course_run_key']]
info['invitation_only'] = course_details[info['course_run_key']].invitation_only

for email in emails:
try:
Expand Down
20 changes: 20 additions & 0 deletions enterprise/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@
CourseUserGroup = None
CourseEnrollmentError = None

try:
from openedx.core.djangoapps.content.course_overviews.models import CourseOverview
except ImportError:
CourseOverview = None

try:
from common.djangoapps.course_modes.models import CourseMode
from common.djangoapps.student.models import CourseEnrollmentAllowed
Expand Down Expand Up @@ -2028,6 +2033,7 @@ def enroll_subsidy_users_in_courses(enterprise_customer, subsidy_users_info, dis
user_id = subsidy_user_info.get('user_id')
user_email = subsidy_user_info['email'].strip().lower() if 'email' in subsidy_user_info else None
course_mode = subsidy_user_info.get('course_mode')
invitation_only = subsidy_user_info.get('invitation_only')
course_run_key = subsidy_user_info.get('course_run_key')
license_uuid = subsidy_user_info.get('license_uuid')
transaction_id = subsidy_user_info.get('transaction_id')
Expand All @@ -2054,6 +2060,12 @@ def enroll_subsidy_users_in_courses(enterprise_customer, subsidy_users_info, dis
enrollment_source = enterprise_enrollment_source_model().get_source(
enterprise_enrollment_source_model().CUSTOMER_ADMIN
)
if invitation_only and enterprise_customer.allow_enrollment_in_invite_only_courses:
CourseEnrollmentAllowed.objects.update_or_create(
course_id=course_run_key,
email=user.email,
)

succeeded, created, source_uuid = customer_admin_enroll_user_with_status(
enterprise_customer,
user,
Expand Down Expand Up @@ -2291,6 +2303,14 @@ def get_best_mode_from_course_key(course_key):
return CourseModes.AUDIT


def get_course_details_from_course_keys(course_keys):
"""
Helper to get a mapping of course keys to course details.
"""
course_overviews = CourseOverview.objects.filter(id__in=course_keys)
return {str(course_overview.id): course_overview for course_overview in course_overviews}


def parse_lms_api_datetime(datetime_string, datetime_format=LMS_API_DATETIME_FORMAT):
"""
Parse a received datetime into a timezone-aware, Python datetime object.
Expand Down

0 comments on commit 75ea83c

Please sign in to comment.