-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport_sickness.py
155 lines (137 loc) · 7 KB
/
report_sickness.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import datetime
from flask import request
from flask_api import status
from utilities import make_response, unobfuscate, check_for_values_in_request, try_for_datetime_from_string, send_message
from database_access_helpers import (report_contact_between_individuals,
report_visited_place,
retrieve_or_create_person_from_identifier,
retrieve_or_create_place_from_lat_and_long,
update_person_statistics)
def isCellPhone(supposedNumber):
#check if it is an int of 10 digits
try:
int(supposedNumber)
except:
return False
if (len(supposedNumber) == 10):
return True
else:
return False
def reportSicknessSum(r):
#Step 1, Check to make sure all parameters are there
content = request.get_json()
necessary_values = ['identifier', 'test_status', 'test_date', 'symptoms_date', 'symptoms', 'additional_info', 'contacted_individuals', 'visited_locations']
isGoodRequest = check_for_values_in_request(necessary_values, content)
if (isGoodRequest[0] == False):
return make_response({'error': 'bad request, please try again and specify the ' + str(isGoodRequest[1]) + ' parameter in the JSON request body.'}, status.HTTP_400_BAD_REQUEST)
identifier = content['identifier']
test_status = content['test_status']
test_date = content['test_date']
symptoms_date = content['symptoms_date']
symptoms = content['symptoms']
additional_info = content['additional_info']
contacted_individuals = content['contacted_individuals']
visited_locations = content['visited_locations']
#Step 2, Check to make sure all parameters are properly formatted/Valid, and then report those parameters to the DB
push_id = unobfuscate(identifier, r)
if (push_id == None or identifier == None):
return make_response({'response': '\'identifier\' is invalid. Not Part of our database'}, status.HTTP_400_BAD_REQUEST)
sender = retrieve_or_create_person_from_identifier(identifier)
update_person_statistics(sender, test_status, test_date, symptoms_date, symptoms, additional_info)
#Step 3, report contacts
for individual in contacted_individuals:
#3.A See that necessary variables exist
try:
contacted_identifier = individual["identifier"]
except:
contacted_identifier = None
try:
contact_time = individual['contact_time']
except:
contact_time = None
#If contact time is not valid, change it to current time as approximation
contact_time = try_for_datetime_from_string(contact_time)
#If contacted_identifier is valid, and it is not a cell phone number (there is a registered account),
#then get the push notification code.
if (contacted_identifier != None):
push_notification_code = unobfuscate(contacted_identifier, r)
else:
push_notification_code = None
#Now, do processing for this individual number based on what data is available
#Check if push notification code is actually a cell phone number
if (push_notification_code != None):
is_cell_phone = isCellPhone(push_notification_code)
else:
is_cell_phone = False
if ((contacted_identifier == None or push_notification_code == None) and is_cell_phone == False):
#Simply an invalid code
print("skipping this number")
elif (is_cell_phone == True):
#DO THE PROCESSING FOR CELLULAR DEVICES
print("do cell phone number processing")
message = "A user of the Exposure App, who you have had contact with, has reported a change in their COVID-19 status. Download Exposure from getmyexposure.com to see your risk"
phone_number = str(push_notification_code)[1:]
successful_message = send_message(message, phone_number)
if (successful_message == False):
print("failed to send text message")
else:
#Do the process for push notifications
contacted_individual = retrieve_or_create_person_from_identifier(contacted_identifier)
#report to the database that the two individuals contacted
report_contact_between_individuals(sender, contacted_individual, contact_time)
#now, send the push notification to the contacted_individual:
true_code = str(push_notification_code)[1:]
print(true_code)
message = "We have a problem to report with your coronavirus exposure."
try:
send_push_message(true_code, message)
except:
print("failed to send push notification for some reason")
#Step 4, Report and Store Locations
for location in visited_locations:
#4.A See that necessary variables exist
try:
latitude = location["latitude"]
except:
latitude = None
try:
longitude = location["longitude"]
except:
longitude = None
try:
contact_time = location['contact_time']
except:
contact_time = None
#If contact time is not valid, change it to current time as approximation
contact_time = try_for_datetime_from_string(contact_time)
#If contacted_identifier is valid, and it is not a cell phone number (there is a registered account),
#then get the push notification code.
#Now, do processing for this individual number based on what data is available
if (latitude == None or longitude == None):
print("skipping this location")
else:
contacted_location = retrieve_or_create_place_from_lat_and_long(latitude, longitude)
#report to the database that the location was visited
report_visited_place(sender, contacted_location, contact_time)
return make_response({'response': 'We sent the Push Notifications!!!'}, status.HTTP_200_OK)
def get_reported_symptoms(r):
content = request.get_json()
necessary_values = ['identifier']
isGoodRequest = check_for_values_in_request(necessary_values, content)
if (isGoodRequest[0] == False):
return make_response({'response': 'bad request, please try again and specify the ' + str(isGoodRequest[1]) + ' parameter in the JSON request body.'}, status.HTTP_400_BAD_REQUEST)
identifier = content['identifier']
push_id = unobfuscate(identifier, r)
if (push_id == None or identifier == None):
return make_response({'response': '\'identifier\' is invalid. Not Part of our database'}, status.HTTP_400_BAD_REQUEST)
else:
individual = retrieve_or_create_person_from_identifier(identifier)
response_json = {
'test_status': individual.test_status,
'symptoms': individual.symptoms,
'additional_info': individual.additional_info,
'test_date': individual.test_date,
'symptoms_date': individual.symptoms_date,
'has_response': individual.didReport
}
return make_response(response_json, status.HTTP_200_OK)