-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
226 lines (149 loc) · 6.29 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
import os
from socket import socket
from time import localtime, strftime
from flask import Flask, render_template, redirect, url_for, flash
from flask_login import LoginManager, login_user, current_user, login_required, logout_user
from flask_socketio import SocketIO, send, emit, join_room, leave_room
from passlib.hash import pbkdf2_sha256
from wtform_fields import *
from models import *
# Configurations
app = Flask(__name__)
app.secret_key = os.environ.get('SECRET')
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL')
db.init_app(app)
engine_container = db.get_engine(app)
db = SQLAlchemy(app)
socketio = SocketIO(app)
CHATROOMS = ["courses", "coop", "resume", "stand-up", "other"]
login = LoginManager(app)
login.init_app(app)
@login.user_loader
def load_user(id):
return User.query.get(int(id))
# Route for login page
@app.route("/", methods=['GET', 'POST'])
def index():
user_login_form = UserLoginForm()
if user_login_form.validate_on_submit():
cur_user = User.query.filter_by(username = user_login_form.username.data).first()
login_user(cur_user)
return redirect(url_for('chat'))
return render_template("login.html", form = user_login_form)
@app.route("/register", methods=['GET', 'POST'])
def register():
user_reg_form = RegistrationForm()
if user_reg_form.validate_on_submit():
current_username = user_reg_form.username.data
current_password = user_reg_form.password.data
hash_password = pbkdf2_sha256.hash(current_password) # hash password before adding to DB
cur_user = User(username = current_username, password = hash_password)
db.session.add(cur_user)
db.session.commit() # add the users data to the DB
flash('Registered Successfully')
return redirect(url_for('index'))
return render_template("index.html", form = user_reg_form)
@app.route("/chat", methods=['GET', 'POST'])
def chat():
if not current_user.is_authenticated:
flash('Must be logged in before accessing chat')
return redirect(url_for('index'))
user_room_add = AddRoomForm()
if user_room_add.validate_on_submit():
new_room = user_room_add.roomname.data
if new_room.lower() not in CHATROOMS:
CHATROOMS.append(new_room)
return render_template('main.html', username=current_user.username, form = user_room_add, rooms=CHATROOMS)
@app.route("/logout", methods=['GET'])
def logout():
logout_user()
flash('Logout successful')
return redirect(url_for('index'))
@app.route("/end", methods=['GET'])
def logout():
logout_user()
flash('Logout successful')
return redirect(url_for('index'))
@app.route("/leave", methods=['GET'])
def logout():
logout_user()
flash('Logout successful')
return redirect(url_for('index'))
def cleanup(session):
session.close()
engine_container.dispose()
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404
@socketio.on('incoming-message')
def on_message(data):
message = data["message"]
username = data["username"]
time = strftime('%b-%d %I:%M%p', localtime())
send({"username": username, "message": message, "time": time}, room=data["room"])
@socketio.on('join')
def join(data):
join_room(data['room'])
send({'message': data['username'] + " has joined " + data['room']}, room=data['room'])
@socketio.on('leave')
def leave(data):
leave_room(data['room'])
send({'message': data['username'] + " has left the room"}, room=data['room'])
@app.route("/create", methods=['GET', 'POST'])
def register():
user_reg_form = RegistrationForm()
if user_reg_form.validate_on_submit():
current_username = user_reg_form.username.data
current_password = user_reg_form.password.data
hash_password = pbkdf2_sha256.hash(current_password) # hash password before adding to DB
cur_user = User(username = current_username, password = hash_password)
db.session.add(cur_user)
db.session.commit() # add the users data to the DB
flash('Registered Successfully')
return redirect(url_for('index'))
return render_template("index.html", form = user_reg_form)
@app.route("/new", methods=['GET', 'POST'])
def register():
user_reg_form = RegistrationForm()
if user_reg_form.validate_on_submit():
current_username = user_reg_form.username.data
current_password = user_reg_form.password.data
hash_password = pbkdf2_sha256.hash(current_password) # hash password before adding to DB
cur_user = User(username = current_username, password = hash_password)
db.session.add(cur_user)
db.session.commit() # add the users data to the DB
flash('Registered Successfully')
return redirect(url_for('index'))
return render_template("index.html", form = user_reg_form)
@app.route("/join", methods=['GET', 'POST'])
def chat():
if not current_user.is_authenticated:
flash('Must be logged in before accessing chat')
return redirect(url_for('index'))
user_room_add = AddRoomForm()
if user_room_add.validate_on_submit():
new_room = user_room_add.roomname.data
if new_room.lower() not in CHATROOMS:
CHATROOMS.append(new_room)
return render_template('main.html', username=current_user.username, form = user_room_add, rooms=CHATROOMS)
@app.route("/login", methods=['GET', 'POST'])
def index():
user_login_form = UserLoginForm()
if user_login_form.validate_on_submit():
cur_user = User.query.filter_by(username = user_login_form.username.data).first()
login_user(cur_user)
return redirect(url_for('chat'))
return render_template("login.html", form = user_login_form)
@app.route("/rooms", methods=['GET', 'POST'])
def chat():
if not current_user.is_authenticated:
flash('Must be logged in before accessing chat')
return redirect(url_for('index'))
user_room_add = AddRoomForm()
if user_room_add.validate_on_submit():
new_room = user_room_add.roomname.data
if new_room.lower() not in CHATROOMS:
CHATROOMS.append(new_room)
return render_template('main.html', username=current_user.username, form = user_room_add, rooms=CHATROOMS)
if __name__ == "__main__":
app.run()