|
69 | 69 | from main import features |
70 | 70 | from openedx.api import ( |
71 | 71 | create_edx_course_mode, |
| 72 | + create_user, |
72 | 73 | enroll_in_edx_course_runs, |
73 | 74 | get_edx_api_course_detail_client, |
74 | 75 | get_edx_api_course_list_client, |
@@ -133,12 +134,33 @@ def get_user_relevant_program_course_run_qset( |
133 | 134 | return enrollable_run_qset.order_by("enrollment_start") |
134 | 135 |
|
135 | 136 |
|
136 | | -def create_run_enrollments( # noqa: C901 |
| 137 | +def _enroll_learner_into_associated_programs(run, user): |
| 138 | + """ |
| 139 | + Enrolls the learner into all programs for which the course they are enrolling into |
| 140 | + is associated as a requirement or elective. If a program enrollment already exists |
| 141 | + then the change_status of that program_enrollment is checked to ensure it equals None. |
| 142 | + """ |
| 143 | + for program in run.course.programs: |
| 144 | + if not program.live: |
| 145 | + continue |
| 146 | + program_enrollment, _ = ProgramEnrollment.objects.get_or_create( |
| 147 | + user=user, |
| 148 | + program=program, |
| 149 | + defaults=dict( # noqa: C408 |
| 150 | + change_status=None, |
| 151 | + ), |
| 152 | + ) |
| 153 | + if program_enrollment.change_status is not None: |
| 154 | + program_enrollment.reactivate_and_save() |
| 155 | + |
| 156 | + |
| 157 | +def create_run_enrollments( # noqa: C901,PLR0913 |
137 | 158 | user, |
138 | 159 | runs, |
139 | 160 | *, |
140 | 161 | change_status=None, |
141 | 162 | keep_failed_enrollments=None, |
| 163 | + create_courseware_user=False, |
142 | 164 | mode=EDX_DEFAULT_ENROLLMENT_MODE, |
143 | 165 | ): |
144 | 166 | """ |
@@ -170,30 +192,14 @@ def create_run_enrollments( # noqa: C901 |
170 | 192 | features.IGNORE_EDX_FAILURES, False |
171 | 193 | ) |
172 | 194 |
|
173 | | - successful_enrollments = [] |
| 195 | + if create_courseware_user: |
| 196 | + create_user(user) |
| 197 | + user.refresh_from_db() |
174 | 198 |
|
175 | | - def send_enrollment_emails(): |
| 199 | + def _subscribe_to_edx_course_emails(): |
176 | 200 | subscribe_edx_course_emails.delay(enrollment.id) |
177 | 201 |
|
178 | | - def _enroll_learner_into_associated_programs(): |
179 | | - """ |
180 | | - Enrolls the learner into all programs for which the course they are enrolling into |
181 | | - is associated as a requirement or elective. If a program enrollment already exists |
182 | | - then the change_status of that program_enrollment is checked to ensure it equals None. |
183 | | - """ |
184 | | - for program in run.course.programs: |
185 | | - if not program.live: |
186 | | - continue |
187 | | - program_enrollment, _ = ProgramEnrollment.objects.get_or_create( |
188 | | - user=user, |
189 | | - program=program, |
190 | | - defaults=dict( # noqa: C408 |
191 | | - change_status=None, |
192 | | - ), |
193 | | - ) |
194 | | - if program_enrollment.change_status is not None: |
195 | | - program_enrollment.reactivate_and_save() |
196 | | - |
| 202 | + successful_enrollments = [] |
197 | 203 | edx_request_success = True |
198 | 204 | if not runs[0].is_fake_course_run: |
199 | 205 | # Make the API call to enroll the user in edX only if the run is not a fake course run |
@@ -233,7 +239,7 @@ def _enroll_learner_into_associated_programs(): |
233 | 239 | ), |
234 | 240 | ) |
235 | 241 |
|
236 | | - _enroll_learner_into_associated_programs() |
| 242 | + _enroll_learner_into_associated_programs(run, user) |
237 | 243 |
|
238 | 244 | # If the run is associated with a B2B contract, add the contract |
239 | 245 | # to the user's contract list and update their org memberships |
@@ -267,7 +273,7 @@ def _enroll_learner_into_associated_programs(): |
267 | 273 | if enrollment_mode_changed: |
268 | 274 | enrollment.enrollment_mode = mode |
269 | 275 | enrollment.reactivate_and_save() |
270 | | - transaction.on_commit(send_enrollment_emails) |
| 276 | + transaction.on_commit(_subscribe_to_edx_course_emails) |
271 | 277 | except: # pylint: disable=bare-except # noqa: PERF203, E722 |
272 | 278 | mail_api.send_enrollment_failure_message(user, run, details=format_exc()) |
273 | 279 | log.exception( |
|
0 commit comments