def import_from_canvas(conf, 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 if "roster" not in conf: conf["roster"] = [] course_id = args.id section = args.section force = args.force canvas = CanvasAPI(conf["canvas-token"], conf["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 {}".format( s['sortable_name'])) try: add_to_roster(conf, conf.roster, s['sortable_name'], s['sis_user_id'], section, force) except DuplicateUserError: logger.warning("User {} is already in the roster, skipping".format( s['sis_user_id'])) print("Imported {} students.".format(len(students)))
def import_students(conf, args): """Imports students from a CSV file to the roster. """ section = args.section # TODO: This should probably move to another file email_re = re.compile(r"^(?P<user>[^@]+)") with open(args.file) as fh: reader = csv.reader(fh) if "roster" not in conf: conf["roster"] = [] # Note: This is incredibly hardcoded. # However, peoplesoft never updates anything, so we're probably good. reader.__next__() # Skip the header count = 0 for row in reader: count += 1 match = email_re.match(row[4]) try: add_to_roster(conf, conf.roster, row[3], match.group("user"), section, args.force) except DuplicateUserError: logger.warning( "User {} is already in the roster, skipping".format( match.group("user"))) print("Imported {} students.".format(count))
def add_student(conf, backend, args): """Add a student to the roster """ try: add_to_roster(conf, backend, conf.roster, args.name, args.username, args.section, args.force) except DuplicateUserError: logger.error("Student already exists in roster!")
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)))