Example #1
0
def print_canvas_courses(conf, args):
    """Show a list of current teacher's courses from Canvas via the API.
    """
    if 'canvas-token' not in conf:
        logging.error("canvas-token configuration is missing! Please set the Canvas API access "
                      "token before attempting to use Canvas API functionality")
        print("Canvas course listing failed: missing Canvas API access token.")
        return

    canvas = CanvasAPI(conf["canvas-token"])

    courses = canvas.get_teacher_courses()

    if not courses:
        print("No courses found where current user is a teacher.")
        return

    print('-'*92)
    print("| # | %-6s | %-75s |" % ('ID', 'Course Title'))
    print('-'*92)
    for ix, c in enumerate(courses):
        print("| %s | %-6s | %-75s |" % (ix+1, c['id'], c['name']))
    print('-'*92)
Example #2
0
def import_from_canvas(conf, args):
    """Imports students from a Canvas course to the roster.
    """
    if 'canvas-token' not in conf:
        logging.error("canvas-token configuration is missing! Please set the Canvas API access "
                      "token before attempting to import users from Canvas")
        print("Import from canvas failed: missing Canvas API access token.")
        return

    if "roster" not in conf:
        conf["roster"] = []

    course_id = args.id
    section = args.section

    canvas = CanvasAPI(conf["canvas-token"])

    students = canvas.get_course_students(course_id)

    for s in students:
        conf.roster.append({
            "name": s['sortable_name'],
            "username": s['sis_user_id'],
            "section": section
        })

        try:
            conf.roster[-1]["id"] = Repo.get_user_id(
                s['sis_user_id'], conf.gitlab_host, conf.token
            )
        except RepoError:
            logger.warning(
                "Student {} does not have a Gitlab account.".format(s['name'])
            )

    print("Imported {} students.".format(len(students)))