def get_canvas_user_profile(sis_user_id):
    """
    This method will fetch the canvas user profile , given the sis_user_id
    :param sis_user_id: The sis_user_id of the user, without the sis_user_id: prefix
    :type sis_user_id: string
    return: Returns json representing the canvas user profile fetched by the canvas_sdk
    """
    response = get_user_profile(request_ctx=SDK_CONTEXT, user_id='sis_user_id:%s' % sis_user_id)
    canvas_user_profile = response.json()
    return canvas_user_profile
def get_canvas_user_profile(sis_user_id):
    """
    This method will fetch the canvas user profile , given the sis_user_id
    :param sis_user_id: The sis_user_id of the user, without the sis_user_id: prefix
    :type sis_user_id: string
    return: Returns json representing the canvas user profile fetched by the canvas_sdk
    """
    response = get_user_profile(request_ctx=SDK_CONTEXT, user_id='sis_user_id:%s' % sis_user_id)
    canvas_user_profile = response.json()
    return canvas_user_profile
def enroll_creator_in_new_course(sis_course_id, user_id):
    """
    Silently enroll instructor / creator to the new course so it can be accessed immediately

        :param sis_course_id: newly created Canvas course's SIS course ID to enroll course creator
        :type sis_course_id: string
        :param user_id: The user ID of the creator/instructor to enroll in the course
        :type user_id: string - Canvas user ID or SIS user ID prefixed with 'sis_user_id:'
        :return: Canvas enrollment information (response of enrollment request)
        :rtype: json-encoded content of response
        :raises: NoCanvasUserToEnroll
    """

    # check if user exists in Canvas before enrolling
    logger.debug("Checking for user_id=%s" % user_id)

    get_user_response = get_user_profile(request_ctx=SDK_CONTEXT, user_id=user_id)

    logger.debug("--> response: %s" % get_user_response.json())

    if get_user_response.status_code == 200:
        # user was found; we may have found user using sis_user_id, so make sure we note canvas_user_id from response
        user_data = get_user_response.json()
        canvas_user_id = str(user_data['id'])
        sis_user_id = str(user_data['sis_user_id'])
    else:
        # Could not find user matching sis_user_id
        logger.debug("No user found with user_id=%s" % user_id)
        raise NoCanvasUserToEnroll(user_id)

    # Assumptions:
    # 1. the course creator should be enrolled in a section with sis_section_id equal to the SIS course ID
    # 2. the course creator should be enrolled according to the the registrar's feed recorded in CourseManger.
    #  default to Teacher if a role is not found

    enrollment_type = 'TeacherEnrollment'

    # Check for the specific  role from the course staff table
    try:
        user_staff_record = CourseStaff.objects.get(
            course_instance_id=sis_course_id,
            user_id=sis_user_id)
        role_id = user_staff_record.role_id
        logger.debug('CourseStaff role_id =%s' % role_id)

        # Fetch the canvas role information from user role table.
        enrollment_role_record = UserRole.objects.get(role_id=role_id)
        enrollment_role = enrollment_role_record.canvas_role
        logger.info('Attempting to add user to course with role=%s ' % enrollment_role)

        current_user_enrollment_result = enroll_user_sections(request_ctx=SDK_CONTEXT,
                                                              section_id='sis_section_id:%s' % sis_course_id,
                                                              enrollment_user_id=canvas_user_id,
                                                              enrollment_type=enrollment_type,
                                                              enrollment_role=enrollment_role,
                                                              enrollment_enrollment_state='active')

    except Exception as exception:
        logger.exception('Exception in getting custom role information for '
                         'sis_course_id=%s and user_id=%s' % (sis_course_id, sis_user_id))

        # If there is a failure in fetching role information, default to adding the user as Teacher
        current_user_enrollment_result = enroll_user_sections(request_ctx=SDK_CONTEXT,
                                                              section_id='sis_section_id:%s' % sis_course_id,
                                                              enrollment_user_id=canvas_user_id,
                                                              enrollment_type=enrollment_type,
                                                              enrollment_enrollment_state='active')

    logger.debug("Enroll user response: %s" % current_user_enrollment_result.json())
    return current_user_enrollment_result.json()
def enroll_creator_in_new_course(sis_course_id, user_id):
    """
    Silently enroll instructor / creator to the new course so it can be accessed immediately

        :param sis_course_id: newly created Canvas course's SIS course ID to enroll course creator
        :type sis_course_id: string
        :param user_id: The user ID of the creator/instructor to enroll in the course
        :type user_id: string - Canvas user ID or SIS user ID prefixed with 'sis_user_id:'
        :return: Canvas enrollment information (response of enrollment request)
        :rtype: json-encoded content of response
        :raises: NoCanvasUserToEnroll
    """

    # check if user exists in Canvas before enrolling
    logger.debug("Checking for user_id=%s" % user_id)

    get_user_response = get_user_profile(request_ctx=SDK_CONTEXT, user_id=user_id)

    logger.debug("--> response: %s" % get_user_response.json())

    if get_user_response.status_code == 200:
        # user was found; we may have found user using sis_user_id, so make sure we note canvas_user_id from response
        user_data = get_user_response.json()
        canvas_user_id = str(user_data['id'])
        sis_user_id = str(user_data['sis_user_id'])
    else:
        # Could not find user matching sis_user_id
        logger.debug("No user found with user_id=%s" % user_id)
        raise NoCanvasUserToEnroll(user_id)

    # Assumptions:
    # 1. the course creator should be enrolled in a section with sis_section_id equal to the SIS course ID
    # 2. the course creator should be enrolled according to the the registrar's feed recorded in CourseManger.
    #  default to Teacher if a role is not found

    enrollment_type = 'TeacherEnrollment'

    # Check for the specific  role from the course staff table
    try:
        user_staff_record = CourseStaff.objects.get(
            course_instance_id=sis_course_id,
            user_id=sis_user_id)
        role_id = user_staff_record.role_id
        logger.debug('CourseStaff role_id =%s' % role_id)

        # Fetch the canvas role information from user role table.
        enrollment_role_record = UserRole.objects.get(role_id=role_id)
        enrollment_role = enrollment_role_record.canvas_role
        logger.info('Attempting to add user to course with role=%s ' % enrollment_role)

        current_user_enrollment_result = enroll_user_sections(request_ctx=SDK_CONTEXT,
                                                              section_id='sis_section_id:%s' % sis_course_id,
                                                              enrollment_user_id=canvas_user_id,
                                                              enrollment_type=enrollment_type,
                                                              enrollment_role=enrollment_role,
                                                              enrollment_enrollment_state='active')

    except Exception as exception:
        logger.exception('Exception in getting custom role information for '
                         'sis_course_id=%s and user_id=%s' % (sis_course_id, sis_user_id))

        # If there is a failure in fetching role information, default to adding the user as Teacher
        current_user_enrollment_result = enroll_user_sections(request_ctx=SDK_CONTEXT,
                                                              section_id='sis_section_id:%s' % sis_course_id,
                                                              enrollment_user_id=canvas_user_id,
                                                              enrollment_type=enrollment_type,
                                                              enrollment_enrollment_state='active')

    logger.debug("Enroll user response: %s" % current_user_enrollment_result.json())
    return current_user_enrollment_result.json()