-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtempCodeRunnerFile.py
More file actions
237 lines (193 loc) · 9.65 KB
/
tempCodeRunnerFile.py
File metadata and controls
237 lines (193 loc) · 9.65 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
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__':
with app.app_context():
db.create_all()
socketio.run(app, debug=True)