def test_enrollment_by_email(self): user = User.objects.create(username="******", email="*****@*****.**") course_id = SlashSeparatedCourseKey("edX", "Test101", "2013") CourseEnrollment.enroll_by_email("*****@*****.**", course_id) self.assertTrue(CourseEnrollment.is_enrolled(user, course_id)) self.assert_enrollment_event_was_emitted(user, course_id) # This won't throw an exception, even though the user is not found self.assertIsNone( CourseEnrollment.enroll_by_email("*****@*****.**", course_id)) self.assert_no_events_were_emitted() self.assertRaises(User.DoesNotExist, CourseEnrollment.enroll_by_email, "*****@*****.**", course_id, ignore_errors=False) self.assert_no_events_were_emitted() # Now unenroll them by email CourseEnrollment.unenroll_by_email("*****@*****.**", course_id) self.assertFalse(CourseEnrollment.is_enrolled(user, course_id)) self.assert_unenrollment_event_was_emitted(user, course_id) # Harmless second unenroll CourseEnrollment.unenroll_by_email("*****@*****.**", course_id) self.assertFalse(CourseEnrollment.is_enrolled(user, course_id)) self.assert_no_events_were_emitted() # Unenroll on non-existent user shouldn't throw an error CourseEnrollment.unenroll_by_email("*****@*****.**", course_id) self.assert_no_events_were_emitted()
def enroll_email(course_id, student_email, auto_enroll=False, email_students=False, email_params=None): """ Enroll a student by email. `student_email` is student's emails e.g. "*****@*****.**" `auto_enroll` determines what is put in CourseEnrollmentAllowed.auto_enroll if auto_enroll is set, then when the email registers, they will be enrolled in the course automatically. `email_students` determines if student should be notified of action by email. `email_params` parameters used while parsing email templates (a `dict`). returns two EmailEnrollmentState's representing state before and after the action. """ previous_state = EmailEnrollmentState(course_id, student_email) if previous_state.user: CourseEnrollment.enroll_by_email(student_email, course_id) if email_students: email_params['message'] = 'enrolled_enroll' email_params['email_address'] = student_email email_params['full_name'] = previous_state.full_name send_mail_to_student(student_email, email_params) else: cea, _ = CourseEnrollmentAllowed.objects.get_or_create(course_id=course_id, email=student_email) cea.auto_enroll = auto_enroll cea.save() if email_students: email_params['message'] = 'allowed_enroll' email_params['email_address'] = student_email send_mail_to_student(student_email, email_params) after_state = EmailEnrollmentState(course_id, student_email) return previous_state, after_state
def test_enrollment_by_email(self): user = User.objects.create(username="******", email="*****@*****.**") course_id = SlashSeparatedCourseKey("edX", "Test101", "2013") CourseEnrollment.enroll_by_email("*****@*****.**", course_id) self.assertTrue(CourseEnrollment.is_enrolled(user, course_id)) self.assert_enrollment_event_was_emitted(user, course_id) # This won't throw an exception, even though the user is not found self.assertIsNone(CourseEnrollment.enroll_by_email("*****@*****.**", course_id)) self.assert_no_events_were_emitted() self.assertRaises( User.DoesNotExist, CourseEnrollment.enroll_by_email, "*****@*****.**", course_id, ignore_errors=False ) self.assert_no_events_were_emitted() # Now unenroll them by email CourseEnrollment.unenroll_by_email("*****@*****.**", course_id) self.assertFalse(CourseEnrollment.is_enrolled(user, course_id)) self.assert_unenrollment_event_was_emitted(user, course_id) # Harmless second unenroll CourseEnrollment.unenroll_by_email("*****@*****.**", course_id) self.assertFalse(CourseEnrollment.is_enrolled(user, course_id)) self.assert_no_events_were_emitted() # Unenroll on non-existent user shouldn't throw an error CourseEnrollment.unenroll_by_email("*****@*****.**", course_id) self.assert_no_events_were_emitted()
def cm_enroll_user(request): response_format = request.REQUEST.get('format','html') if response_format == 'json' or 'application/json' in request.META.get('HTTP/ACCEPT', 'application/json'): if request.method == 'POST': if validate_token(request.body, request) is False: return HttpResponse('Unauthorized', status=401) if 'email' not in request.json or 'course_id' not in request.json: return HttpResponse(content=json.dumps({'errors':'Missing params'}), \ content_type = 'application/json', status=400) try: log.info("Enrolling user: "******" course: " + request.json.get('course_id')) CourseEnrollment.enroll_by_email(request.json.get('email'), \ get_key_from_course_id(request.json.get('course_id')), ignore_errors=False) # See if enrolling with staff credentials if request.json.get('role') == 'staff': global_admin = AdminFactory() course = get_key_from_course_id(request.json.get('course_id')) auth.add_users(global_admin, CourseInstructorRole(course), User.objects.get(email=request.json.get('email'))) content = {'success':'ok'} status_code = 200 except User.DoesNotExist: content = {'errors':'User does not exist'} status_code = 422 return HttpResponse(content=json.dumps(content), status=status_code, \ content_type = 'application/json') else: return HttpResponse(content=json.dumps({}), status=404, content_type = 'application/json') else: return HttpResponse(content=json.dumps({}), status=404, content_type = 'application/json')
def enroll_email(course_id, student_email, auto_enroll=False): """ Enroll a student by email. `student_email` is student's emails e.g. "*****@*****.**" `auto_enroll` determines what is put in CourseEnrollmentAllowed.auto_enroll if auto_enroll is set, then when the email registers, they will be enrolled in the course automatically. returns two EmailEnrollmentState's representing state before and after the action. """ previous_state = EmailEnrollmentState(course_id, student_email) if previous_state.user: CourseEnrollment.enroll_by_email(student_email, course_id) else: cea, _ = CourseEnrollmentAllowed.objects.get_or_create(course_id=course_id, email=student_email) cea.auto_enroll = auto_enroll cea.save() after_state = EmailEnrollmentState(course_id, student_email) return previous_state, after_state
def enroll_email(course_id, student_email, auto_enroll=False): """ Enroll a student by email. `student_email` is student's emails e.g. "*****@*****.**" `auto_enroll` determines what is put in CourseEnrollmentAllowed.auto_enroll if auto_enroll is set, then when the email registers, they will be enrolled in the course automatically. returns two EmailEnrollmentState's representing state before and after the action. """ previous_state = EmailEnrollmentState(course_id, student_email) if previous_state.user: CourseEnrollment.enroll_by_email(student_email, course_id) else: cea, _ = CourseEnrollmentAllowed.objects.get_or_create( course_id=course_id, email=student_email) cea.auto_enroll = auto_enroll cea.save() after_state = EmailEnrollmentState(course_id, student_email) return previous_state, after_state
def enroll_email(course_id, student_email, auto_enroll=False, email_students=False, email_params=None, language=None): """ Enroll a student by email. `student_email` is student's emails e.g. "*****@*****.**" `auto_enroll` determines what is put in CourseEnrollmentAllowed.auto_enroll if auto_enroll is set, then when the email registers, they will be enrolled in the course automatically. `email_students` determines if student should be notified of action by email. `email_params` parameters used while parsing email templates (a `dict`). `language` is the language used to render the email. returns two EmailEnrollmentState's representing state before and after the action. """ previous_state = EmailEnrollmentState(course_id, student_email) if previous_state.user: # if the student is currently unenrolled, don't enroll them in their # previous mode course_mode = u"honor" if previous_state.enrollment: course_mode = previous_state.mode CourseEnrollment.enroll_by_email(student_email, course_id, course_mode) if email_students: email_params["message"] = "enrolled_enroll" email_params["email_address"] = student_email email_params["full_name"] = previous_state.full_name send_mail_to_student(student_email, email_params, language=language) else: cea, _ = CourseEnrollmentAllowed.objects.get_or_create(course_id=course_id, email=student_email) cea.auto_enroll = auto_enroll cea.save() if email_students: email_params["message"] = "allowed_enroll" email_params["email_address"] = student_email send_mail_to_student(student_email, email_params, language=language) after_state = EmailEnrollmentState(course_id, student_email) return previous_state, after_state
def enroll_email(course_id, student_email, auto_enroll=False, email_students=False, email_params=None, language=None): """ Enroll a student by email. `student_email` is student's emails e.g. "*****@*****.**" `auto_enroll` determines what is put in CourseEnrollmentAllowed.auto_enroll if auto_enroll is set, then when the email registers, they will be enrolled in the course automatically. `email_students` determines if student should be notified of action by email. `email_params` parameters used while parsing email templates (a `dict`). `language` is the language used to render the email. returns two EmailEnrollmentState's representing state before and after the action. """ previous_state = EmailEnrollmentState(course_id, student_email) enrollment_obj = None if previous_state.user: # if the student is currently unenrolled, don't enroll them in their # previous mode # for now, White Labels use 'shoppingcart' which is based on the # "honor" course_mode. Given the change to use "audit" as the default # course_mode in Open edX, we need to be backwards compatible with # how White Labels approach enrollment modes. if CourseMode.is_white_label(course_id): course_mode = CourseMode.DEFAULT_SHOPPINGCART_MODE_SLUG else: course_mode = None if previous_state.enrollment: course_mode = previous_state.mode enrollment_obj = CourseEnrollment.enroll_by_email(student_email, course_id, course_mode) if email_students: email_params['message_type'] = 'enrolled_enroll' email_params['email_address'] = student_email email_params['full_name'] = previous_state.full_name send_mail_to_student(student_email, email_params, language=language) elif not is_email_retired(student_email): cea, _ = CourseEnrollmentAllowed.objects.get_or_create(course_id=course_id, email=student_email) cea.auto_enroll = auto_enroll cea.save() if email_students: email_params['message_type'] = 'allowed_enroll' email_params['email_address'] = student_email send_mail_to_student(student_email, email_params, language=language) after_state = EmailEnrollmentState(course_id, student_email) return previous_state, after_state, enrollment_obj