-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebapp.py
More file actions
149 lines (129 loc) · 4.5 KB
/
Copy pathwebapp.py
File metadata and controls
149 lines (129 loc) · 4.5 KB
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
143
144
145
146
147
148
149
#!/usr/bin/env python3
from flask import Flask, render_template, request, jsonify, redirect, session
from flask_login import LoginManager, UserMixin, login_user, login_required, current_user, logout_user
import json
import pymongo
from bson import ObjectId
import re
from passlib.hash import sha256_crypt
from bson.son import SON
# config
app = Flask(__name__)
app.config["SECRET_KEY"] = "once egyszer the ido there volt kette little kecske"
login_manager = LoginManager()
login_manager.init_app(app)
# mongodb
conn = pymongo.MongoClient()
db = conn["torpedo-proto"]
coll_users = db["users"]
coll_games = db["games"]
# class for Users
class User(UserMixin):
def __init__(self, id, username, email, password):
self.id = id # this must be unicode!!!
self.username = username
self.email = email
self.password = password
@login_manager.user_loader
def load_user(id):
tmp = coll_users.find_one({"_id": ObjectId(id)})
return User(str(tmp["_id"]), tmp["username"], tmp["email"], tmp["password"])
@app.route("/login", methods=["GET", "POST"])
def login():
if current_user.is_authenticated:
return redirect("/secure")
if request.method == "GET":
return render_template("login.html")
else:
data = request.get_json()
tmp = coll_users.find_one({"username": data["username"]})
if tmp is None:
return jsonify({"success": False})
if sha256_crypt.verify(data["password"], tmp["password"]):
user = User(str(tmp["_id"]), tmp["username"], tmp["email"], tmp["password"])
login_user(user)
return jsonify({"success": True})
else:
return jsonify({"success": False})
def registration_error(field, message):
error = {
"field": field,
"message": message
}
return {"success": False, "error": error}
def registrate_user(data):
# extract data from json
username = data["username"]
email = data["email"]
password = data["password"]
repassword = data["repassword"]
# validate data
if re.match(r"[a-zA-Z0-9]{4,10}", username) is None:
return jsonify(registration_error("username", "Username's length must be 4-10 characters!"))
if re.match(r".{6,}", password) is None:
return jsonify(registration_error("password", "Password's length must be at least 6 characters!"))
if not password==repassword:
return jsonify(registration_error("password", "Passwords doesn't match!"))
if coll_users.find_one({"username": username}) is not None:
return jsonify(registration_error("username", "Username already in use!"))
if re.match("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", email) is None:
return jsonify(registration_error("email", "Invalid e-mail address!"))
if coll_users.find_one({"email": email}) is not None:
return jsonify(registration_error("email", "E-mail has already taken!"))
# if data is valid, encrypt password and save user to db
hash = sha256_crypt.encrypt(password)
coll_users.insert_one({"username": username, "email": email, "password": hash, "wins": 0, "losses": 0})
return jsonify({"success": True, "error": None})
@app.route("/registration", methods=["GET", "POST"])
def registration():
if current_user.is_authenticated:
return redirect("/")
if request.method == "GET":
return render_template("/registration.html")
else:
data = request.get_json()
return registrate_user(data)
@app.route("/logout")
@login_required
def logout():
logout_user()
return redirect("/")
@app.route("/secure")
@login_required
def secured():
context = {
"username": current_user.username
}
return render_template("secure.html", **context)
@app.route('/')
def hello_world():
return render_template("main.html")
def getGamesList(username):
return list(coll_games.find({"$or": [{"player1": username}, {"player2": username}]}).sort("date", pymongo.DESCENDING))
def getTopNPlayers(N):
tmp_list = list(coll_users.find().sort("wins", pymongo.DESCENDING).limit(N))
result = []
for i in range(0, len(tmp_list)):
result.append({"rank": i+1, "username": tmp_list[i]["username"], "wins": tmp_list[i]["wins"], "losses": tmp_list[i]["losses"]})
return result
@app.route("/user/<username>")
def user_page(username):
user = coll_users.find_one({"username": username})
if user is None:
return redirect("/")
else:
context = {
"username": user["username"],
"win": user["wins"],
"lose": user["losses"],
"games": getGamesList(username)
}
return render_template("user.html", **context)
@app.route("/toplist")
def toplist():
context = {
"toplist": getTopNPlayers(10)
}
return render_template("toplist.html", **context)
if __name__ == "__main__":
app.run(debug=True, threaded=True, host="0.0.0.0")