Exemple #1
0
def patients():
    query = Patient.query.join(Department.patients).filter(
        Department.department_name == request.cookies.get('department', 'default'))

    if request.method == "GET":
        offset = int(request.args.get('offset', 0))
        limit = int(request.args.get('limit', 100))

        return make_response((json.dumps([
            p.serialize() for p in query
            .order_by(desc(Patient.latest_checkin_time))
            .offset(offset)
            .limit(limit)
            .all()
        ]), 200, {"Content-Type": "application/json"}))

    elif request.method == "POST":
        patient_data = json.loads(request.data)
        patient = Patient(**patient_data)
        patient = db.merge(patient)
        db.commit()

        department = Department.query.filter_by(
            department_name=request.cookies.get('department', 'default')).first()

        if department:
            patient.departments.append(department)

        db.commit()

        if 'checkin' in patient_data:
            add_checkin(patient.nhi, patient_data['checkin'])

        return jsonify(patient.serialize())
Exemple #2
0
def create_patient():

    body = request.get_json()

    if body is None:
        raise APIException("You need to specify the request body as a json object", 400)

    # Validations
    #user_id
    if "user_id" in body:
        ui = body["user_id"]
    else:
        raise APIException("You need to specify an user_id", 400)
    #phobia
    if "phobia" in body:
        p = body["phobia"]
    #wishfearless
    if "wishfearless" in body:
        w = body["wishfearless"]
    #previous_help
    if "previous_help" in body:
        ph = body["previous_help"]
    #severity
    if "severity" in body:
        s = body["severity"]

    new_patient = Patient(user_id = ui, 
                    phobia = p,
                    wishfearless = w, 
                    previous_help = ph, 
                    severity = s)

    db.session.add(new_patient)
    db.session.commit()

    # target_patient = Patient.query.get(ui)
    # if target_patient is None:
    #     raise APIException("User not found", 400)

    print(new_patient.serialize())
    return jsonify(new_patient.serialize(), 200)
def handle_patient():
    """
    Create person and retrieve all users
    """
    # POST request

    if request.method == 'POST':
        body = request.get_json()
        if body is None:
            raise APIException(
                "You need to specify the request body as a json object",
                status_code=400)
        if ('first_name' not in body or 'last_name' not in body
                or 'date' not in body or 'birth_date' not in body
                or 'gender' not in body or 'address' not in body
                or 'email' not in body or 'phone_number' not in body
                or 'id_number' not in body):
            raise APIException('Please check your input', status_code=400)
        print(body['date'])
        user1 = Patient(date=datetime.strptime(body['date'], "%m-%d-%Y"),
                        first_name=body['first_name'],
                        last_name=body['last_name'],
                        birth_date=datetime.strptime(body['birth_date'],
                                                     "%m-%d-%Y"),
                        gender=body['gender'],
                        address=body['address'],
                        email=body['email'],
                        phone_number=body['phone_number'],
                        id_number=body['id_number'],
                        doctor_id=get_jwt_identity())
        try:
            db.session.add(user1)
            db.session.commit()
            return jsonify(user1.serialize()), 201
        except Exception as error:
            db.session.rollback()
            print(error)
            return "bad request", 400
    # GET request
    if request.method == 'GET':
        all_people = Patient.query.filter_by(
            doctor_id=get_jwt_identity()).all()
        all_people = list(map(lambda x: x.serialize(), all_people))
        return jsonify(all_people), 200
    return "Invalid Method", 404