-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
482 lines (388 loc) · 19.9 KB
/
app.py
File metadata and controls
482 lines (388 loc) · 19.9 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
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
# from flask import Flask, render_template, redirect, url_for, request, session,flash
# from flask_sqlalchemy import SQLAlchemy
# from flask_socketio import SocketIO, join_room, leave_room, send, emit
# from flask_login import LoginManager, current_user, login_required,login_user,UserMixin, logout_user
# from werkzeug.security import generate_password_hash,check_password_hash
# from datetime import datetime
# app = Flask(__name__)
# app.config['SECRET_KEY'] = 'your_secret_key'
# app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///chat.db'
# app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# db = SQLAlchemy(app)
# login_manager=LoginManager()
# login_manager.init_app(app)
# socketio = SocketIO(app)
# class Message(db.Model):
# id = db.Column(db.Integer, primary_key=True)
# room = db.Column(db.String(150), nullable=False)
# sender = db.Column(db.String(150), nullable=False)
# content = db.Column(db.Text, nullable=False)
# timestamp = db.Column(db.DateTime, default=datetime.utcnow)
# class User(UserMixin,db.Model):
# id = db.Column(db.Integer, primary_key=True)
# username = db.Column(db.String(80), unique=True, nullable=False)
# email = db.Column(db.String(120), unique=True, nullable=False)
# fname = db.Column(db.String(120), nullable=False)
# lname = db.Column(db.String(120), nullable=False)
# user_type = db.Column(db.String(20), nullable=False)
# password = db.Column(db.String(120), nullable=False)
# def __repr__(self):
# return f'<User {self.username}, {self.email},{self.password}>'
# class Prescription(db.Model):
# id = db.Column(db.Integer, primary_key=True)
# doctor_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
# patient_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
# prescription_text = db.Column(db.Text, nullable=False)
# created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)
# def __repr__(self):
# return f'<Prescription {self.doctor_id}, {self.patient_id},{self.prescription_text},{self.created_at}>'
# class PatientParentRelation(db.Model):
# id = db.Column(db.Integer, primary_key=True)
# patient_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
# parent_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
# relation_type = db.Column(db.String(50), nullable=False)
# def __repr__(self):
# return f'<PatientParentRelation {self.patient_id}, {self.parent_id}, {self.relation_type}>'
# @login_manager.user_loader
# def load_user(id):
# return User.query.get(int(id))
# @app.route("/")
# def index():
# # data=Blog.query.all()
# return render_template("index.html")
# @app.route("/register", methods=['POST', 'GET'])
# def register():
# if request.method == 'POST':
# # Retrieve form data
# email = request.form.get('email')
# password = request.form.get('password')
# username = request.form.get('uname')
# fname = request.form.get('fname')
# lname = request.form.get('lname')
# user_type = request.form.get('user_type')
# if user_type not in ["doctor", "patient"]:
# flash("Please select a valid user type (Doctor or Patient)", "error")
# # Redirect back to register form with filled values
# return render_template("register.html", email=email, uname=username, fname=fname, lname=lname)
# # Print data for debugging
# print(email, username, password,user_type)
# password=generate_password_hash(password,method='pbkdf2:sha256')
# # Add new user to the database
# user = User(username=username, email=email, fname=fname, lname=lname, password=password,user_type=user_type)
# db.session.add(user)
# db.session.commit()
# flash("user is registered successfully","success")
# return redirect('/login')
# return render_template("register.html")
# @app.route('/login', methods=['GET', 'POST'])
# def login():
# if request.method == 'POST':
# email = request.form.get('email')
# password = request.form.get('password')
# user=User.query.filter_by(email=email).first()
# if user and check_password_hash(user.password,password):
# session['username'] = user.username
# flash(f'Logged in successfully as {user.username}', 'success')
# login_user(user)
# # print_users
# print(user.username+" is a "+user.user_type)
# if(user.user_type=="doctor"):
# return redirect('/patients')
# if(user.user_type=="patient"):
# return redirect('/doctors')
# return redirect('/')
# # return redirect(url_for('chat'))
# else:
# flash('Invalid Credentials','warning')
# return redirect('/login')
# return render_template("login.html")
# @app.route("/logout")
# def logout():
# flash(f'{current_user.username} is logged out','message')
# logout_user()
# return redirect("/")
# @app.route("/patient_detail/<int:id>", methods=["GET", "POST"])
# def patient_detail(id):
# patient = User.query.get(id)
# # prescriptions = Prescription.query.filter_by(patient_id=id).all()
# # prescriptions = db.session.query(Prescription, User.username)\
# # .join(User, Prescription.doctor_id == User.id)\
# # .all()
# prescriptions = db.session.query(Prescription, User.username).join(User, Prescription.doctor_id == User.id).filter(Prescription.patient_id == id).all()
# # relations = db.session.query(PatientParentRelation, User.username).join(User, PatientParentRelation.parent_id == User.id).filter(PatientParentRelation.patient_id == id).all()
# #patient is relation with some other patient
# child_relations = db.session.query(PatientParentRelation, User.username,User.fname,User.lname).join(User, PatientParentRelation.parent_id == User.id).filter(PatientParentRelation.patient_id == id).all()
# #some other patient is related to the patient
# parent_relations = db.session.query(PatientParentRelation, User.username,User.fname,User.lname).join(User, PatientParentRelation.patient_id == User.id).filter(PatientParentRelation.parent_id == id).all()
# # print("a babu gandu ho kya")
# for child in child_relations:
# print(child)
# # print("a babu gandu ho kya")
# if request.method == 'POST':
# prescription_text = request.form.get('prescription_text')
# doctor_id = current_user.id
# prescription = Prescription(doctor_id=doctor_id, patient_id=id, prescription_text=prescription_text)
# db.session.add(prescription)
# db.session.commit()
# flash('Prescription added successfully', 'success')
# return redirect(f"/patient_detail/{id}")
# return render_template("patient_detail.html", patient=patient,prescriptions=prescriptions,child_relations=child_relations,parent_relations=parent_relations)
# @app.route("/add_relative/<int:id>",methods=["GET","POST"])
# def add_parent(id):
# if request.method=="POST":
# patient_id=id #Patient
# relative=User.query.filter_by(email=request.form.get("parent_email")).first() #Relative or Relative
# relation=request.form.get("relationship")#Relation
# if not relative:
# flash('Sorry No such Relative has been registered', 'warning')
# return redirect(f"/patient_detail/{id}")
# relative_id=relative.id
# # Check if the relation already exists
# existing_relation = PatientParentRelation.query.filter_by(patient_id=patient_id, parent_id=relative.id).first()
# if existing_relation:
# flash(f'Relative {relative.username} is already linked as {existing_relation.relation_type}.', 'warning')
# return redirect(f"/patient_detail/{id}")
# patientParentRelation=PatientParentRelation(patient_id=patient_id,parent_id=relative_id,relation_type=relation)
# db.session.add(patientParentRelation)
# db.session.commit()
# print("add ho gya")
# flash('Relative has been linked successfully', 'success')
# return redirect(f"/patient_detail/{id}")
# return redirect(f"/patient_detail/{id}")
# @app.route("/patients")
# def patients():
# patients = User.query.filter_by(user_type='patient').all()
# return render_template('patients.html', patients=patients)
# @app.route("/doctors")
# def doctors():
# doctors = User.query.filter_by(user_type='doctor').all()
# return render_template('doctors.html', doctors=doctors)
# @app.route('/chat')
# def chat():
# if 'username' not in session:
# return redirect(url_for('login'))
# users = User.query.filter(User.username != session['username']).all()
# return render_template('chat.html', users=users, current_user=session['username'])
# @app.route('/private_chat/<username>')
# def private_chat(username):
# if 'username' not in session:
# return redirect(url_for('login'))
# current_user = session['username']
# chat_room = '_'.join(sorted([current_user, username]))
# messages = Message.query.filter_by(room=chat_room).order_by(Message.timestamp.asc()).all()
# return render_template('private_chat.html', room=chat_room, username=username, current_user=current_user, messages=messages)
# @socketio.on('private_message')
# def handle_private_message(data):
# room = data['room']
# message = data['message']
# sender = session['username']
# new_message = Message(room=room, sender=sender, content=message)
# db.session.add(new_message)
# db.session.commit()
# emit('private_message', {'sender': sender, 'message': message}, room=room)
# @socketio.on('join')
# def on_join(data):
# room = data['room']
# join_room(room)
# send(f"{session['username']} has entered the room.", room=room)
# if __name__ == '__main__':
# with app.app_context():
# # db.drop_all()
# db.create_all()
# socketio.run(app, debug=True)
# Main application file
import uuid
from flask import Flask, jsonify, render_template, redirect, url_for, request, session, flash
from flask_sqlalchemy import SQLAlchemy
from flask_socketio import SocketIO, join_room, leave_room, send, emit
from flask_login import LoginManager, current_user, login_required, login_user, UserMixin, logout_user
from werkzeug.security import generate_password_hash, check_password_hash
from datetime import datetime
app = Flask(__name__)
app.config['SECRET_KEY'] = 'your_secret_key'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///chat.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
login_manager = LoginManager()
login_manager.init_app(app)
socketio = SocketIO(app)
# Models
class Message(db.Model):
id = db.Column(db.Integer, primary_key=True)
room = db.Column(db.String(150), nullable=False)
sender = db.Column(db.String(150), nullable=False)
content = db.Column(db.Text, nullable=False)
timestamp = db.Column(db.DateTime, default=datetime.utcnow)
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
fname = db.Column(db.String(120), nullable=False)
lname = db.Column(db.String(120), nullable=False)
user_type = db.Column(db.String(20), nullable=False)
password = db.Column(db.String(120), nullable=False)
class Prescription(db.Model):
id = db.Column(db.Integer, primary_key=True)
doctor_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
patient_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
prescription_text = db.Column(db.Text, nullable=False)
created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)
class PatientParentRelation(db.Model):
id = db.Column(db.Integer, primary_key=True)
patient_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
parent_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
relation_type = db.Column(db.String(50), nullable=False)
@login_manager.user_loader
def load_user(id):
return User.query.get(int(id))
@app.route("/")
def index():
return render_template("index.html")
@app.route("/register", methods=['POST', 'GET'])
def register():
if request.method == 'POST':
email = request.form.get('email')
password = request.form.get('password')
username = request.form.get('uname')
fname = request.form.get('fname')
lname = request.form.get('lname')
user_type = request.form.get('user_type')
if user_type not in ["doctor", "patient"]:
flash("Please select a valid user type (Doctor or Patient)", "error")
return render_template("register.html", email=email, uname=username, fname=fname, lname=lname)
password = generate_password_hash(password, method='pbkdf2:sha256')
user = User(username=username, email=email, fname=fname, lname=lname, password=password, user_type=user_type)
db.session.add(user)
db.session.commit()
flash("User is registered successfully", "success")
return redirect('/login')
return render_template("register.html")
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
email = request.form.get('email')
password = request.form.get('password')
user = User.query.filter_by(email=email).first()
if user and check_password_hash(user.password, password):
session['username'] = user.username
flash(f'Logged in successfully as {user.username}', 'success')
login_user(user)
if user.user_type == "doctor":
return redirect('/patients')
if user.user_type == "patient":
return redirect('/doctors')
return redirect('/')
else:
flash('Invalid Credentials', 'warning')
return redirect('/login')
return render_template("login.html")
@app.route("/logout")
def logout():
flash(f'{current_user.username} is logged out', 'warning')
logout_user()
return redirect("/")
@app.route("/patients")
def patients():
patients = User.query.filter_by(user_type='patient').all()
return render_template('patients.html', patients=patients)
@app.route("/doctors")
def doctors():
doctors = User.query.filter_by(user_type='doctor').all()
return render_template('doctors.html', doctors=doctors)
@app.route("/patient_detail/<int:id>", methods=["GET", "POST"])
def patient_detail(id):
patient = User.query.get(id)
# prescriptions = Prescription.query.filter_by(patient_id=id).all()
# prescriptions = db.session.query(Prescription, User.username)\
# .join(User, Prescription.doctor_id == User.id)\
# .all()
prescriptions = db.session.query(Prescription, User.username).join(User, Prescription.doctor_id == User.id).filter(Prescription.patient_id == id).all()
# relations = db.session.query(PatientParentRelation, User.username).join(User, PatientParentRelation.parent_id == User.id).filter(PatientParentRelation.patient_id == id).all()
#patient is relation with some other patient
child_relations = db.session.query(PatientParentRelation, User.username,User.fname,User.lname).join(User, PatientParentRelation.parent_id == User.id).filter(PatientParentRelation.patient_id == id).all()
#some other patient is related to the patient
parent_relations = db.session.query(PatientParentRelation, User.username,User.fname,User.lname).join(User, PatientParentRelation.patient_id == User.id).filter(PatientParentRelation.parent_id == id).all()
# print("a babu gandu ho kya")
for child in child_relations:
print(child)
# print("a babu gandu ho kya")
if request.method == 'POST':
prescription_text = request.form.get('prescription_text')
doctor_id = current_user.id
prescription = Prescription(doctor_id=doctor_id, patient_id=id, prescription_text=prescription_text)
db.session.add(prescription)
db.session.commit()
flash('Prescription added successfully', 'success')
return redirect(f"/patient_detail/{id}")
return render_template("patient_detail.html", patient=patient,prescriptions=prescriptions,child_relations=child_relations,parent_relations=parent_relations)
@app.route("/add_relative/<int:id>",methods=["GET","POST"])
def add_parent(id):
if request.method=="POST":
patient_id=id #Patient
relative=User.query.filter_by(email=request.form.get("parent_email")).first() #Relative or Relative
relation=request.form.get("relationship")#Relation
if not relative:
flash('Sorry No such Relative has been registered', 'warning')
return redirect(f"/patient_detail/{id}")
relative_id=relative.id
# Check if the relation already exists
existing_relation = PatientParentRelation.query.filter_by(patient_id=patient_id, parent_id=relative.id).first()
if existing_relation:
flash(f'Relative {relative.username} is already linked as {existing_relation.relation_type}.', 'warning')
return redirect(f"/patient_detail/{id}")
patientParentRelation=PatientParentRelation(patient_id=patient_id,parent_id=relative_id,relation_type=relation)
db.session.add(patientParentRelation)
db.session.commit()
print("add ho gya")
flash('Relative has been linked successfully', 'success')
return redirect(f"/patient_detail/{id}")
return redirect(f"/patient_detail/{id}")
@app.route('/chat')
def chat():
if 'username' not in session:
return redirect(url_for('login'))
users = User.query.filter(User.username != session['username']).all()
return render_template('chat.html', users=users, current_user=session['username'])
@app.route('/private_chat/<username>')
def private_chat(username):
if 'username' not in session:
return redirect(url_for('login'))
current_user = session['username']
chat_room = '_'.join(sorted([current_user, username]))
messages = Message.query.filter_by(room=chat_room).order_by(Message.timestamp.asc()).all()
return render_template('private_chat.html', room=chat_room, username=username, current_user=current_user, messages=messages)
@socketio.on('private_message')
def handle_private_message(data):
room = data['room']
message = data['message']
sender = session['username']
new_message = Message(room=room, sender=sender, content=message)
db.session.add(new_message)
db.session.commit()
emit('private_message', {'sender': sender, 'message': message}, room=room)
@socketio.on('join')
def on_join(data):
room = data['room']
join_room(room)
send(f"{session['username']} has entered the room.", room=room)
@socketio.on('call_user')
def call_user(data):
room = data['room']
caller = data['caller']
callee = data['callee']
emit('incoming_call', {'caller': caller, 'room': room}, room=callee)
@app.route('/video_call/<username>')
@login_required
def video_call(username):
if 'username' not in session:
return redirect(url_for('login'))
current_user = session['username']
chat_room = '_'.join(sorted([current_user, username]))
socketio.emit('call_user', {'caller': current_user, 'callee': username, 'room': chat_room}, room=username)
return render_template('video_call.html', room=chat_room, username=username, current_user=current_user)
if __name__ == '__main__':
import os
port = int(os.environ.get("PORT", 5000))
with app.app_context():
db.create_all()
socketio.run(app, host="0.0.0.0", port=port, debug=True, allow_unsafe_werkzeug=True)