-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
540 lines (414 loc) · 17.9 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
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
from flask import Flask, request, redirect, render_template, flash
from flask_sqlalchemy import SQLAlchemy
from flask_login import UserMixin, login_required, logout_user, login_user, login_manager, LoginManager, current_user
from flask_mail import Mail
from sqlalchemy import func
from werkzeug.security import generate_password_hash, check_password_hash
from cryptography.fernet import Fernet
import json
# reading config file
encoding = 'ascii'
with open('config.json', 'r') as config:
contents = json.load(config)
dburi = contents["database"]["uri"]
f = Fernet(contents['key'].encode(encoding))
# creating flask app
app = Flask(__name__)
app.secret_key = "topsecretdonotread"
app.config['SQLALCHEMY_DATABASE_URI'] = dburi
db = SQLAlchemy(app)
# setting up login manager
login_manager = LoginManager(app)
login_manager.login_view = 'user_login'
login_manager.login_message = "Please login first"
login_manager.login_message_category = "warning"
# userloader
@login_manager.user_loader
def load_user(user_id):
return Patient.query.filter_by(srfid=user_id).first() or Hospitaluser.query.filter_by(hcode=user_id).first() or Admin.query.filter_by(username=user_id).first()
# Database models
class Admin(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(120), unique=True)
password = db.Column(db.String(120))
gmail = db.Column(db.String(254))
gpassword = db.Column(db.String(254))
def get_id(self):
return self.username
class Patient(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
srfid = db.Column(db.String(13), unique=True)
email = db.Column(db.String(254), unique=True)
dob = db.Column(db.String(10))
password = db.Column(db.String(254))
def get_id(self):
return self.srfid
class Hospitaluser(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
hcode = db.Column(db.String(10), unique=True)
email = db.Column(db.String(254), unique=True)
password = db.Column(db.String(1000))
def get_id(self):
return self.hcode
class Hospitaldata(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
hcode = db.Column(db.String(20), unique=True)
hname = db.Column(db.String(200))
normalbed = db.Column(db.Integer)
hicubed = db.Column(db.Integer)
icubed = db.Column(db.Integer)
ventbed = db.Column(db.Integer)
class Bookingpatient(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
srfid = db.Column(db.String(20), unique=True)
bedtype = db.Column(db.String(10))
hcode = db.Column(db.String(20), unique=True)
spo2 = db.Column(db.Integer)
pname = db.Column(db.String(50))
pphone = db.Column(db.String(12))
paddress = db.Column(db.String(100))
# setting up mail
def get_admin():
return Admin.query.filter_by().first()
def initiate_mail(admin):
app.config.update(
MAIL_SERVER='smtp.gmail.com',
MAIL_PORT='465',
MAIL_USE_SSL=True,
MAIL_USERNAME=admin.gmail,
MAIL_PASSWORD=f.decrypt(
admin.gpassword.encode(encoding)).decode(encoding)
)
# required decorators
def make_sure_user(access_to):
def decorator(func):
def wrapper(*args, **kwargs):
try:
if access_to == 'admin':
current_user.username
if access_to == 'patient':
current_user.srfid
if access_to == 'hospital':
current_user.hcode
except:
flash("Method not allowed", "danger")
return redirect('/')
return func(*args, **kwargs)
wrapper.__name__ = func.__name__
return wrapper
return decorator
# ROUTES
# routes accessible by everyone
@app.route('/')
def home():
hoscount = Hospitaluser.query.count()
pcount = Bookingpatient.query.count()
normalbed = db.engine.execute(
db.select(func.sum(Hospitaldata.normalbed))).fetchall()[0][0]
hicubed = db.engine.execute(
db.select(func.sum(Hospitaldata.hicubed))).fetchall()[0][0]
icubed = db.engine.execute(
db.select(func.sum(Hospitaldata.icubed))).fetchall()[0][0]
ventbed = db.engine.execute(
db.select(func.sum(Hospitaldata.ventbed))).fetchall()[0][0]
beds = 0
if (normalbed):
beds = normalbed+hicubed+icubed+ventbed
return render_template('index.html', hoscount=hoscount, pcount=pcount, beds=beds)
@app.route('/admin', methods=['GET', 'POST'])
def admin_login():
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
admin = Admin.query.filter_by().first()
if (admin and check_password_hash(admin.username, username) and check_password_hash(admin.password, password)):
login_user(admin)
flash("Login Success", "success")
return redirect("/")
flash("Invalid Credentials", "danger")
return render_template("admin.html")
@app.route('/hospitallogin', methods=['GET', 'POST'])
def hospital_login():
if request.method == 'POST':
email = request.form.get('email')
password = request.form.get('password')
user = Hospitaluser.query.filter_by(email=email).first()
if user and check_password_hash(user.password, password):
login_user(user)
flash("Login Success", "success")
return redirect('/')
else:
flash("Invalid Credentials", "danger")
return render_template("hospitallogin.html")
return render_template('hospitallogin.html')
@app.route('/usersignup', methods=['GET', 'POST'])
def user_signup():
if request.method == 'POST':
srfid = request.form.get('srfid')
email = request.form.get('email')
dob = request.form.get('dob')
encpass = generate_password_hash(request.form.get('password'))
srfid_exists = Patient.query.filter_by(srfid=srfid).first()
email_exists = Patient.query.filter_by(email=email).first()
if (len(srfid) != 13):
flash("SrfID must be strictly 13 digit number", "warning")
return render_template("usersignup.html")
if srfid_exists or email_exists:
flash("User with entered email or srfid already exists", "warning")
return render_template("usersignup.html")
db.engine.execute(
f"INSERT INTO patient (srfid, email, dob, password) VALUES ('{srfid}', '{email}', '{dob}', '{encpass}')")
flash("Signup success, please login here", "success")
return render_template('userlogin.html')
return render_template('usersignup.html')
@app.route('/userlogin', methods=['GET', 'POST'])
def user_login():
if request.method == 'POST':
logout_user()
srfid = request.form.get('srfid')
password = request.form.get('password')
patient = Patient.query.filter_by(srfid=srfid).first()
if patient and check_password_hash(patient.password, password):
login_user(patient)
flash("Login Success", "success")
return redirect("/")
else:
flash("Invalid Credentials", "danger")
return render_template("userlogin.html")
return render_template('userlogin.html')
@app.route("/logout")
@login_required
def logout():
logout_user()
flash("Logout Successful", "warning")
return redirect('/')
@app.route("/availablebeds")
def available_beds():
posts = Hospitaldata.query.all()
return render_template("availablebeds.html", posts=posts)
# routes accessible by admin only
@app.route('/addhospitaluser', methods=['GET', 'POST'])
@login_required
@make_sure_user('admin')
def add_hospital_user():
if request.method == 'POST':
hcode = request.form.get('hcode').upper()
email = request.form.get('email')
password = request.form.get('password')
encpass = generate_password_hash(password)
hcode_exists = Hospitaluser.query.filter_by(hcode=hcode).first()
email_exists = Hospitaluser.query.filter_by(email=email).first()
if len(hcode) > 10:
flash("HCODE must be less than 10 characters", "warning")
return render_template("addHosUser.html")
if hcode_exists or email_exists:
flash("Hospital with entered hcode or email already exists", "warning")
return render_template("addHosUser.html")
new_hos = Hospitaluser(hcode=hcode, email=email, password=encpass)
db.session.add(new_hos)
try:
''' Sending email - uncomment this section
message = (
"Thankyou for choosing us!\n\n" +
"As per your request we are registering your hospital with us. We are happy to have you " +
"and we appreciate your support for making booking process easier for patients.\n\n" +
"Here are your login credentials:\n" +
f"Hospital code: {hcode}\n"
f"Email: {email}\n"
f"Password: {password}\n\n"
"Now you can use above credentials to login to your hospital's account on our site " +
"http://localhost:5000. Do add your hospital data, so that others can view and book your beds.\n" +
"Thankyou."
)
admin = get_admin()
initiate_mail(admin)
Mail(app).send_message('COVID CARE CENTRE',
sender=admin.gmail,
recipients=[email],
body=message
)
'''
db.session.commit()
flash("Data Sent and Inserted Successfully", "success")
except:
flash("Failed, Error occured while sending mail, hospital not added", "danger")
return render_template('addHosUser.html')
@app.route("/triggeredevents")
@login_required
@make_sure_user('admin')
def triggered_events():
trigdata = db.engine.execute(f"SELECT * FROM trigevents")
return render_template("triggeredevents.html", trigdata=trigdata)
# routes accessible by hospitals only
@app.route("/addhospitalinfo", methods=['POST', 'GET'])
@login_required
@make_sure_user('hospital')
def add_hospital_info():
if request.method == 'POST':
hcode = request.form.get('hcode').upper()
hname = request.form.get('hname')
normalbed = request.form.get('normalbed')
hicubed = request.form.get('hicubed')
icubed = request.form.get('icubed')
ventbed = request.form.get('ventbed')
hos_exists = Hospitaluser.query.filter_by(hcode=hcode).first()
hos_data_exists = Hospitaldata.query.filter_by(hcode=hcode).first()
if (current_user.hcode != hcode):
flash(
"HCODE mismatch, please login with your hospital account to change data")
if hos_data_exists:
flash("Data is already present, you can update it", "warning")
elif hos_exists:
new_data = Hospitaldata(hcode=hcode, hname=hname, normalbed=normalbed, hicubed=hicubed, icubed=icubed, ventbed=ventbed)
db.session.add(new_data)
db.session.commit()
flash("Data added", "primary")
else:
flash("Hospital code does not exist", "danger")
return redirect("/addhospitalinfo")
posts = Hospitaluser.query.filter_by(email=current_user.email).first()
postsdata = Hospitaldata.query.filter_by(hcode=posts.hcode).first()
return render_template("hospitaldata.html", postsdata=postsdata)
@app.route("/hedit/<string:id>", methods=['POST', 'GET'])
@login_required
@make_sure_user('hospital')
def hedit(id):
curruser = current_user.hcode
urluser = Hospitaldata.query.filter_by(id=id).first().hcode
if (curruser != urluser):
flash("ID mismatch, please login with your account to change data", "warning")
return redirect("/addhospitalinfo")
if request.method == "POST":
hcode = request.form.get('hcode').upper()
hname = request.form.get('hname')
normalbed = request.form.get('normalbed')
hicubed = request.form.get('hicubed')
icubed = request.form.get('icubed')
ventbed = request.form.get('ventbed')
db.session.query(Hospitaldata).filter_by(id=id).update({
'hcode': hcode,
'hname': hname,
'normalbed': normalbed,
'hicubed': hicubed,
'icubed': icubed,
'ventbed': ventbed
})
db.session.commit()
flash("Data successfully updated", "success")
return redirect("/addhospitalinfo")
posts = Hospitaldata.query.filter_by(id=id).first()
return render_template("hedit.html", posts=posts)
@app.route("/hdelete/<string:id>", methods=['POST', 'GET'])
@login_required
@make_sure_user('hospital')
def hdelete(id):
curruser = current_user.hcode
urluser = Hospitaldata.query.filter_by(id=id).first().hcode
if (curruser != urluser):
flash("ID mismatch, please login with your account to delete data", "warning")
else:
db.session.query(Hospitaldata).filter_by(id=id).delete()
db.session.commit()
flash("Data successfully deleted", "danger")
return redirect("/addhospitalinfo")
@app.route("/pbookings")
@login_required
@make_sure_user('hospital')
def pbookings():
posts = Bookingpatient.query.filter_by(hcode=current_user.hcode)
return render_template("pbookings.html", posts=posts)
# routes accessible by patients
@app.route("/changepass", methods=['GET', 'POST'])
@login_required
@make_sure_user('patient')
def change_pass():
if request.method == 'POST':
oldpass = request.form.get('old-password')
newpass = request.form.get('new-password')
if check_password_hash(current_user.password, oldpass):
db.session.query(Patient).filter_by(srfid=current_user.srfid).update(
{'password': generate_password_hash(newpass)})
db.session.commit()
flash("Password change successfully", "success")
else:
flash("Entered old password is wrong", "danger")
return render_template('changepass.html')
@app.route("/slotbooking", methods=['GET', 'POST'])
@login_required
@make_sure_user('patient')
def slot_booking():
posts = Hospitaldata.query.filter()
if request.method == "POST":
srfid = request.form.get('srfid')
bedtype = request.form.get('bedtype')
hcode = request.form.get('hcode').upper()
spo2 = request.form.get('spo2')
pname = request.form.get('pname')
pphone = request.form.get('pphone')
paddress = request.form.get('paddress')
hos_data_exists = Hospitaldata.query.filter_by(hcode=hcode).first()
patient_exists = Bookingpatient.query.filter_by(srfid=srfid).first()
if current_user.srfid != srfid:
flash("You cannot book slot for other account", "warning")
return render_template("slotbooking.html", posts=posts)
if not hos_data_exists:
flash("Entered hospital code doesn't exist", "warning")
return render_template("slotbooking.html", posts=posts)
if patient_exists:
flash(
"You already have a bed booked, view Patient details page to view your booking", "warning")
return render_template("slotbooking.html", posts=posts)
beds = hos_data_exists.__dict__[bedtype]
if beds <= 0:
flash("No beds available", "danger")
return render_template("slotbooking.html", posts=posts)
db.session.query(Hospitaldata).filter_by(hcode=hcode).update({bedtype: beds - 1})
new_book = Bookingpatient(srfid=srfid, bedtype=bedtype, hcode=hcode,
spo2=spo2, pname=pname, pphone=pphone, paddress=paddress)
db.session.add(new_book)
try:
''' Sending email - uncomment this section
hos = Hospitaluser.query.filter_by(hcode=hcode).first()
patient = Patient.query.filter_by(srfid=current_user.srfid).first()
message = (
"Thankyou for choosing us!\n\n" +
"Bed booking was successful\n\n" +
"Bed booked at:\n" +
f"Hospital code: {hos.hcode}\n"
f"Hospital Email: {hos.email}\n\n"
"Bed booked by:\n" +
f"Patient SRFID: {patient.srfid}\n"
f"DOB: {patient.dob}\n"
f"Email: {patient.email}\n\n"
"Thankyou."
)
admin = get_admin()
initiate_mail(admin)
Mail(app).send_message('COVID CARE CENTRE',
sender=admin.gmail,
recipients=[hos.email, current_user.email],
body=message
)
'''
db.session.commit()
flash("Your slot booking was successful. Kindly visit hospital for further procedure", "success")
except:
flash("Failed, Error occured while sending mail, hospital not added", "danger")
return render_template("slotbooking.html", posts=posts)
srfid = current_user.srfid
patient_exists = Bookingpatient.query.filter_by(srfid=srfid).first()
ispatient = not not patient_exists
if ispatient: flash("You already have a bed booked, view Patient details page to view your booking", "warning")
return render_template("slotbooking.html", posts=posts, ispatient=ispatient)
@app.route("/pdetails", methods=['GET', 'POST'])
@login_required
@make_sure_user('patient')
def pdetails():
data = Bookingpatient.query.filter_by(srfid=current_user.srfid).first()
return render_template("pdetails.html", data=data)
# Error handlers
@app.errorhandler(Exception)
def handle_generic_error(e):
return redirect('/')
if __name__ == '__main__':
app.run(host="0.0.0.0", debug=True)