Пример #1
0
def load_students() -> list:
    SharedData.STUDENTS.clear()
    path = SharedData.get_student_data()
    if not os.path.exists(path):
        return []

    with open(path, "r") as students_file:
        try:
            students_json = json.load(students_file)
        except json.decoder.JSONDecodeError:
            students_json = []

    for student in students_json:
        SharedData.STUDENTS.append(
            Student(
                student["first_name"],
                student["last_name"],
                student["email"],
                student["section"],
                student["id"],
            ))

    return SharedData.STUDENTS
Пример #2
0
def save_roster(roster):
    """Save the roster of students to a json file"""
    roster = roster["roster"]  # It is stored under "roster" in the json

    # Download students (and others)
    students = []
    for role in roster:
        for person in roster[role]:
            student = {}
            student["first_name"] = person["first_name"]
            student["last_name"] = person["last_name"]
            student["email"] = person["primary_email"]
            student["id"] = person["user_id"]

            if "class_section" in person:
                student["section"] = person["class_section"]["value"]
            else:
                student["section"] = -1

            students.append(student)

    out_path = SharedData.get_student_data()
    with open(out_path, "w") as _file:
        json.dump(students, _file, indent=2)