def test_failed_dns(self): self.mock_get.side_effect = HTTPError("Name or service not known") api = CanvasAPI("test token", "mst.instructure.com") with self.assertRaises(NameResolutionFailed): api.get_instructor_courses() with self.assertRaises(NameResolutionFailed): api.get_course_students(420)
def import_from_canvas(conf, backend, args): """Imports students from a Canvas course to the roster. """ if 'canvas-token' not in conf: logger.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 course_id = args.id section = args.section force = args.force username_column = args.username_column canvas = CanvasAPI(conf["canvas-token"], conf["canvas-host"]) try: students = canvas.get_course_students(course_id) if "canvas-courses" not in conf: conf["canvas-courses"] = [] add_to_courses(conf["canvas-courses"], int(course_id), section) except AuthenticationFailed as e: logger.debug(e) logger.error( "Canvas authentication failed. Is your token missing or expired?") return except CourseNotFound as e: logger.debug(e) logger.error( "Course ID %s not found. Make sure to use the ID, not the row number.", course_id) return for s in students: logger.debug(s) if username_column not in s or not s[username_column]: logger.error("Could not get username for %s", s['sortable_name']) try: add_to_roster(conf, backend, conf.roster, s['sortable_name'], s[username_column], section, force, s['id']) except DuplicateUserError: logger.warning("User %s is already in the roster, skipping", s[username_column]) print("Imported {} students.".format(len(students)))
def import_from_canvas(args): """Imports students from a Canvas course to the roster. """ g = Grader(args.path) if 'canvas-token' not in g.config: logger.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 course_id = args.id force = args.force canvas = CanvasAPI(g.config["canvas-token"], g.config["canvas-host"]) students = canvas.get_course_students(course_id) for s in students: if 'sis_user_id' not in s: logger.error("Could not get username for %s", s['sortable_name']) if not force: for student in g.config.roster: if student['name'] == s['sortable_name']: logger.warning( "User %s is already in the roster, skipping", s['sis_user_id']) break else: g.config.roster.append({ 'name': s['sortable_name'], 'id': s['sis_user_id'] }) else: g.config.roster.append({ 'name': s['sortable_name'], 'id': s['sis_user_id'] }) print("Imported {} students.".format(len(students))) g.config.save()
def test_course_not_found(self): self.mock_get.side_effect = HTTPError(response=FakeResponse(404)) api = CanvasAPI("test token", "mst.instructure.com") with self.assertRaises(CourseNotFound): api.get_course_students(420)
def test_students_auth_failure(self): self.mock_get.side_effect = HTTPError(response=FakeResponse(401)) api = CanvasAPI("test token", "mst.instructure.com") with self.assertRaises(AuthenticationFailed): api.get_course_students(420)