def __init__(self): self.__key_model = KeyModel() self.__patient_model = PatientModel() # temporary parameters for patient information self.__patient_id = None self.__patient_name = None self.__patient_prescription = None self.__timestamps = None
def post(self): string_req = request.get_data() data = json.loads(string_req) new_patient = PatientModel(data['firstname'], data['lastname'], parse_date(data['date_birth']), data['birth_place'], data['tax_code'], is_hospitalized=False) new_patient.add_to_db() return {'message': 'Patient Successfully Inserted'}, 200
def get(self): user_id = get_jwt_identity() all_patients = [patient.json() for patient in PatientModel.find_all()] if user_id: return json.dumps({'patients': all_patients}) return json.dumps({'message': 'Invalid credentials'}), 401
def createdb(testdata=True): """Initializes the database """ app = create_app() with app.app_context(): db.drop_all() db.create_all() if testdata: patient = PatientModel(name='test') db.session.add(patient) db.session.commit()
def delete(self, id): claims = get_raw_jwt()['jti'] if not claims['is_admin']: return jsonify({"message", "Admin required"}) patient = PatientModel.find_id(id=id) if patient: patient.delete_from_db() return jsonify({'message': 'Patient Deleted Successfully'}), 200 return jsonify({'messase': f'Patient {patient.firstname} not found'}), 404
def dashboard_add_patient(): if request.method == 'POST': name = request.form.get('name') email = request.form.get('email') age = request.form.get('age') national_id = request.form.get('national_id') medical_history = request.form.get('medical_history') phone_num = request.form.get('phone_num') Address = request.form.get('Address') gender = request.form.get('gender') enter = str(request.form.get('enter')) imagesList = request.files.getlist("inputFile") check = PatientModel.query.filter_by(national_id=national_id).first() if check: # if a patient already exists by its id, we flash a msg that he is already in the database. flash('Patient with same ID already exists.') return redirect(url_for('auth.dashboard_control_form')) patient = PatientModel(name=name, email=email, age=age, national_id=national_id, medical_history=medical_history, phone_num=phone_num, Address=Address, gender=gender, enter=enter, action="No Action added yet.") db.session.add(patient) db.session.commit() folder_path = os.path.join("static", "images", str(patient.id)) os.mkdir(folder_path) for image in imagesList: newFile = FileModel(name=os.path.join(folder_path, image.filename), patient_id=patient.id) image.save(os.path.join(folder_path, image.filename)) db.session.add(newFile) db.session.commit() flash(f"{patient.name} has been successfully added to the system!") return render_template('dashboard-control.html') else: return render_template('dashboard-control.html')
def post(cls, patient_id: int): data_string = request.get_data() data = json.loads(data_string) patient = PatientModel.find_by_id(patient_id) if patient.medical_records: return json.dumps({"message": "Medical Records already exists"}) new_medical_records = MedicalRecordsModel(data["date_admission"], data["first_diagnosis"], data["last_diagnosis"], data["date_discharge"]) patient.medical_records = new_medical_records patient.add_to_db() return json.dumps({"message": "Medical Records Successfully Created"})
class PillPresenter: def __init__(self): self.__key_model = KeyModel() self.__patient_model = PatientModel() # temporary parameters for patient information self.__patient_id = None self.__patient_name = None self.__patient_prescription = None self.__timestamps = None @property def patient_id(self): return self.__patient_id @patient_id.setter def patient_id(self, value): self.__patient_id = value @property def patient_prescription(self): return self.__patient_prescription @patient_prescription.setter def patient_prescription(self, value): self.__patient_prescription = value @staticmethod def __retrieve_components_combination( data): # retrieve unique combinations combination = set( ) # empty set for saving unique data (10,10,10) => (10) for row in data: row_combination = row["Does_not_Combine_With"] combination.update(row_combination.split( ',')) # "10,10,10" => [10,10,10] insert data to set return combination @staticmethod def __refactor_day_number( prescription): # convert from database day to python format for i in range(len(prescription)): day_id = prescription[i]["Day_Id"] prescription[i]["Day_Id"] = day_id - 2 if day_id != 1 else 6 return prescription @staticmethod def __add_obtained_prescription_field(prescription): updated_prescription = [] for row in prescription: row.update({"obtained": False }) # add field to check if the pill is obtained or not updated_prescription.append(row) return updated_prescription def get_patients_ids(self): # get patients ids from model data = self.__patient_model.get_ids() return data def validate_safe_pills_combinations(self): data = self.__patient_model.get_combinations(self.patient_id) combination = self.__retrieve_components_combination( data) # create list of unique combinations for row in data: if row["Active_component"] in combination: return False return True def store_patient_prescription( self): # get and store prescription in the temporary database prescription = self.__patient_model.get_prescription(self.patient_id) prescription = self.__refactor_day_number( prescription) # convert from database day to python format prescription = self.__add_obtained_prescription_field( prescription ) # add obtaining key to check if patient took the pill self.patient_prescription = prescription def save_patient_name(self, patient_id): self.__patient_name = self.__patient_model.get_name_by_id(patient_id)[ 0] # store patient name in the temporary db. def retrieve_keys( self ): # returns list of valid keys from prescription from temporary database return [obtain["Cell_id"] for obtain in self.patient_prescription] def validate_input_key(self, key): # return true or false if the key is valid prescription_keys = self.retrieve_keys( ) # returns list of valid keys from prescription from temporary database if key not in prescription_keys: print("You've pressed an invalid key") return False return True @staticmethod def __retrieve_time_borders(prescription): current_time = datetime.now() current_timestamp = datetime.strptime( f"{current_time.weekday() + 1}:{current_time.hour}:{0 if current_time.minute < 10 else ''}" f"{current_time.minute}", '%d:%H:%M') # 0:8:30 (python format) => Monday, 8:30 AM prescription_timestamp = datetime.strptime( f"{prescription['Day_Id'] + 1}:{prescription['Hour_Id']}", '%d:%H:%M') # cast from string to datetime (in python) border_timestamp = prescription_timestamp + timedelta( minutes=WAITING_SECONDS / 60) # adds 30 minutes to border timestamp return current_timestamp, prescription_timestamp, border_timestamp def validate_input_key_timing(self, key): prescription_by_key = list( filter(lambda row: row['Cell_id'] == key, self.patient_prescription))[ 0] # get obtaining by it's pressed key message_time = f"{calendar.day_name[int(prescription_by_key['Day_Id'])]} at {prescription_by_key['Hour_Id']}." # create string of the time of obtaining if prescription_by_key[ 'obtained'] is True: # if patient pressed the same key self.send_alert( message= f"obtained the same pill from box number {prescription_by_key['Box_Id']}" f" originally obtained on {message_time}") return False current_timestamp, prescription_timestamp, border_timestamp = self.__retrieve_time_borders( prescription_by_key) if not prescription_timestamp < current_timestamp < border_timestamp: # if the current time is not between 8:30 to 9:00, border - time limit for obtain self.send_alert( message= f"obtained a pill not at the right timing. {self.__patient_name} " f"should've obtained it on {message_time}") return False return True def update_obtaining(self, key): for i in range(len(self.patient_prescription)): if self.patient_prescription[i][ "Cell_id"] == key: # update obtaining to true in temporary database self.patient_prescription[i]["obtained"] = True def __extract_timestamps( self): # build from prescription time - a timestamp => 1:20:30 prescription = self.patient_prescription self.__timestamps = [ f"{row['Day_Id']}:{row['Hour_Id']}" for row in prescription ] # create list of timestamps def __is_prescription_time_passed( self, timestamp): # checks if it's now the last obtaining time last_prescription_time = self.__timestamps[ len(self.__timestamps) - 1] # takes the last timestamp from temporary db return last_prescription_time == timestamp def __is_obtained_on_time(self, day, hour_with_min): # checks if obtain pill on time current_prescription = list( filter( lambda row: row["Day_Id"] == day and row["Hour_Id"] == hour_with_min, self.patient_prescription))[ 0] # filter the obtaining for the current time. if not current_prescription[ "obtained"]: # checks if obtain is true or false return False, current_prescription return True, current_prescription def assurance_listener( self ): # checks if at the end of obtaining time (8:30 => 9:00) patient didn't take a pill self.__extract_timestamps() while True: current_time = datetime.now() # gets the time right now timestamp = f"{current_time.weekday()}:{current_time.hour}:{0 if current_time.minute < 10 else ''}" \ f"{current_time.minute}" # Monday 8:30 => 0:8:30 if timestamp in self.__timestamps: # checks if it is time to take pill time.sleep( WAITING_SECONDS ) # the thread waiting 30 minutes until pill time limit. day, hour_with_min = timestamp.split( ':', 1) # => day = 0, hour_with_min = 8:30 obtained, current_prescription = self.__is_obtained_on_time( day, hour_with_min) if not obtained: self.send_alert( message= f"did not obtain a pill on {calendar.day_name[int(day)]}," f" {hour_with_min}.") # on Monday, 8:30 self.__store_obtained( obtained, current_prescription['Collect_Id'] ) # update Collect table with is_obtained (true/false) if self.__is_prescription_time_passed( timestamp ): # checks if obtaining is the last row in database. break time.sleep(5) def __store_obtained(self, is_obtained, collect_id): self.__patient_model.update_prescription_obtain( is_obtained, collect_id) def __send_alert(self, message): phone_number = f"{ISRAEL_CALLING_CODE}{self.patient_prescription[0]['Phone_number'][1:]}" # build valid international phone number sns.publish(PhoneNumber=phone_number, Message=message) def send_alert(self, message): sms_template = f"Hello {self.patient_prescription[0]['First_Name']}, your patient - {self.__patient_name}, " # insert variables to string self.__send_alert(sms_template + message)
def test_patient(self): date = datetime.date(2020, 2, 3) patient = PatientModel('francesco', 'lastname', date) self.assertEqual(patient.firstname, 'francesco') self.assertEqual(patient.lastname, 'lastname')