Example #1
0
def edit_student(student_id):
    try:
        body = request.get_json()

        student = util.query_model(model=Student, id=student_id)

        if 'name' in body:
            student.name = body['name']
        if 'age' in body:
            student.age = util.compute_int(body['age'])
        if 'gender' in body:
            student.gender = body['gender']
        if 'image_link' in body:
            student.image_link = body['image_link']
        if 'profile_link' in body:
            student.profile_link = body['profile_link']
        if 'seeking_volunteer' in body:
            student.seeking_volunteer = util.compute_boolean(
                body['seeking_volunteer'])
        if 'seeking_description' in body:
            student.seeking_description = body['seeking_description']

        student.update()

        return jsonify({"success": True}), 200

    except GenericException as e:
        raise e

    except Exception:
        raise APIException("Bad Request", 400)
Example #2
0
def get_classrooms_of_one_volunteer(volunteer_id):
    try:
        volunteer = util.query_model(model=Volunteer, id=volunteer_id)

        classrooms = (db.session.query(
            Classroom, Student.name).filter_by(volunteer_id=volunteer_id).join(
                Student, Classroom.student_id == Student.id).order_by(
                    Classroom.id).all())

        if not classrooms:
            raise APIException("Resource Not Found", 404)

        classroom_info_list = [{
            **classroom.long(),
            **{
                "student_name": student_name
            }
        } for classroom, student_name in classrooms]

        response = {
            "volunteer_id": volunteer_id,
            "volunteer_name": volunteer.name,
            "count": len(classrooms),
            "classrooms": classroom_info_list
        }

        return jsonify(response), 200

    except GenericException as e:
        raise e

    except Exception:
        raise APIException("Internal Error", 500)
Example #3
0
def get_one_classroom_info(classroom_id):
    try:
        classroom = util.query_model(model=Classroom, id=classroom_id)
        return jsonify(classroom.long()), 200

    except GenericException as e:
        raise e

    except Exception:
        raise APIException("Internal Error", 500)
Example #4
0
def get_one_student_info(student_id):
    try:
        student = util.query_model(model=Student, id=student_id)
        return jsonify(student.long()), 200

    except GenericException as e:
        raise e

    except Exception:
        raise APIException("Internal Error", 500)
Example #5
0
def get_one_volunteer_info(volunteer_id):
    try:
        volunteer = util.query_model(model=Volunteer, id=volunteer_id)
        return jsonify(volunteer.long()), 200

    except GenericException as e:
        raise e

    except Exception:
        raise APIException("Internal Error", 500)
Example #6
0
def remove_classroom(classroom_id):
    try:
        classroom = util.query_model(model=Classroom, id=classroom_id)
        classroom.delete()

        return jsonify({"success": True}), 200

    except GenericException as e:
        raise e

    except Exception:
        raise APIException("Internal Error", 500)
Example #7
0
def get_students():
    try:
        students = util.query_model(model=Student, id=None)
        students_list = [student.short() for student in students]
        response = {"count": len(students), "volunteers": students_list}
        return jsonify(response), 200

    except GenericException as e:
        raise e

    except Exception:
        raise APIException("Internal Error", 500)
Example #8
0
def remove_student(student_id):
    try:
        student = util.query_model(model=Student, id=student_id)
        student.delete()

        return jsonify({"success": True}), 200

    except GenericException as e:
        raise e

    except Exception:
        raise APIException("Internal Error", 500)
Example #9
0
def remove_volunteer(volunteer_id):
    try:
        volunteer = util.query_model(model=Volunteer, id=volunteer_id)
        volunteer.delete()

        return jsonify({"success": True}), 200

    except GenericException as e:
        raise e

    except Exception:
        raise APIException("Internal Error", 500)
Example #10
0
def get_volunteers():
    try:
        volunteers = util.query_model(model=Volunteer, id=None)
        volunteers_list = [volunteer.short() for volunteer in volunteers]
        response = {"count": len(volunteers), "volunteers": volunteers_list}

        return jsonify(response), 200

    except GenericException as e:
        raise e

    except Exception:
        raise APIException("Internal Error", 500)
Example #11
0
def edit_classroom(classroom_id):
    try:
        body = request.get_json()

        classroom = util.query_model(model=Classroom, id=classroom_id)

        if 'description' in body:
            classroom.description = body['description']
        if 'time' in body:
            classroom.time = body['time']
        if 'start_date' in body:
            classroom.start_date = body['start_date']
        if 'end_date' in body:
            classroom.end_date = body['end_date']

        classroom.update()

        return jsonify({"success": True}), 200

    except GenericException as e:
        raise e

    except Exception:
        raise APIException("Bad Request", 400)