def activation_token(token): #You should check if user is logged in MAYBE user = User.getUserByEmail(User.verify_user_token(token)) if user is None: pass #You should print a message saying token is not valid or expired UserHandler.activateAccount(user) return redirect('https://the-covid-tracker.herokuapp.com/')
def notifyNearbyUsers(json, user): if(json['test_status']==3): visited_locations = VisitedLocation.getLocationsVisitedByUserId(json['patient_id']) for patient_visited_location in visited_locations: patient_location = Utilities.to_dict(Location.getLocationById(patient_visited_location.location_id)) patient_location['lattitude'] = float("{:.14f}".format(patient_location['lattitude'])) patient_location['longitude'] = float("{:.14f}".format(patient_location['longitude'])) locations_within_1km = Location.getLocationsWithinOneKilometerRadius(patient_location) for close_location in locations_within_1km: locations_in_danger = VisitedLocation.getVisitedLocationByLocationId(close_location.location_id) for user_visited_location in locations_in_danger: user_in_danger = Utilities.to_dict(User.getUserById(user_visited_location.user_id)) if user_in_danger['user_id'] != user['user_id'] and patient_visited_location.date_visited == user_visited_location.date_visited: msg = Message('Possible COVID-19 Contact', sender='*****@*****.**', recipients=[user_in_danger['email']]) msg.body = f'''Hi {user_in_danger['full_name']}, An indivual that tested positive to COVID-19 visited location @ lattitude: {patient_location['lattitude']}, @longitude: {patient_location['longitude']}. in the day of {patient_visited_location.date_visited}. It looks like you visited a location within 1 km of distance, so you might have been exposed to the COVID-19. If you don't feel well in the following days, get tested. For the offices that provides COVID-19 test, please check our website.''' mail.send(msg)
def updateRecord(json): valid_parameters = Utilities.verify_parameters(json, CovidCases.REQUIRED_PARAMETERS) if valid_parameters: try: updatedInfo = CovidCases.updateCovidStatus(json) result = { "message": "Success!", "case": Utilities.to_dict(updatedInfo) } user = Utilities.to_dict(User.getUserById(json['patient_id'])) office = Utilities.to_dict(MedicalOffice.getMedicalOfficeById(json['office_id'])) statuses = {2: 'negative', 3: 'positive'} msg = Message('COVID-19 Test Result', sender='*****@*****.**', recipients=[user['email']]) msg.body = f'''Hi {user['full_name']}, Your tested {statuses[json['test_status']]} to the COVID-19. If you want to know more info about your COVID-19 test, please call {office['office_phone_number']}. ''' mail.send(msg) CovidCasesHandler.notifyNearbyUsers(json, user) return jsonify(result), 200 except Exception as e: return jsonify(reason="Server error", error=e.__str__()), 500
def getUserById(uid): try: user = User.getUserById(uid) user_dict = Utilities.to_dict(user) result = {"message": "Success!", "user": user_dict} return jsonify(result), 200 except Exception as e: return jsonify(reason="Server error", error=e.__str__()), 500
def getAllUsers(): try: users = User.getAllUsers() result_list = [] for user in users: result_list.append(Utilities.to_dict(user)) result = {"message": "Success!", "users": result_list} return jsonify(result), 200 except Exception as e: return jsonify(reason="Server error", error=e.__str__()), 500
def updateUserInfo(json): valid_parameters = Utilities.verify_parameters( json, ['user_id', 'email', 'phone_number', 'password', 'address_id']) if valid_parameters: try: email_exists = User.getUserByEmail( json['email']) and User.user_id == json['user_id'] if email_exists: return jsonify(message="Email already in use."), 400 updatedInfo = User.updateUserInfo(**valid_parameters) result = { "message": "Success!", "user": Utilities.to_dict(updatedInfo) } return jsonify(result), 200 except Exception as e: return jsonify(reason="Server error", error=e.__str__()), 500
def getUserByEmail(email): try: user = User.getUserByEmail(email) if user: user_dict = Utilities.to_dict(user) result = {"message": "Success!", "user": user_dict} return jsonify(result), 200 else: return jsonify(reason="User does not exist."), 401 except Exception as e: return jsonify(reason="Server error", error=e.__str__()), 500
def createUser(json): valid_params = Utilities.verify_parameters(json, User.REQUIRED_PARAMETERS) if valid_params: try: email_exists = User.getUserByEmail(json['email']) if email_exists: return jsonify( message="Email already taken. Please use another one." ), 400 created_user = User(**valid_params).create() user_dict = Utilities.to_dict(created_user) result = { "message": "Success!", "user": user_dict, } return jsonify(result), 201 except Exception as err: return jsonify(message="Server error!", error=err.__str__()), 500 else: return jsonify(message="Bad Request!"), 40
def login(json): try: if json['email'] == "" or json['password'] == "": return jsonify( reason="Must fill both email and password fields."), 400 user = User.getUserByEmail(json['email']) user_dic = Utilities.to_dict(user) if user.active == False: result = { "message": "Inactive Account", } return jsonify(result), 200 if user and user.password == json['password']: session['logged_in'] = True result = {"message": "Success!", "user": user_dic} return jsonify(result), 200 else: return jsonify(reason="Incorrect email or password."), 401 except Exception as e: return jsonify(reason="Server error", error=e.__str__()), 500
def createDoctor(json): valid_params = Utilities.verify_parameters(json, Doctor.REQUIRED_PARAMETERS) if valid_params: try: user_exists = User.getUserById(json['user_id']) if not user_exists: return jsonify( message= "The doctor you are trying to register doesn't have an account." ), 400 created_doctor = Doctor(**valid_params).create() doctor_dict = Utilities.to_dict(created_doctor) result = { "message": "Success!", "doctor": doctor_dict, } return jsonify(result), 201 except Exception as err: return jsonify(message="Server error!", error=err.__str__()), 500 else: return jsonify(message="Bad Request!"), 40
def activation_request(): if(request.method == "POST"): json = request.json user = User.getUserByEmail(json['email']) send_activation_email(user) return UserHandler.sentEmail()
def activateAccount(user): try: User.activateUser(user) except Exception as err: return jsonify(message="Server error!", error=err.__str__()), 500 return jsonify(status='Success!'), 200