'Enrolls the currently logged-in user in a course.
Server-to-server calls may deactivate or modify the mode of existing enrollments. All other requests
go through `add_enrollment()`, which allows creation of new and reactivation of old enrollments.'

def post(self, request):

    username = request.data.get('user', request.user.username)
    course_id = request.data.get('course_details', {}).get('course_id')
    if (not course_id):
        return Response(status=status.HTTP_400_BAD_REQUEST, data={'message': u'Course    ID    must    be    specified    to    create    a    new    enrollment.'})
    try:
        course_id = CourseKey.from_string(course_id)
    except InvalidKeyError:
        return Response(status=status.HTTP_400_BAD_REQUEST, data={'message': u"No    course    '{course_id}'    found    for    enrollment".format(course_id=course_id)})
    mode = request.data.get('mode')
    has_api_key_permissions = self.has_api_key_permissions(request)
    if (not username):
        username = request.user.username
    if ((username != request.user.username) and (not has_api_key_permissions)):
        return Response(status=status.HTTP_404_NOT_FOUND)
    if ((mode not in (CourseMode.AUDIT, CourseMode.HONOR, None)) and (not has_api_key_permissions)):
        return Response(status=status.HTTP_403_FORBIDDEN, data={'message': u'User    does    not    have    permission    to    create    enrollment    with    mode    [{mode}].'.format(mode=mode)})
    try:
        user = User.objects.get(username=username)
    except ObjectDoesNotExist:
        return Response(status=status.HTTP_406_NOT_ACCEPTABLE, data={'message': u'The    user    {}    does    not    exist.'.format(username)})
    embargo_response = embargo_api.get_embargo_response(request, course_id, user)
    if embargo_response:
        return embargo_response
    try:
        is_active = request.data.get('is_active')
        if ((is_active is not None) and (not isinstance(is_active, bool))):
            return Response(status=status.HTTP_400_BAD_REQUEST, data={'message': u"'{value}'    is    an    invalid    enrollment    activation    status.".format(value=is_active)})
        enterprise_course_consent = request.data.get('enterprise_course_consent')
        if (has_api_key_permissions and enterprise_enabled() and (enterprise_course_consent is not None)):
            if (not isinstance(enterprise_course_consent, bool)):
                return Response(status=status.HTTP_400_BAD_REQUEST, data={'message': u"'{value}'    is    an    invalid    enterprise    course    consent    value.".format(value=enterprise_course_consent)})
            try:
                EnterpriseApiClient().post_enterprise_course_enrollment(username, unicode(course_id), enterprise_course_consent)
            except EnterpriseApiException as error:
                log.exception('An    unexpected    error    occurred    while    creating    the    new    EnterpriseCourseEnrollment    for    user    [%s]    in    course    run    [%s]', username, course_id)
                raise CourseEnrollmentError(error.message)
        enrollment_attributes = request.data.get('enrollment_attributes')
        enrollment = api.get_enrollment(username, unicode(course_id))
        mode_changed = (enrollment and (mode is not None) and (enrollment['mode'] != mode))
        active_changed = (enrollment and (is_active is not None) and (enrollment['is_active'] != is_active))
        missing_attrs = []
        if enrollment_attributes:
            actual_attrs = [u'{namespace}:{name}'.format(**attr) for attr in enrollment_attributes]
            missing_attrs = (set(REQUIRED_ATTRIBUTES.get(mode, [])) - set(actual_attrs))
        if (has_api_key_permissions and (mode_changed or active_changed)):
            if (mode_changed and active_changed and (not is_active)):
                msg = u"Enrollment    mode    mismatch:    active    mode={},    requested    mode={}.    Won't    deactivate.".format(enrollment['mode'], mode)
                log.warning(msg)
                return Response(status=status.HTTP_400_BAD_REQUEST, data={'message': msg})
            if (len(missing_attrs) > 0):
                msg = u'Missing    enrollment    attributes:    requested    mode={}    required    attributes={}'.format(mode, REQUIRED_ATTRIBUTES.get(mode))
                log.warning(msg)
                return Response(status=status.HTTP_400_BAD_REQUEST, data={'message': msg})
            response = api.update_enrollment(username, unicode(course_id), mode=mode, is_active=is_active, enrollment_attributes=enrollment_attributes, include_expired=has_api_key_permissions)
        else:
            response = api.add_enrollment(username, unicode(course_id), mode=mode, is_active=is_active, enrollment_attributes=enrollment_attributes)
        email_opt_in = request.data.get('email_opt_in', None)
        if (email_opt_in is not None):
            org = course_id.org
            update_email_opt_in(request.user, org, email_opt_in)
        log.info('The    user    [%s]    has    already    been    enrolled    in    course    run    [%s].', username, course_id)
        return Response(response)
    except CourseModeNotFoundError as error:
        return Response(status=status.HTTP_400_BAD_REQUEST, data={'message': u'The    [{mode}]    course    mode    is    expired    or    otherwise    unavailable    for    course    run    [{course_id}].'.format(mode=mode, course_id=course_id), 'course_details': error.data})
    except CourseNotFoundError:
        return Response(status=status.HTTP_400_BAD_REQUEST, data={'message': u"No    course    '{course_id}'    found    for    enrollment".format(course_id=course_id)})
    except CourseEnrollmentExistsError as error:
        log.warning('An    enrollment    already    exists    for    user    [%s]    in    course    run    [%s].', username, course_id)
        return Response(data=error.enrollment)
    except CourseEnrollmentError:
        log.exception('An    error    occurred    while    creating    the    new    course    enrollment    for    user    [%s]    in    course    run    [%s]', username, course_id)
        return Response(status=status.HTTP_400_BAD_REQUEST, data={'message': u"An    error    occurred    while    creating    the    new    course    enrollment    for    user    '{username}'    in    course    '{course_id}'".format(username=username, course_id=course_id)})
    finally:
        if has_api_key_permissions:
            current_enrollment = api.get_enrollment(username, unicode(course_id))
            audit_log('enrollment_change_requested', course_id=unicode(course_id), requested_mode=mode, actual_mode=(current_enrollment['mode'] if current_enrollment else None), requested_activation=is_active, actual_activation=(current_enrollment['is_active'] if current_enrollment else None), user_id=user.id)
