def student_route(uname):
    if request.method == "POST":
        try:
            data = request.get_json(force=True)
            student = models.Student.query.filter_by(username=uname).first()
            for k in data:
                if k == "username":
                    student.username = data[k].lower()
                    continue
                student.__setattr__(k, data[k])
            db.add(student)
            db.commit()
            response = row2dict(student)
        except Exception as e:
            response = dict()
            response["msg"] = e.message
    else:
        try:
            student = models.Student.query.filter_by(username=uname).first()
            response = row2dict(student)
            response["gravatar"] = student.gravatar()
        except Exception as e:
            response = dict()
            response["msg"] = e.message
    return jsonify(response)
def new_school_route():
    data = request.get_json(force=True)
    try:
        school = models.School(data["name"])
        db.add(school)
        db.commit()
        response = row2dict(school)
        response["msg"] = "Success"
    except Exception as e:
        response = dict()
        response["msg"] = e.message
    return jsonify(response)
def new_student_route():
    data = request.get_json(force=True)
    try:
        student = models.Student(data["username"], data["realname"], data["email"])
        db.add(student)
        db.commit()
        response = row2dict(student)
        response["msg"] = "Success"
    except Exception as e:
        response = dict()
        response["msg"] = e.message
    return jsonify(response)
def course_addstudent_route(cid, uname):
    try:
        course = models.Course.query.filter_by(id=cid).first()
        student = models.Student.query.filter_by(username=uname).first()
        course.students.append(student)
        db.add(student)
        db.add(course)
        db.commit()
        response = dict()
        response["msg"] = "Success"
    except Exception as e:
        response = dict()
        response["msg"] = e.message
    return jsonify(response)
def new_course_route():
    data = request.get_json(force=True)
    try:
        course = models.Course(data["coursename"], data["professor"])
        school = models.School.query.filter_by(id=data["school_id"]).first()
        school.courses.append(course)
        db.add(course)
        db.add(school)
        db.commit()
        response = row2dict(course)
        response["msg"] = "Success"
    except Exception as e:
        response = dict()
        response["msg"] = e.message
    return jsonify(response)
def course_route(cid):
    if request.method == "POST":
        try:
            data = request.get_json(force=True)
            course = models.Course.query.filter_by(id=cid).first()
            for k in data:
                course.__setattr__(k, data[k])
            db.add(course)
            db.commit()
            response = row2dict(course)
        except Exception as e:
            response = dict()
            response["msg"] = e.message
    else:
        try:
            course = models.Course.query.filter_by(id=cid).first()
            response = row2dict(course)
        except Exception as e:
            response = dict()
            response["msg"] = e.message
    return jsonify(response)
def note_route(nid):
    if request.method == "POST":
        try:
            data = request.get_json(force=True)
            note = models.Note.query.filter_by(id=nid).first()
            for k in data:
                if k == "timestamp":
                    note.timestamp = dt.strptime(data["timestamp"], "%Y-%m-%d %H:%M:%S.%f")  # ISO 8601
                    continue
                note.__setattr__(k, data[k])
            db.add(note)
            db.commit()
            response = row2dict(note)
        except Exception as e:
            response = dict()
            response["msg"] = e.message
    else:
        try:
            note = models.Note.query.filter_by(id=nid).first()
            response = row2dict(note)
        except Exception as e:
            response = dict()
            response["msg"] = e.message
    return jsonify(response)
def new_note_route():
    data = request.get_json(force=True)
    try:
        if hasattr(data, "timestamp"):
            note = models.Note(data["title"], data["body"], data["timestamp"])
        else:
            note = models.Note(data["title"], data["body"])
        student = models.Student.query.filter_by(id=data["student_id"]).first()
        course = models.Course.query.filter_by(id=data["course_id"]).first()
        student.notes.append(note)
        course.notes.append(note)
        db.add(student)
        db.add(course)
        db.add(note)
        db.commit()
        response = row2dict(note)
        response["msg"] = "Success"
    except Exception as e:
        response = dict()
        response["msg"] = e.message
    return jsonify(response)