def _force_create_enrollment(username, course_id, mode, is_active): """ forced create enrollment internal function """ try: course_key = get_valid_course_key(course_id) user = User.objects.get(username=username) enrollment = CourseEnrollment.enroll(user, course_key, check_access=False) api._data_api()._update_enrollment(enrollment, is_active=is_active, mode=mode) except Exception as err: # pylint: disable=broad-except raise APIException(repr(err)) return enrollment
def _create_or_update_enrollment(username, course_id, mode, is_active, try_update): """ non-forced create or update enrollment internal function """ try: enrollment = api._data_api().create_course_enrollment( username, course_id, mode, is_active) except CourseEnrollmentExistsError as err: if try_update: enrollment = api._data_api().update_course_enrollment( username, course_id, mode, is_active) else: raise Exception( repr(err) + ", use force to update the existing enrollment") return enrollment
def update_enrollment(user, course_id, mode, *args, **kwargs): """ Update enrollment of given user in the course provided. Example: >>>update_enrollment( user_object, course_id, mode, is_active=False, enrollment_attributes=[ { "namespace": "credit", "name": "provider_id", "value": "hogwarts", }, {...} ] } ) """ username = user.username is_active = kwargs.get('is_active', True) enrollment_attributes = kwargs.get('enrollment_attributes', None) LOG.info('Updating enrollment for student: %s of course: %s mode: %s', username, course_id, mode) enrollment = api._data_api().update_course_enrollment( username, course_id, mode, is_active) if not enrollment: raise NotFound('No enrollment found for {}'.format(username)) if enrollment_attributes is not None: api.set_enrollment_attributes(username, course_id, enrollment_attributes) return { 'user': enrollment['user'], 'course_id': course_id, 'mode': enrollment['mode'], 'is_active': enrollment['is_active'], 'enrollment_attributes': enrollment_attributes, }