def nursePastAppt(): if not helper.check_nurse_privilege(): return redirect("/loadHomePage") end_date = datetime.datetime.strptime(request.form['endDate'], helper.DATE_FORMAT) nurse_id = current_user.get_id() if request.form['startDate']: start_date = datetime.datetime.strptime(request.form['startDate'], helper.DATE_FORMAT) apps = helper.dept_appts(user=current_user, period=(end_date-start_date).days,\ start_date=start_date).filter(Application.status==StatusEnum.finished) else: # if startDate is None then get all past appts apps = helper.dept_appts(user=current_user, direction="past",\ start_date=end_date).filter(Application.status==StatusEnum.finished) helper.load_id2name_map() return make_response( jsonify([{ "appID": app.id, "date": app.date.strftime(helper.DATE_FORMAT), "time": app.time.strftime(helper.TIME_FORMAT), "doctor": helper.id2name(app.doctor_id), "patient": helper.id2name(app.patient_id), "symptoms": app.symptoms } for app in apps]))
def nurseOnGoingAppt(): if not helper.check_nurse_privilege(): return redirect("/loadHomePage") helper.load_id2name_map() nurse_id = current_user.get_id() nowtime = datetime.datetime.now() # filter1: today's appts; filter2: status=approved today_approved_appts = helper.dept_appts(user=current_user, period=0).\ filter( Application.status == StatusEnum.approved ).all() # filter2: now() in timeslot now_approved_appts = [] for appt in today_approved_appts: appt_date_time = datetime.datetime.combine(appt.date, appt.time) if appt_date_time <= nowtime <= appt_date_time + timedelta(minutes=30): now_approved_appts.append(appt) return make_response( jsonify([{ "appID": appt.id, "date": appt.date.strftime(helper.DATE_FORMAT), "time": appt.time.strftime(helper.TIME_FORMAT), "doctor": helper.id2name(appt.doctor_id), "patient": helper.id2name(appt.patient_id), "symptoms": appt.symptoms } for appt in now_approved_appts]))
def viewMC(): if not (helper.check_doctor_privilege() or helper.check_nurse_privilege()): return redirect("/loadHomePage") patient_id = request.form['patientID'] helper.load_id2name_map() table = Application.query.filter( Application.patient_id == patient_id, Application.status == StatusEnum.finished).all() return make_response( jsonify({ 'patientID': str(patient_id), 'patientName': helper.id2name(patient_id), 'appts': [{ 'appID': str(table[i].id), 'mcID': table[i].mc_id, 'date': table[i].date.strftime(helper.DATE_FORMAT), 'time': table[i].time.strftime(helper.TIME_FORMAT), 'doctor': helper.id2name(table[i].doctor_id), 'symptoms': table[i].symptoms } for i in range(len(table))] }))
def nursePendingApp(): if not helper.check_nurse_privilege(): return redirect("/loadHomePage") # look up Time_slot table for next 7 days time_slot id next7d_slotid = helper.day2slotid(period=7) nurse_id = current_user.get_id() pending_app = helper.dept_appts(user=current_user, period=7).\ filter( Application.status==StatusEnum.pending, Application.time_slot_id.in_(next7d_slotid)).all() helper.load_id2name_map() def response_generator(app): return { "appID": app.id, "date": app.date.strftime(helper.DATE_FORMAT), "time": app.time.strftime(helper.TIME_FORMAT), "doctor": helper.id2name(app.doctor_id), "patient": helper.id2name(app.patient_id), "symptoms": app.symptoms } ret = [response_generator(app) for app in pending_app] return make_response(jsonify(ret), 200)
def goViewMC(): if not (helper.check_doctor_privilege() or helper.check_nurse_privilege()): return redirect("/loadHomePage") patient_id = request.form['patientID'] helper.load_id2name_map() return render_template('doctorNurseViewMC.html', patientID=patient_id, patientName=helper.id2name(patient_id))
def nurseFutureAppt(): if not helper.check_nurse_privilege(): return redirect("/loadHomePage") nurse_id = current_user.get_id() start_date = datetime.datetime.strptime(request.form['startDate'], helper.DATE_FORMAT) start_date_str = request.form['startDate'] today_str = datetime.datetime.strftime(datetime.datetime.now(), helper.DATE_FORMAT) if request.form['endDate']: end_date = datetime.datetime.strptime(request.form['endDate'], helper.DATE_FORMAT) future_appt_filter = helper.dept_appts( user=current_user, direction="future", period=(end_date - start_date).days, start_date=start_date).filter( Application.status == StatusEnum.approved) if start_date_str == today_str: nowTime = datetime.datetime.strftime(datetime.datetime.today(), helper.TIME_FORMAT) future_appts = future_appt_filter.filter( Application.time >= nowTime).all() else: future_appts = future_appt_filter.all() else: future_appt_filter = helper.dept_appts( user=current_user, direction="future", start_date=start_date).filter( Application.status == StatusEnum.approved) if start_date_str == today_str: nowTime = datetime.datetime.strftime(datetime.datetime.today(), helper.TIME_FORMAT) future_appts = future_appt_filter.filter( Application.time >= nowTime).all() else: future_appt_filter.all() helper.load_id2name_map() def response_generator(app): return { "appID": app.id, "date": app.date.strftime(helper.DATE_FORMAT), "time": app.time.strftime(helper.TIME_FORMAT), "doctor": helper.id2name(app.doctor_id), "patient": helper.id2name(app.patient_id), "symptoms": app.symptoms } return make_response( jsonify([response_generator(app) for app in future_appts]), 200)
def register(): """ patient: register by (national)id doctor/nurse: register by (license)id """ if current_user.is_authenticated: return redirect(url_for('loadHomePage')) id = request.form['id'] # check if the user exists if User.query.filter_by(id=id).first() != None: return make_response( jsonify({ 'ret': 1, 'message': 'You already registered!' })) role = request.form['role'] first_name = request.form['firstName'] last_name = request.form['lastName'] phone = request.form['phone'] email = request.form['email'] password = request.form['password'] if role == "nurse" or role == "doctor": department = request.form['department'] user = User(id=id, first_name=first_name, last_name=last_name, role=role, email=email, phone=phone, password_hash=generate_password_hash(password)) try: db.session.add(user) # update corresponding table if role == "patient": patient = Patient(id=id) db.session.add(patient) elif role == "doctor": doctor = Doctor(id=id, department_id=department) db.session.add(doctor) elif role == "nurse": nurse = Nurse(id=id, department_id=department) db.session.add(nurse) db.session.commit() # update roster cache helper.load_id2name_map(True) return make_response(jsonify({"ret": 0, 'message': ""}), 200) except: db.session.rollback() return make_response(jsonify({'ret': 1, 'message': "error"}))
def patientFutureAppt(): if not helper.check_patient_privilege(): return redirect("/login") n_offset, n_tot_records, n_tot_page, page_count = helper.paginate( Application) patientID = current_user.get_id() total_number = len( Application.query.filter( Application.patient_id == patientID, Application.status == StatusEnum.approved, Application.date + Application.time >= datetime.datetime.today()).order_by(Application.date.desc(), Application.time.desc()).all()) apps = Application.query.filter( Application.patient_id == patientID, Application.status == StatusEnum.approved, Application.date + Application.date >= datetime.datetime.today()).order_by( Application.date.desc(), Application.time.desc()).offset(n_offset).limit(page_count).all() helper.load_id2name_map() return make_response( jsonify({ 'total_number': total_number, "apps": [{ "appID": app.id, "date": app.date.strftime(helper.DATE_FORMAT), "time": app.time.strftime(helper.TIME_FORMAT), "hospital": Hospital.query.filter(Hospital.id == helper.user2hosp( app.doctor_id, "doctor")).first().name, "department": helper.user2dept_name(app.doctor_id, "doctor"), "nurse": helper.id2name(app.approver_id), "patient": helper.id2name(app.patient_id), "doctor": helper.id2name(app.doctor_id), "symptoms": app.symptoms, } for app in apps] }))
def viewDoctorByID(doctorID): doctor = Doctor.query.join(Department, Doctor.department_id == Department.id).\ join(Hospital,Department.hospital_id == Hospital.id).filter(Doctor.id == doctorID).first() helper.load_id2name_map() doctorName = id2name(doctorID) hospital = doctor.department.hospital.name department = doctor.department.title return render_template('doctorPage.html', doctorID=doctorID, doctorName=doctorName, hospital=hospital, department=department)
def doctorTodayAppt(): if not helper.check_doctor_privilege(): return redirect("/login") doctorID = current_user.get_id() appt_list = helper.doc2appts(doctorID, 0) helper.load_id2name_map() return make_response( jsonify([{ "appID": str(appt_list[i].id), "date": appt_list[i].date.strftime(helper.DATE_FORMAT), "time": appt_list[i].time.strftime(helper.TIME_FORMAT), "nurse": helper.id2name(appt_list[i].approver_id), "patient": helper.id2name(appt_list[i].patient_id), "symptoms": appt_list[i].symptoms } for i in range(len(appt_list))]))
def doctorFutureAppt(): if not helper.check_doctor_privilege(): return redirect("/login") if request.method == "POST": start_date = request.form["startDate"] end_date = request.form["endDate"] elif request.method == "GET": start_date = 0 end_date = 0 doctorID = current_user.get_id() # if start date is given, use it directly; o.w. set it to today if start_date == datetime.datetime.today().strftime(helper.DATE_FORMAT): filter = True if start_date: start_date = datetime.datetime.strptime(start_date, helper.DATE_FORMAT) else: start_date = datetime.datetime.today() # similar to start date if end_date: end_date = datetime.datetime.strptime(end_date, helper.DATE_FORMAT) if end_date: apps = helper.doc2appts(doctorID, period=(end_date - start_date).days, start_date=start_date) else: apps = helper.doc2appts(doctorID, start_date=start_date, limit='no') if filter: for i in range(len(apps)): if datetime.datetime.combine( apps[i].date, apps[i].time ) < datetime.datetime.now() - timedelta(minutes=30): apps.pop(i) helper.load_id2name_map() return make_response( jsonify([{ "appID": app.id, "date": app.date.strftime(helper.DATE_FORMAT), "time": app.time.strftime(helper.TIME_FORMAT), "nurse": helper.id2name(app.approver_id), "patient": helper.id2name(app.patient_id), "symptoms": app.symptoms, } for app in apps]))
def login(): """ patient login with: national id + password doctor/patient login with: license id + password """ if request.method == 'GET': if current_user.is_authenticated: helper.load_id2name_map(True) return redirect(url_for('loadHomePage')) return render_template('login.html') if request.method == 'POST': if not current_user.is_authenticated: id = request.form['id'] password = request.form['password'] try: user = User.query.get(id) if not user: return make_response( jsonify({ "ret": 1, "message": "Unregistered user" })) if not user.check_password(password): return make_response( jsonify({ "ret": 1, "message": "Incorrect password" })) login_user(user) # update roster helper.load_id2name_map(True) except: return make_response( jsonify({ "ret": 1, "message": "Unknown error" })) return make_response( jsonify({ "ret": 0, "role": current_user.role.value, "id": current_user.id }))
def doctorGoViewAppt(appID): if not helper.check_doctor_privilege(): return redirect("/login") appt_res = Application.query.filter(Application.id == appID).first() finished = False if appt_res.status.value == "finished": finished = True helper.load_id2name_map() return render_template('doctorViewAppt.html', appID=appt_res.id, date=appt_res.date.strftime(helper.DATE_FORMAT), time=appt_res.time.strftime(helper.TIME_FORMAT), approverID=appt_res.approver_id, patientID=appt_res.patient_id, patient=helper.id2name(appt_res.patient_id), symptoms=appt_res.symptoms, comments=appt_res.reject_reason, mcID=appt_res.mc_id, finished=finished)
def doctorPastAppt(): if not helper.check_doctor_privilege(): return redirect("/login") if request.method == "POST": start_date = request.form["startDate"] end_date = request.form["endDate"] elif request.method == "GET": start_date = 0 end_date = 0 doctorID = current_user.get_id() if start_date: start_date = datetime.datetime.strptime(start_date, helper.DATE_FORMAT) if end_date: end_date = datetime.datetime.strptime(end_date, helper.DATE_FORMAT) else: end_date = datetime.date.today() if start_date: apps = helper.doc2appts(doctorID, period=(end_date - start_date).days, direction='past', start_date=end_date) else: apps = helper.doc2appts(doctorID, start_date=start_date, direction='past', limit='no') helper.load_id2name_map() return make_response( jsonify([{ "appID": app.id, "date": app.date.strftime(helper.DATE_FORMAT), "time": app.time.strftime(helper.TIME_FORMAT), "nurse": helper.id2name(app.approver_id), "patient": helper.id2name(app.patient_id), "symptoms": app.symptoms, } for app in apps]))
def doctorOnGoingAppt(): if not helper.check_doctor_privilege(): return redirect("/login") doctorID = current_user.get_id() nowtime = datetime.datetime.now() appt_list = helper.doc2appts(doctorID, 0) now_approved_appts = [] for appt in appt_list: appt_date_time = datetime.datetime.combine(appt.date, appt.time) if appt_date_time <= nowtime <= appt_date_time + timedelta(minutes=30): now_approved_appts.append(appt) helper.load_id2name_map() return make_response( jsonify([{ "appID": appt.id, "date": appt.date.strftime(helper.DATE_FORMAT), "time": appt.time.strftime(helper.TIME_FORMAT), "nurse": helper.id2name(appt.approver_id), "patient": helper.id2name(appt.patient_id), "symptoms": appt.symptoms } for appt in now_approved_appts]))
def nurseTodayAppt(): if not helper.check_nurse_privilege(): return redirect("/loadHomePage") nurseID = current_user.get_id() # department ID of current nurse today_depts_appts = helper.dept_appts( user=current_user, period=0).filter(Application.status == StatusEnum.approved).all() helper.load_id2name_map() def response_generator(app): return { "appID": app.id, "date": app.date.strftime(helper.DATE_FORMAT), "time": app.time.strftime(helper.TIME_FORMAT), "doctor": helper.id2name(app.doctor_id), "patient": helper.id2name(app.patient_id), "symptoms": app.symptoms } return make_response( jsonify([response_generator(app) for app in today_depts_appts]), 200)
def getPatientRecord(): if not helper.check_patient_privilege(): return redirect("/login") type = request.args.get('type') if type == "appointment": n_offset, n_tot_records, n_tot_page, page_count = helper.paginate( Application) patientID = current_user.get_id() total_number = len( Application.query.filter( Application.patient_id == patientID).order_by( Application.date.desc(), Application.time.desc()).all()) apps = Application.query.filter( Application.patient_id == patientID).order_by( Application.date.desc(), Application.time.desc()).offset( n_offset).limit(page_count).all() helper.load_id2name_map() return make_response( jsonify({ 'total_number': total_number, "apps": [ { "appID": app.id, "date": app.date.strftime(helper.DATE_FORMAT), "time": app.time.strftime(helper.TIME_FORMAT), "hospital": Hospital.query.filter(Hospital.id == helper.user2hosp( app.doctor_id, "doctor")).first().name, "department": helper.user2dept_name(app.doctor_id, "doctor"), "doctor": helper.id2name(app.doctor_id), "status": app.status.value, # "nurse": helper.id2name(app.approver_id) if app.approver_id else "", # "patient": helper.id2name(app.patient_id), # "symptoms": app.symptoms, # "reject_reason":app.reject_reason, } for app in apps ] })) elif type == "medical_record": n_offset, n_tot_records, n_tot_page, page_count = helper.paginate( Medical_record) patientID = current_user.get_id() apps = Application.query.filter( Application.patient_id == patientID, Application.status == StatusEnum.finished).order_by( Application.date.desc(), Application.time.desc()).offset( n_offset).limit(page_count).all() mcs = [ Medical_record.query.filter( Medical_record.id == app.mc_id).first() for app in apps ] helper.load_id2name_map() return make_response( jsonify({ 'total_number': len(mcs), "mcs": [{ "mcID": mcs[i].id, "appID": apps[i].id, "date": apps[i].date.strftime(helper.DATE_FORMAT), "time": apps[i].time.strftime(helper.TIME_FORMAT), "doctor": helper.id2name(apps[i].doctor_id), "hospital": Hospital.query.filter(Hospital.id == helper.user2hosp( apps[i].doctor_id, "doctor")).first().name, "department": helper.user2dept_name(apps[i].doctor_id, "doctor"), } for i in range(len(mcs))] }))