-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
145 lines (104 loc) · 3.99 KB
/
application.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
import os
from flask import Flask, render_template, redirect, url_for, flash
from flask_socketio import SocketIO, emit, send, join_room, leave_room
from passlib.hash import pbkdf2_sha256
from flask_login import LoginManager, login_user, current_user, login_required, logout_user
from time import localtime, strftime
from form_fields import *
from models import *
app = Flask(__name__)
app.secret_key = 'replace later'
# app.config["SECRET_KEY"] = os.getenv("SECRET_KEY")
# app.secret_key = os.getenv("SECRET_KEY")
# Initialize flask-socketio
socketio = SocketIO(app, manage_session=False)
# Creating Flack rooms
ROOMS = ["lunch", "movies", "games", "news", "general"]
# Configure database
# app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get('DATABASE_URL')
app.config["SQLALCHEMY_DATABASE_URI"] = 'postgres://rcrlddecvxotif:fe0adb891142e2d956e6f6209c2472c045f17ffb5e15aef4ee2c20ec525885dd@ec2-174-129-231-116.compute-1.amazonaws.com:5432/da364vd80pj157'
db = SQLAlchemy(app)
# Configure Flask Login
login = LoginManager(app)
login.init_app(app)
@login.user_loader
def load_user(id):
return User.query.get(int(id))
@app.route("/", methods=["GET", "POST"])
def index():
reg_form = RegistrationForm()
# Update the DB if the registraion details are valid.
if reg_form.validate_on_submit():
username = reg_form.username.data
password = reg_form.password.data
# Hash the password
hashed_pswd = pbkdf2_sha256.hash(password)
# Add user to DB
user = User(username=username, password=hashed_pswd)
db.session.add(user)
db.session.commit()
# Flash messages
flash('Registered Successfully. Please Login!', 'success')
return redirect(url_for('login'))
return render_template('index.html', form=reg_form)
@app.route("/login", methods=["GET", "POST"])
def login():
""" Route for login"""
login_form = LoginForm()
# check if the login is valid
if login_form.validate_on_submit():
user_object = User.query.filter_by(username=login_form.username.data).first()
login_user(user_object)
return redirect(url_for('chat'))
return render_template('login.html', form=login_form)
@app.route("/chat", methods=["GET", "POST"])
def chat():
# if not current_user.is_authenticated:
# flash('Please Login!', 'danger')
# return redirect(url_for('login'))
# Remember current user so that we can display him
return render_template('chat.html', username=current_user.username,
rooms=ROOMS)
@app.route("/logout", methods=["GET"])
def logout():
logout_user()
flash('Logged out Successfully!', 'success')
return redirect(url_for('login'))
# @app.route("/create", methods=["GET"])
# def create(newchannel):
# ROOMS.append(newchannel)
# return render_template('chat.html', username=current_user.username,
# rooms=ROOMS)
@socketio.on('message')
def message(data):
print (data)
msg = data["msg"]
username = data["username"]
room = data["room"]
time_stamp = strftime('%b-%d %I:%M%p', localtime())
send({"username": username, "msg": msg, "time_stamp": time_stamp}, room=room)
@socketio.on('create')
def create(data):
ROOMS.append(data["newchannel"])
print ("ROOMS:", ROOMS)
# join_room(data['room'])
send({'msg': data['username'] + ' has created the #' + data['newchannel'] + " channel."},
room=data['room'])
emit('redirect', {
'url': url_for('chat'),
'newchannel': data['newchannel']
},
room=data['room'])
# redirect(url_for('chat'))
@socketio.on('join')
def join(data):
join_room(data['room'])
send({'msg': data['username'] + ' has joined the #' + data['room'] + " channel."}, room=data['room'])
@socketio.on('leave')
def leave(data):
leave_room(data['room'])
send({'msg': data['username'] + ' has left the #' + data['room'] + " channel."}, room=data['room'])
if __name__ == "__main__":
# socketio.run(app, debug=True)
# app.run()
socketio.run(app)