diff --git a/enterprise/api/v1/views/enterprise_customer.py b/enterprise/api/v1/views/enterprise_customer.py index 1685304d17..3ce2a692ca 100644 --- a/enterprise/api/v1/views/enterprise_customer.py +++ b/enterprise/api/v1/views/enterprise_customer.py @@ -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, ) @@ -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: @@ -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: diff --git a/enterprise/utils.py b/enterprise/utils.py index 32770d8b52..99debbc724 100644 --- a/enterprise/utils.py +++ b/enterprise/utils.py @@ -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 @@ -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') @@ -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, @@ -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.