-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
430 lines (356 loc) · 12.4 KB
/
views.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
from flask_sqlalchemy import SQLAlchemy
from flask import Flask, jsonify, request,render_template,send_file
from app import app, db
from models import mediapp_patient, mediapp_userinfo, post, mediapp_problems
import datetime,os
# get imgs in the project
@app.route('/sys_img/<pic_path>',methods=['GET','POST'])
def returnPic(pic_path):
fileDir = os.path.join(os.path.dirname(os.path.realpath('__file__')),'static')
targetPath=os.path.join(fileDir,pic_path)
return send_file(targetPath, mimetype='image/png') #, video/mp4
# return webpage
@app.route('/createUser',methods=['GET','POST'])
def showAddUserPage():
#fileDir = os.path.join(os.path.dirname(os.path.realpath('__file__')),'static')
#targetPath=os.path.join(fileDir,'index.html')
#print(targetPath)
#return render_template("index.html")
return render_template("test.html")
'''routes connect to db and fetch data'''
@app.route('/test')
def index():
sql_cmd = """
select *
from disease
"""
query_data = db.engine.execute(sql_cmd)
for row in query_data:
print("id:", row)
print(query_data)
print("OK")
return 'OK'
@app.route('/getPatientInfo/<patient_id>',methods=['POST'])
def getPatientInfo(patient_id):
patient=mediapp_patient.query.filter_by(id=int(patient_id)).first()
if patient:
patientInfo={}
patientInfo["name"]=patient.Name
patientInfo["line_id"]=patient.lineID
return jsonify(patientInfo),200
else:
return 'no such patient',500
@app.route('/getLineStatus',methods=['POST'])
def getLineStatus():
print(request.get_json())
line_id=request.get_json()["line_id"]
linePatient=mediapp_patient.query.filter_by(lineID=line_id).all()
lineStatus='0'
if linePatient:
for patient in linePatient:
if patient.Certified=='1':
lineStatus='1'
elif patient.Certified=='2':
lineStatus='2'
break
else:
lineStatus='3'
#0 patient has no line account
#1 patient applied account, not certidfied
#2 patient line account certified
#3 lineid not found in db
print('test\ntest\ntest\n')
print(lineStatus)
return lineStatus
@app.route('/getLinePending',methods=['POST'])
def getLinePending():
line_id=request.get_json()["line_id"]
linePatient=mediapp_patient.query.filter_by(lineID=line_id).all()
pendings=[]
if linePatient:
for patient in linePatient:
pendingInfo={}
if patient.Certified=='1':
pendingInfo["name"]=patient.Name
pendings.append(pendingInfo)
#0 patient has no line account
#1 patient applied account, not certidfied
#2 patient line account certified
#3 lineid not found in db
return jsonify(pendings),200
@app.route('/user/<lineID>',methods=['GET'])
def users(lineID):
lineID = "'"+lineID+"'"
sql_cmd = """
select *
from mediapp_patient
where lineID =
"""+lineID
data = []
query_data = db.engine.execute(sql_cmd)
for row in query_data:
diction = {}
phID = row['PharmacyID']
print("user:", row['Name'],row['Sex'],row['PharmacyID'])
diction['name'] = row['Name']
diction['gender'] = row['Sex']
userName = row['Name']
userGender = row['Sex']
sql_cmd2 = """
select *
from mediapp_userinfo
where id =
""" + phID
query_data2 = db.engine.execute(sql_cmd2)
for row2 in query_data2:
# phID = row['PharmacyID']
print("user:", row2['PhName'])
diction['phName'] = row2['PhName']
phName = row2['PhName']
data.append(diction)
print(data)
print(query_data2)
print('ok')
return jsonify(data), 200
@app.route('/phInfo/<lineID>', methods=['GET'])
def phInfo(lineID):
# lineID = "'"+lineID+"'"
data = []
# sql_cmd = """
# select *
# from mediapp_patient
# where lineID =
# """ + lineID
# query_data = db.engine.execute(sql_cmd)
# for row in query_data:
# print("user:", row)
# phId = row['PharmacyID']
# sql_cmd2 = """
# select *
# from mediapp_userinfo
# where id =
# """ + phId
# query_data2 = db.engine.execute(sql_cmd2)
# for row in query_data2:
# diction = {}
# print("phInfo:", row)
# diction['phName'] = row['PhName']
# diction['phTel'] = row['Telephone']
# diction['phAdd'] = row['Address']
# diction['LineID'] = row['LineID']
# diction['WebURL'] = row['WebURL']
# data.append(diction)
query = mediapp_patient.query.filter_by(lineID=lineID).all()
if query:
for num in query:
query2 = mediapp_userinfo.query.filter_by(id=num.PharmacyID).first()
if query2 != None:
diction = {}
print("phInfo:", query2)
diction['phName'] = query2.PhName
diction['phTel'] = query2.Telephone
diction['phAdd'] = query2.Address
diction['LineID'] = query2.LineID
diction['WebURL'] = query2.WebURL
data.append(diction)
else:
dictionErr = {}
# dictionErr['result'] = 'Not Found'
# dictionErr['userName'] = num.Name
# data.append(dictionErr)
else:
dictionErr = {}
# dictionErr['lineID'] = lineID
# dictionErr['result'] = 'Not Found'
# data.append(dictionErr)
return jsonify(data) , 200
@app.route('/addUser', methods=['POST'])
def addUser():
# , cardNumber, lineID, photo
print('test1')
print(request.form)
print('test2')
print('test3')
req_data = request.form
cardID = req_data['cardID']
cardNumber = req_data['cardNumber']
# celePhone = req_data['celePhone']
lineID = req_data['lineID']
lineName = req_data['lineName']
check = mediapp_patient.query.filter_by(lineID = lineID).first()
if check == None:
query = mediapp_patient.query.filter_by(cardID=cardID).first()
query.cardNumber = cardNumber
# query.Celephone = celePhone
query.lineID = lineID
query.lineName = lineName
query.Certified = 2
db.session.commit()
else:
query = mediapp_patient.query.filter_by(cardID=cardID).first()
query.cardNumber = cardNumber
# query.Celephone = celePhone
query.lineID = lineID
query.lineName = lineName
query.Certified = 1
db.session.commit()
return 'ok', 200
@app.route('/checkUser/<cardID>', methods=['GET'])
def checkUser(cardID):
res = ''
query = mediapp_patient.query.filter_by(cardID=cardID).first()
if query == None:
print('Not Found')
res = 'Not Found'
else:
print(query.id)
res = 'Found'
return res, 200
@app.route('/checkTime/<lineID>',methods=['GET'])
def checkMul(lineID):
# data = []
# lineID = "'"+lineID+"'"
# sql_cmd = """
# select *
# from mediapp_patient
# where lineID = """+lineID
# query_data = db.engine.execute(sql_cmd)
# for row in query_data:
# print("info: ",row['id'])
# sql_cmd2 = """
# select TakeMedDate
# from post
# where PatientID_id ={id}
# """.format(id=row['id'])
# query_data2 = db.engine.execute(sql_cmd2)
# for row2 in query_data2:
# diction = {}
# print("time: ",row2['TakeMedDate'])
# diction['dayStart'] = row2['TakeMedDate']
# d = datetime.datetime.strptime(row2['TakeMedDate'],'%Y-%m-%d')
# delta = datetime.timedelta(days=10)
# n_days = d + delta
# print (n_days.strftime('%Y-%m-%d'))
# diction['dayEnd'] = n_days.strftime('%Y-%m-%d')
# data.append(diction)
# # query_data = db.engine.execute(sql_cmd2)
data=[]
query = mediapp_patient.query.filter_by(lineID=lineID).all()
if query:
for num in query:
query2 = post.query.filter_by(PatientID_id=num.id).all()
if query2:
for row2 in query2:
if row2:
diction = {}
diction['userName'] = num.Name
diction['dayStart'] = row2.TakeMedDate_start
d = datetime.datetime.strptime(row2.TakeMedDate_start,'%Y-%m-%d')
diction['weekDayStart'] = d.weekday()
delta = datetime.timedelta(days=10)
n_days = d + delta
diction['dayEnd'] = n_days.strftime('%Y-%m-%d')
diction['weekDayEnd'] = n_days.weekday()
diction['symptom']=row2.symptom
data.append(diction)
# print(data)
else:
diction2 = {}
diction2['userName'] = num.Name
diction2['result'] = 'Not Found'
data.append(diction2)
else:
diction2 = {}
# diction2['userName'] = num.Name
# diction2['result'] = 'Not Found'
# data.append(diction2)
else:
diction3 = {}
# diction3['lineID'] = lineID
# diction3['result'] = 'Not Found'
# data.append(diction3)
return jsonify(data), 200
'''
@app.route('/notify/<postID>',methods=['GET'])
def notify(postID):
sql_cmd = """
update post
set RemindStatus = 2
where id = {id}""".format(id=postID)
db.engine.execute(sql_cmd)
return 'ok', 200
'''
@app.route('/problem',methods=['POST'])
def problem():
req_data = request.form
Type = req_data['Type']
LineID = req_data['LineID']
query = mediapp_patient.query.filter_by(lineID=LineID).first()
if query is None:
return 'Not Found', 400
else:
problemAns = mediapp_problems(Type=Type,LineID=LineID)
db.session.add(problemAns)
db.session.commit()
return 'ok', 200
@app.route('/notify/<postID>',methods=['GET'])
def notify(postID):
# sql_cmd = """
# update post
# set RemindStatus = 2
# where id = {id}""".format(id=postID)
# db.engine.execute(sql_cmd)
query = post.query.filter_by(id=postID).first()
if query is None:
return 'Not Found', 400
else:
query.RemindStatus = 2
db.session.commit()
return 'ok', 200
@app.route('/postPh/<PhName>',methods=['GET'])
def postUser(PhName):
data = []
query = mediapp_userinfo.query.filter_by(PhName=PhName).first()
if query == None:
return 'Not Found', 200
else:
diction = {}
# print("phInfo:", query)
diction['phName'] = query.PhName
diction['phTel'] = query.Telephone
diction['phAdd'] = query.Address
diction['LineID'] = query.LineID
diction['WebURL'] = query.WebURL
data.append(diction)
return jsonify(data), 200
@app.route('/postInfo/<int:id>',methods=['GET'])
def postInfo(id):
data = []
diction = {}
diction['postID'] = id
query = post.query.filter_by(id=id).first()
if query == None:
return 'Post not exist', 200
else:
query2 = mediapp_patient.query.filter_by(id=query.PatientID_id).first()
if query2 == None:
return 'Patient not exist', 200
else:
diction['Gender'] = query2.Sex
diction['Name'] = query2.Name
diction['lineID'] = query2.lineID
query3 = mediapp_userinfo.query.filter_by(id=query.PharmacyID).first()
if query3 == None:
return 'Pharmacy null', 200
else:
diction['PhName'] = query3.PhName
diction['dayStart'] = query.TakeMedDate_start
d = datetime.datetime.strptime(query.TakeMedDate_start,'%Y-%m-%d')
diction['weekDayStart'] = d.weekday()
delta = datetime.timedelta(days=10)
n_days = d + delta
diction['dayEnd'] = n_days.strftime('%Y-%m-%d')
diction['weekDayEnd'] = n_days.weekday()
diction['symptom']=query.symptom
data.append(diction)
return jsonify(data),200