forked from vsahni3/EasyMed
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgreeting.py
More file actions
244 lines (211 loc) · 7.34 KB
/
greeting.py
File metadata and controls
244 lines (211 loc) · 7.34 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
from flask import Flask, request, jsonify
import sql
import ml
import sqlite3
from datetime import datetime
app = Flask(__name__)
@app.route('/login/', methods=['POST'])
def login():
email = request.form.get('email')
password = request.form.get('password')
if not email:
email = request.get_json()['email']
if not password:
password = request.get_json()['password']
if email and password:
conn = sqlite3.connect('mydatabase.db', check_same_thread=False)
mycursor = conn.cursor()
mycursor.execute(f'SELECT password FROM userInfo WHERE username = "{email}"')
correct_password = mycursor.fetchone()[0]
if password == correct_password:
sql.create_records_table(email)
sql.create_meds_table(email)
response = {
# this includes med_id which is needed for other requests
"Medicines": sql.load_meds(email),
# Add this option to distinct the POST request
"METHOD": "POST"
}
return jsonify(response)
else:
return jsonify({
'ERROR': 'Incorrect Login or Password'
})
else:
return jsonify({
"ERROR": "No email found. Please send an email."
})
@app.route('/upload/', methods=['POST'])
def upload():
data = request.form.get('text')
email = request.form.get('email')
if not email:
email = request.get_json()['email']
if not data:
data = request.get_json()['text']
if data and email:
extracted_data = ml.extract_data(data)
sql.final_insert_meds(email, extracted_data)
response = {
"Message": f"Your prescription has been added",
# this includes med_id which is needed for other requests
"Medicines": sql.load_meds(email),
# Add this option to distinct the POST request
"METHOD": "POST"
}
return jsonify(response)
else:
return jsonify({
"ERROR": "No email found. Please send an email."
})
@app.route('/records/', methods=['POST'])
def records():
email = request.form.get('email')
if not email:
email = request.get_json()['email']
if email:
response = {
"Records": sql.load_records(email),
# Add this option to distinct the POST request
"METHOD": "POST"
}
return jsonify(response)
else:
return jsonify({
"ERROR": "No email found. Please send an email."
})
@app.route('/points/', methods=['POST'])
def points():
email = request.form.get('email')
if not email:
email = request.get_json()['email']
if email:
response = {
"Points": sql.load_points(email),
# Add this option to distinct the POST request
"METHOD": "POST"
}
return jsonify(response)
else:
return jsonify({
"ERROR": "No email found. Please send an email."
})
@app.route('/create/', methods=['POST'])
def create_medicine():
email = request.form.get('email')
new_med = request.form.get('new_med')
if not email:
email = request.get_json()['email']
new_med = request.get_json()['new_med']
# new_med should be an array of [day, time, name, dosage]
if email and new_med:
sql.insert_meds_table(email, *new_med)
response = {
"Medicines": sql.load_meds(email),
# Add this option to distinct the POST request
"METHOD": "POST"
}
return jsonify(response)
elif not email:
return jsonify({
"ERROR": "No email found. Please send an email."
})
else:
return jsonify({
"ERROR": "No changes found. Please include medicine changes."
})
@app.route('/update/', methods=['POST'])
def update_medicine():
email = request.form.get('email')
new_med = request.form.get('new_med')
if not email:
email = request.get_json()['email']
new_med = request.get_json()['new_med']
# new_med should be an array of [med_id, day, time, name, dosage]
if email and new_med:
sql.final_update_meds_table(email, *new_med)
response = {
"Medicines": sql.load_meds(email),
# Add this option to distinct the POST request
"METHOD": "POST"
}
return jsonify(response)
elif not email:
return jsonify({
"ERROR": "No email found. Please send an email."
})
else:
return jsonify({
"ERROR": "No changes found. Please include medicine changes."
})
@app.route('/remove/', methods=['POST'])
def remove_medicine():
email = request.form.get('email')
med_id = request.form.get('med_id')
if not email:
email = request.get_json()['email']
med_id = request.get_json()['med_id']
if email and med_id and med_id.isdigit():
sql.remove_meds_row(email, int(med_id))
response = {
"Medicines": sql.load_meds(email),
# Add this option to distinct the POST request
"METHOD": "POST"
}
return jsonify(response)
elif not email:
return jsonify({
"ERROR": "No email found. Please send an email."
})
else:
return jsonify({
"ERROR": "No med id found. Please include med id."
})
@app.route('/newrecord/', methods=['POST'])
def add_record():
email = request.form.get('email')
med_id = request.form.get('med_id')
expected_time = request.form.get('expected_time')
current_time = request.form.get('current_time')
current_date = request.form.get('current_date')
if not email:
email = request.get_json()['email']
med_id = request.get_json()['med_id']
expected_time = request.get_json()['expected_time']
current_time = request.get_json()['current_time']
current_date = request.get_json()['current_date']
# expected_time is in HH:MM:SS, 24h
# current_date is in MM/DD/YY
if email and med_id and expected_time:
current_datetime = datetime.strptime(current_date + " " + current_time, "%m/%d/%y %H:%M:%S")
expected_datetime = datetime.strptime(current_date + " " + expected_time, "%m/%d/%y %H:%M:%S")
time_diff = current_datetime - expected_datetime
mins_diff = abs(time_diff.total_seconds() / 60)
if mins_diff > 10:
status = 'MISS'
else:
status = 'GOOD'
# sql.update_users_table(email, 10)
sql.insert_records_table(email, status, med_id, current_date, current_time)
response = {
# Add this option to distinct the POST request
"Records": sql.load_records(email),
'email': email,
"METHOD": "POST"
}
return jsonify(response)
elif not email:
return jsonify({
"ERROR": "No email found. Please send an email."
})
else:
return jsonify({
"ERROR": "No med id found. Please include med id."
})
@app.route('/')
def index():
# A welcome message to test our server
return "<h1>Welcome to our medium-greeting-api!</h1>"
if __name__ == '__main__':
# Threaded option to enable multiple instances for multiple user access support
app.run(threaded=True, port=5000)