-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.py
35 lines (28 loc) · 1.15 KB
/
chat.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
from flask import render_template, request, redirect, url_for, Blueprint, session
from helpers import authorize, myDb, login_required
chat = Blueprint('chat', __name__, template_folder='templates')
@chat.route("/")
@login_required
@authorize(["alum"])
def index():
# Create a cursor to execute queries
cursor = myDb.cursor(dictionary=True)
# Get messages
cursor.execute("SELECT `alumni_chat_messages`.*, `user`.`first_name`, `user`.`last_name` FROM `alumni_chat_messages` INNER JOIN `user` ON `user`.`id` = `alumni_chat_messages`.`user_id` ORDER BY `timestamp` DESC")
messages = cursor.fetchall()
myDb.commit()
cursor.close()
return render_template("chat.html", messages=messages)
@chat.route("/send", methods=["POST"])
@login_required
@authorize(["alum"])
def send():
# Create a cursor to execute queries
cursor = myDb.cursor(dictionary=True)
# Get message
message = request.form['message']
# Insert message
cursor.execute("INSERT INTO `alumni_chat_messages` (`message`, `user_id`) VALUES (%s, %s)", (message, session["userId"]))
myDb.commit()
cursor.close()
return redirect(url_for(".index"))