def get(cls, patient_id): if get_jwt_claims()["type"] != "patient": patient = PatientModel.find_by_id(patient_id) if patient: return patient.json_with_info() return {"message": "User not found"}, 404 return {"message": "You have to be an admin or doctor"}
def get(cls): identity = get_jwt_identity() claims = get_jwt_claims() if claims["type"] == "doctor": doctor_appointments = DoctorModel.find_by_id(identity).appointments doctorapp = [ appointment.json() for appointment in doctor_appointments ] return doctorapp, 200 elif claims["type"] == "patient": patient_appointments = PatientModel.find_by_id( identity).appointments patientapp = [ appointment.json() for appointment in patient_appointments ] return patientapp else: appointments = AppointmentModel.find_all() appointments_list = [ appointment.json() for appointment in appointments ] return appointments_list, 200
def delete(self, name): """Delete patient from the patient's database.""" patient = PatientModel.findPatient(name) if patient: patient.deletePatient() return {'message': 'Patient: {} removed from the database' .format(name)}, 200 return {'message': 'Patient with name {}, not in our database'.format(name)}, 404
def get(cls): if get_jwt_claims()["type"] != "doctor": return {"message": "You must be a doctor"} identity = get_jwt_identity() results = PatientModel.find_by_doctor(identity) result_list = [result.json() for result in results] return result_list, 200
def delete(cls, patient_id): if get_jwt_claims()["type"] == "admin": patient = PatientModel.find_by_id(patient_id) if patient: patient.delete_from_db() return {"message": "User deleted"}, 200 return {"message": "User not found"}, 404 return {"message": "Admin authorization required."}
def post(self, patient_id): if get_jwt_claims()["type"] == "patient": {"message": "Invalid authorization"}, 401 if not PatientModel.find_by_id(patient_id): return {"message": "A patient with this id does not exist"}, 404 data = request.files print(request.files) if type(data["image"]) != FileStorage: return {"message": "Invalid data."} image_path = image_helper.save_image(data["image"], folder=f"patient_{patient_id}") basename = image_helper.get_basename(image_path) return {"message": "image uploaded"}, 201
def get(self, patient_id): if get_jwt_claims()["type"] == "patient": return { "message": "Invalid authorization: you must be a doctor or an admin." }, 401 if not PatientModel.find_by_id(patient_id): return {"message": "A patient with this id does not exist"} folder = os.getcwd() dirs = os.listdir(f"{folder}/static/images/patient_{patient_id}") file_list = [] for file in dirs: file_list.append({ "image": f"http://localhost:5000/static/images/patient_{patient_id}/{file}" }) return file_list
def post(cls): claims = get_jwt_claims() if claims["type"] != "patient": return {"message": "Access denied"} data = cls.appointment_parser.parse_args() identity = get_jwt_identity() if data["date"].isspace(): return {"message": "One of the inputs is empty"}, 400 data["patient_id"] = identity data['patient_username'] = PatientModel.find_by_id(identity).username data["doctor_id"] = int(data["doctor_id"]) data['doctor_username'] = DoctorModel.find_by_id( data['doctor_id']).username doctor = DoctorModel.find_by_id(data["doctor_id"]) if not doctor: return {"message": "Doctor not found"}, 404 data["created_at"] = datetime.now().date() y1, m1, d1 = [int(x) for x in data["date"].split("-")] app_date = datetime(y1, m1, d1).date() if app_date < data["created_at"]: return {"message": "Invalid date"} apps_date = AppointmentModel.find_by_date(app_date) for app in apps_date: if app.patient_id == identity: return { "message": "Appointment already exists at the same date" } AppointmentModel.main(app_date) appointment = AppointmentModel(**data) appointment.save_to_db() return {"message": "Appointment created successfully."}, 201
def delete(self, patient_id): if get_jwt_claims()["type"] != "admin": return { "message": "Invalid authorization, you have to be an admin." }, 401 if not PatientModel.find_by_id(patient_id): return {"message": "A patient with this id does not exist"}, 404 filename = request.args.get("filename") folder = os.getcwd() dirs = os.listdir(f"{folder}/static/images/patient_{patient_id}") for file in dirs: if filename == file: os.remove( image_helper.get_path( file, folder=f"{folder}/static/images/patient_{patient_id}")) return {"message": "file deleted"} return {"message": "file not found"}, 404
def post(cls): data = cls.patient_parser.parse_args() patient = PatientModel.find_by_username(data["username"]) if patient and check_password_hash(patient.password, data["password"]): access_token = create_access_token( identity=patient.id, fresh=True, user_claims={"type": "patient"}, expires_delta=timedelta(1), ) refresh_token = create_refresh_token( identity=patient.id, user_claims={"type": "patient"}) return { "access_token": access_token, "refresh_token": refresh_token }, 200 return {"message": "Invaild credentials"}, 401
def post(self, name): """Will post data to the database.""" if PatientModel.findPatient(name): return {'message': 'Patient with name {}, already in our database'.format(name)}, 400 dataInput = Patient.parser.parse_args() patient = PatientModel(name, dataInput['sex'], dataInput['age'], dataInput['race'], dataInput['clinic_id']) try: patient.insertPatient() except: return {'message': 'Error occured during insertion'}, 500 return patient.json(), 201
def post(self): data = PatientRegister.patient_parser.parse_args() if (data["username"].isspace() or data["password"].isspace() or data["address"].isspace() or data["mobile"].isspace() or data["email"].isspace() or data["first_name"].isspace() or data["last_name"].isspace()): return {"message": "One of the inputs is empty"}, 400 if len(data["username"]) < 5: return {"message": "Username is too short"}, 400 if PatientModel.find_by_username(data["username"]): return {"message": "A user with that username already exists"}, 400 if PatientModel.find_by_email(data["email"]): return {"message": "A user with that email already exists"}, 400 y, m, d = [int(x) for x in data["birthdate"].split("-")] data["birthdate"] = datetime(y, m, d).date() if ((datetime.now().date() - data["birthdate"]).days // 365) < 1: return {"message": "Invalid age"}, 400 data["gender"] = int(data["gender"]) if data["gender"] != 0 and data["gender"] != 1: return { "message": "Invalid request: gender is only '0' if male or '1' if female" } data["created_at"] = datetime.now().date() patient = PatientModel(**data) patient.save_to_db() return {"message": "User created successfully."}, 201
def parent_id_exists(path_id): """Check if patient with id=path_id exists in the DB""" if not PatientModel.find_by_id(path_id): raise ValidationError( 'Patient with id={} does not exist.'.format(path_id))
def put(self, name): """Update the table.""" dataget = Patient.parser.parse_args() patient = PatientModel.findPatient(name) if patient is None: patient = PatientModel(name, dataget['sex'], dataget['age'], dataget['race'], dataget['clinic_id']) else: patient.name = name patient.sex = dataget['sex'] patient.age = dataget['age'] patient.race = dataget['race'] patient.clinic_id = dataget['clinic_id'] patient.insertPatient() return patient.json(), 201
def get(self, name): """Define method on the resource i.e get.""" patient = PatientModel.findPatient(name) if patient: return patient.json(), 200 return {'message': 'Patient {} not found in our patient\'s database'.format(name)}, 404
def get(cls): if get_jwt_claims()["type"] == "admin": patients = PatientModel.find_all() patients_list = [patient.json() for patient in patients] return patients_list, 200 return {"message": "Authorization required."}