Skip to content

Commit 111176c

Browse files
author
Naím Rodríguez
authored
added cookies for user ver0.1
added cookies to identify users in chat room's added the function to delete the cookies to get a new username
1 parent a62dce9 commit 111176c

File tree

3 files changed

+72
-4
lines changed

3 files changed

+72
-4
lines changed

server.py

+50-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from flask import Flask, redirect, request, render_template, url_for, send_file
1+
from flask import Flask, redirect, request, render_template, url_for, send_file, make_response
22
import secrets
33
import os
44
import time
@@ -7,11 +7,44 @@
77

88
app = Flask(__name__)
99
app.config['SECRET_KEY'] = secrets.token_hex(32)
10+
user_list = []
1011

1112
# Home page
12-
@app.route("/")
13+
@app.get("/")
1314
def home():
14-
return render_template("home.html")
15+
# If there is no cookies need to get them
16+
if not request.cookies.get('cookie_user'):
17+
return render_template("home-post.html")
18+
# If there is already cookies on the browser return the home-get.html
19+
else:
20+
return render_template("home-get.html")
21+
22+
23+
# to set the cookie for the user
24+
@app.post("/")
25+
def home_post():
26+
# If user has been requested
27+
if request.form['user']:
28+
# If user is not elected already
29+
if request.form['user'] not in user_list:
30+
# Create a cookie for the user
31+
resp = make_response(render_template("home-get.html"))
32+
# Cookie for 20 minutes
33+
tok = secrets.token_urlsafe(16)
34+
resp.set_cookie("cookie_user", tok, max_age=60*20)
35+
36+
# add user to session
37+
user_list.append(request.form['user'])
38+
print(user_list)
39+
# redirect to the home-get page
40+
return resp
41+
# user in user_list
42+
else:
43+
return "usuario ya escogido"
44+
# user not requested
45+
else:
46+
return render_template("home-post.html")
47+
1548

1649
# Create a room
1750
@app.route("/create/", methods=['GET'])
@@ -22,6 +55,7 @@ def create():
2255
new_file_room.close()
2356
return redirect(f"/room/{number}")
2457

58+
2559
# GET the join.html or POST to join a room
2660
@app.route("/join/", methods=['GET', 'POST'])
2761
def join():
@@ -30,6 +64,7 @@ def join():
3064
if request.method == 'POST':
3165
return redirect(f"/room/{request.form.get('room_key')}")
3266

67+
3368
# Get the messages from the room
3469
@app.get("/room/<string:number>/")
3570
def room_get(number):
@@ -41,15 +76,18 @@ def room_get(number):
4176
else:
4277
return "no existe esa sala"
4378

79+
4480
# Post a message on the room
4581
@app.post("/room/<string:number>/")
4682
def room_post(number):
4783
content_room = open(f"{os.getcwd()}/chats/room.{number}.txt", "a", encoding='utf-8')
4884
if request.form['message']:
4985
time_msg = f"[{time.localtime().tm_hour}:{time.localtime().tm_min}.{time.localtime().tm_sec}]"
50-
content_room.write(f"{time_msg} - {request.form['message']}\n")
86+
content_room.write(f"{time_msg} - *{request.cookies.get('cookie_user')}*: {request.form['message']}\n")
5187
return redirect(url_for("room_get", number=number))
5288

89+
90+
# Get or generate QR code for room
5391
@app.get("/room/<string:number>/qr/")
5492
def room_get_qr(number):
5593
# If the qr code of the room exists return it
@@ -62,6 +100,14 @@ def room_get_qr(number):
62100
return send_file(f"{os.getcwd()}/qrs/{number}.png")
63101

64102

103+
# Delete your cookies
104+
@app.route("/del-cookie/")
105+
def delete_cookies():
106+
resp = make_response(render_template("home-post.html"))
107+
resp.set_cookie("cookie_user", "", max_age=0)
108+
return resp
109+
110+
65111
if __name__ == "__main__":
66112
os.chdir(os.getcwd())
67113
app.run(host="0.0.0.0", port=80, debug=True)

templates/home-get.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<head>
3+
<title>Secmeet Home</title>
4+
</head>
5+
6+
<body>
7+
<a href="create">Create</a>
8+
<a href="join">Join</a>
9+
<a href="del-cookie">Delete cookies</a>
10+
</body>

templates/home-post.html

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<head>
3+
<title>Set your user</title>
4+
</head>
5+
6+
<body>
7+
<form action="/" method="post">
8+
<label>User for posting messages</label>
9+
<input type="text" name="user" id="user">
10+
<input type="submit" value="submit">
11+
</form>
12+
</body>

0 commit comments

Comments
 (0)