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())
def checkin(nhi, checkin_id): checkin = CheckIn.query.filter_by( patient_nhi=nhi, checkin_id=checkin_id).first() if not checkin: return make_response("No checkin for patient {} with id {}".format(nhi, checkin_id)) elif request.method == "GET": return jsonify(checkin.serialize()) elif request.method == "POST": checkin_data = json.loads(request.data) checkin = CheckIn(**checkin_data) checkin.checkin_id = checkin_id checkin = db.merge(checkin) db.commit() return jsonify(checkin.serialize()) elif request.method == "DELETE": db.delete(checkin) db.commit() return make_response("Deleted checkin: {} from patient: {}".format(checkin_id, nhi), 200)
def patient_appointment(nhi, appointment_id): appointment = Appointment.query.filter_by( patient_nhi=nhi, appointment_id=appointment_id).first() if not appointment: return make_response("No appointment for patient {} with id {}".format(nhi, appointment_id)) elif request.method == "GET": return jsonify(appointment.serialize()) elif request.method == "POST": appointment_data = json.loads(request.data) appointment = Appointment(**appointment_data) appointment.appointment_id = appointment_id appointment = db.merge(appointment) db.commit() return jsonify(appointment.serialize()) elif request.method == "DELETE": db.delete(appointment) db.commit() return make_response("Deleted appointment: {} from patient: {}".format(appointment_id, nhi), 200)
def vital_info(nhi, vital_info_id): vitalinfo = VitalInfo.query.filter_by( patient_nhi=nhi, vital_info_id=vital_info_id).first() if not vitalinfo: return make_response("No vitalinfo for patient {} with id {}".format(nhi, vital_info_id)) elif request.method == "GET": return jsonify(vitalinfo.serialize()) elif request.method == "POST": vitalinfo_data = json.loads(request.data) vitalinfo = VitalInfo(**vitalinfo_data) vitalinfo.vital_info_id = vital_info_id vitalinfo = db.merge(vitalinfo) db.commit() return jsonify(vitalinfo.serialize()) elif request.method == "DELETE": db.delete(vitalinfo) db.commit() return make_response("Deleted vitalinfo: {} from patient: {}".format(vital_info_id, nhi), 200)