-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
76 lines (66 loc) · 3.25 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
from flask import Flask, render_template, request, flash, session, url_for, redirect
from flask_session import Session
from flask_socketio import SocketIO, emit
app = Flask(__name__)
app.config['SECRET_KEY'] = 'POP'
app.config['SESSION_PERMANENT'] = False
app.config['SESSION_TYPE'] = 'filesystem'
Session(app)
socketio = SocketIO(app)
channels = []
channels_name = []
messages = []
@app.route('/', methods=['GET', 'POST'])
@app.route('/home', methods=['GET', 'POST'])
def home():
if session.get('username') == None:
session['username'] = None
if request.method == "POST":
if request.form.get('display_name'):
session['username'] = request.form.get('display_name')
flash(f'You\'re now logged in as {session["username"]}', 'success')
elif request.form.get('channel'):
if request.form.get('channel') in channels_name:
flash(f'This channel name already exists, please try again!!', 'danger')
return render_template('home.html', display_name=session['username'], channels=channels)
session['channel'] = request.form.get('channel')
flash(f'Channel {session["channel"]} successfully created', 'success')
channels.append({'name': session['channel'], 'messages': []})
channels_name.append(request.form.get('channel'))
return render_template('home.html', display_name=session['username'], channels=channels)
@app.route('/<channel>', methods=["GET", "POST"])
def messages(channel):
if session.get('username') == None:
session['username'] = None
session['channel'] = None
if session.get('channel') == None:
session['channel'] = None
if request.method == "POST":
if request.form.get('display_name'):
session['username'] = request.form.get('display_name')
flash(f'You\'re now logged in as {session["username"]}', 'success')
elif request.form.get('channel'):
if request.form.get('channel') in channels_name:
flash(f'This channel name already exists, please try again!!', 'danger')
return render_template('home.html', display_name=session['username'], channels=channels)
session['channel'] = request.form.get('channel')
flash(f'Channel {session["channel"]} successfully created', 'success')
channels.append({'name': session['channel'], 'messages': []})
channels_name.append(request.form.get('channel'))
for i in channels:
if i['name'] == channel:
messages = i['messages']
return render_template('messages.html', messages=messages, display_name=session['username'], channels=channels, heading=channel)
@app.route('/logout', methods=['POST', 'GET'])
def logout():
session['username'] = None
flash(f'You\'re now logged out', 'primary')
return redirect('home')
@socketio.on('store message')
def store_message(data):
for i in channels:
if i['name'] == data['channel']:
i['messages'].append(data['message'])
if len(i['messages']) >= 100:
i['messages'].remove(i['messages'][0])
emit('display message', {'message': data['message']}, broadcast=True)