-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
108 lines (78 loc) · 2.86 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
from flask import (Flask, render_template, request,
jsonify, session, redirect, url_for)
from models import dbManager
from string import ascii_letters, digits
import chess
app = Flask(__name__)
app.secret_key = "b524f20474380e639312586bf20ef9ab95e2b5395dbe405efba2949e7870f7f3"
@app.route("/")
def index():
print(session )
return render_template("index.html", session=session)
@app.route("/login", methods=["POST", "GET"])
def login():
if request.method == "GET":
return render_template("login.html")
else:
datas = request.get_json()
print(request)
username = datas.get('username', '').strip()
password = datas.get('password', '').strip()
if not username:
return jsonify({"success": False, "message": "Username not defined."})
if not password:
return jsonify({"success": False, "message": "Password not defined."})
if not dbManager.valid_user(username, password):
return jsonify({"success": False, "message": "Invalid username or password"})
session['username'] = username
return jsonify({"success": True})
@app.route("/signup", methods=["POST", "GET"])
def signup():
if request.method == "GET":
return render_template("signup.html")
else:
datas = request.get_json()
username = datas.get('username', '').strip()
password = datas.get('password', '').strip()
if not username:
return jsonify({"success": False, "message": "Username not defined."})
if len(username) < 4:
return jsonify({"success": False, "message": "Too short username."})
valid_chars = ascii_letters + digits + "_"
for char in set(username):
if char not in valid_chars:
return jsonify({'success': False, "message": "Special characters forbidded in username."})
if dbManager.username_in_use(username):
return jsonify({"success": False, "message": "Username already in use."})
if not password:
return jsonify({"success": False, "message": "Password not defined."})
if len(password) < 8:
return jsonify({"success": False, "message": "Too short password."})
if not dbManager.password_valid(password):
return jsonify({"succes": False, "message": "Insecure password."})
dbManager.save_user(username, password)
return jsonify({"success": True})
@app.route("/game", methods=["GET", "POST"])
def game():
if request.method == "GET":
if 'username' not in session:
return redirect(url_for('login'))
elif 'id' not in request.args:
username = session['username']
id = chess.create_or_get_game(username)
return redirect(url_for('game', id=id))
else:
id = request.args['id']
return render_template("game.html", session=session, gameId=id)
elif request.method == "POST":
data = request.get_json()
message = data.get('mesage')
return jsonify({"message": "message recu"})
@app.route("/friendly")
def friendly():
return ""
@app.route("/computer")
def computer():
return ""
if __name__ == "__main__":
app.run(debug=True)