-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
319 lines (287 loc) · 10.4 KB
/
app.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import os
from flask import Flask, render_template, jsonify, request, redirect, url_for, make_response
import settings
import requests
import hmac
import base64
import json
import sqlite3
import time
import datetime
from flask_cors import CORS, cross_origin
project_dir = os.path.dirname(os.path.abspath(__file__))
def send_email(sender_email_id, sender_email_id_password, receiver_email_id, message):
# Python code to illustrate Sending mail from
# your Gmail account
import smtplib, ssl
# creates SMTP session
# s = smtplib.SMTP('smtp.gmail.com', 587)
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as s:
# start TLS for security
# s.starttls()
# Authentication
s.login(sender_email_id, sender_email_id_password)
# sending the mail
s.sendmail(sender_email_id, receiver_email_id, message)
# terminating the session
# s.quit()
# create and configure the app
app = Flask(__name__, instance_relative_config=True)
cors = CORS(app)
# con = sqlite3.connect('newdb.db')
app.config.from_object(settings)
port = 5000
with open('symptoms.json') as f:
symptoms = json.load(f)
@app.route('/nearby', methods=['GET', 'POST'])
def nearby():
if request.method == 'GET':
return render_template('nearby.html', res=None)
# dummy = [(19.116884428986182, 72.93164483021962), (19.10123794041552, 72.91207824204169)]
# print('request data',request.json)
con = sqlite3.connect('newdb.db')
def execute(query):
with con:
data = con.execute(query)
return data
# query_res = execute('select latitude,longitude from doctor;')
# for lat, lon in list(query_res):
# tempstri += '|{},{}'.format(lat, lon)
user_lat, user_lon = [request.json[i] for i in ['latitude', 'longitude']]
api_key = app.config['MAPS_API_KEY']
url = 'https://maps.googleapis.com/maps/api/staticmap?zoom=13&size=600x300¢er={},{}&zoom=13&size=600x300&maptype=roadmap&markers=color:red|label:D'.format(user_lat, user_lon, api_key)
docs = []
for doc in execute('select * from doctor;'): # list(query_res):
print('inside loop',doc)
lat, lon = doc[6], doc[7]
rev_geo_url = 'https://maps.googleapis.com/maps/api/geocode/json?latlng={},{}&key={}'.format(lat,lon,api_key)
temp_res = requests.get(rev_geo_url).json()
address = temp_res['results'][0]['formatted_address']
doc = list(doc)
doc.append(address)
doc = tuple(doc)
docs.append(doc)
url += '|{},{}'.format(lat,lon)
url += '&markers=color:blue|label:I|{},{}'.format(user_lat, user_lon)
url += '&key={}'.format(api_key)
print(url)
res = requests.get(url)
print(docs)
return render_template('nearby.html', res = base64.b64encode(res.content).decode(), doctors = docs)
@app.route('/', methods=['GET'])
def root():
if request.cookies.get('docID'):
return redirect(url_for('doc_home'))
return render_template('index.html')
@app.route('/doctor', methods=['GET', 'POST'])
def doctor_reg():
if request.cookies.get('docID'):
return redirect(url_for('root'))
if request.method == 'GET':
return render_template('doctor-reg.html')
else:
# form ke values ko DB me daalo
con = sqlite3.connect('newdb.db')
name = request.form['name']
phone = request.form['number']
email = request.form['email']
reg_no = request.form['reg_no']
council = request.form['council']
query = f"""
insert into doctor values (NULL, "{name}", "{phone}", "{email}", "{reg_no}", "{council}", NULL, NULL, '')
"""
print(query)
with con:
data = con.execute(query)
query = f"""
SELECT * FROM doctor ORDER BY ID DESC LIMIT 1
"""
print(query)
with con:
data = con.execute(query)
id = 1
for row in data:
id = row[0]
# get ID of that doctor, next form me needed!
return render_template('doctor-reg-2.html', docID = id)
@app.route('/docRegLocation', methods=['POST'])
def doc_reg_location():
# ID, lat, lng aaega, push it to DB for given ID
con = sqlite3.connect('newdb.db')
id = request.form['id']
lat = request.form['lat']
lng = request.form['lng']
query = f"""
update doctor set latitude="{lat}", longitude="{lng}" where id="{id}"
"""
print(query)
with con:
data = con.execute(query)
response = make_response(redirect(url_for('doc_home')))
response.set_cookie('docID', id, max_age=60*60*24*365)
return response
@app.route('/docHome', methods=['GET'])
def doc_home():
return render_template('doc_home.html')
@app.route('/logout', methods=['GET'])
def logout_doc():
response = make_response(redirect(url_for('root')))
response.set_cookie('docID', '3435', max_age=0)
return response
@app.route('/new-blog', methods=['GET'])
def new_blog():
return render_template('new-blog.html')
def get_doc_name(docID):
con = sqlite3.connect('newdb.db')
query = f"""
SELECT name FROM doctor where id={int(docID)}
"""
print(query)
with con:
data = con.execute(query)
for row in data:
return row[0]
@app.route('/view-blogs', methods=['GET'])
def view_blogs():
# get list of all blogs
# (blog_id, title, author_id, published_at, content)
blogs = []
con = sqlite3.connect('newdb.db')
query = f"""
SELECT * FROM blogpost
"""
print(query)
with con:
data = con.execute(query)
for row in data:
blogs.append(row)
print(blogs)
length = len(blogs)
for i in range(length):
blogs[i] = list(blogs[i])
blogs[i][2] = get_doc_name(blogs[i][2])
return render_template('view-blogs.html', length = length, blogs = blogs)
@app.route('/view-blog/<id>')
def view_blog(id):
# get stuff from blog table
con = sqlite3.connect('newdb.db')
query = f"""
SELECT * FROM blogpost where blog_id={int(id)}
"""
print(query)
blog = []
with con:
data = con.execute(query)
for row in data:
blog = tuple(row)
print(blog)
# get doc name from doc table using docID recvd from blog table
doc_name = get_doc_name(blog[2])
title = blog[1]
content = blog[4]
return render_template('view-blog.html', title = title, content = content, author = doc_name)
@app.route('/submit-blog', methods=['POST'])
def submit_blog():
docID = request.cookies.get('docID')
# put stuff in the DB - blogID, title, content, docID
con = sqlite3.connect('newdb.db')
title = request.form['title']
content = request.form['content']
ts = time.time()
timestamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
query = f"""
insert into blogpost values (NULL, "{title}", "{docID}", "{timestamp}", "{content}")
"""
print(query)
with con:
data = con.execute(query)
query = f"""
SELECT * FROM blogpost ORDER BY blog_id DESC LIMIT 1
"""
print(query)
with con:
data = con.execute(query)
blogID = 1
for row in data:
blogID = row[0] # get ID
return redirect(url_for('view_blog', id=blogID))
@app.route('/new-conference', methods=['GET'])
def new_conference():
return render_template('new-conference.html')
@app.route('/view-conferences', methods=['GET'])
def view_conferences():
# get list of all conferences
# (id, zoom_link, title, desc, start, end, docID)
conferences = [(1, 'https://us02web.zoom.us/j/2289', 'title', 'desc', 'timestamp_start', 'timestamp_end', 1), (2, 'https://us02web.zoom.us/j/8193', 'title2', 'desc2', 'timestamp_start', 'timestamp_end', 1)]
length = len(conferences)
for i in range(length):
conferences[i] = list(conferences[i])
conferences[i][6] = get_doc_name(conferences[i][6])
return render_template('view-conferences.html', length = len(conferences), conferences = conferences)
@app.route('/submit-conference', methods=['POST'])
def submit_conference():
docID = request.cookies.get('docID')
# put stuff in the DB - confID, title, description, date, starttime, duration, docID
return redirect(url_for('view_conferences'))
@app.route('/share-symptoms', methods=['POST'])
def send_mail():
age = request.cookies.get('age')
gender = request.cookies.get('gender')
symptoms = request.cookies.get('symptoms')
con = sqlite3.connect('newdb.db')
def execute(query):
with con:
data = con.execute(query)
return data
print(request.json)
doc_email = list(execute('select email from doctor where id={}'.format(request.json['doctorId'])))[0][0]
message = """
Hi, patient has shared symptoms with you
Age: {}
Gender: {}
Symptoms: {}
""".format(age, gender, symptoms)
print(message)
send_email("[email protected]", "1711065and66", doc_email, message)
return jsonify({'status': True});
@app.route('/diagnosis', methods=['GET'])
def diagnosis():
url = 'https://sandbox-healthservice.priaid.ch/diagnosis'
token = app.config['DIAGNOSIS_TOKEN']
print(token)
args = dict(request.args.lists())
# return jsonify({'a':'b'})
symptom_list = args['symptoms']
print(symptom_list)
symptom_ids = []
for symptom in symptom_list:
if symptoms.get(symptom.lower()):
symptom_ids.append(symptoms[symptom.lower()])
symptom_ids = '[{}]'.format(','.join(map(str, symptom_ids)))
print(symptom_ids)
data = {
'token': token,
'language': 'en-gb',
'symptoms': symptom_ids,
'gender': args['gender'][0],
'year_of_birth': 2021 - int(args['age'][0]),
'format': 'json'
}
res = requests.get(url, data)
# return jsonify({'text':res.text, 'status':res.status_code})
data = eval(res.text)
print('response',data)
res = []
for i in range(min(3, len(data))):
# if data[i]['Issue']['Accuracy'] >= 50:
res.append(data[i]['Issue']['ProfName'])
print(res)
# return jsonify({'data': res})
response = make_response(render_template('diagnosis_result.html', res=res))
response.set_cookie('gender', str(args['gender'][0]), max_age=60*60*24*365)
response.set_cookie('age', str(args['age'][0]), max_age=60*60*24*365)
response.set_cookie('symptoms', str(res), max_age=60*60*24*365)
return response
if __name__ == '__main__':
app.run(port=port, debug=True)