-
Notifications
You must be signed in to change notification settings - Fork 0
/
register.py
100 lines (87 loc) · 3.38 KB
/
register.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
from datetime import datetime, timedelta
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output, State
from global_vars import app, db
import hashlib
import os
# layout = html.Div([
# dcc.Location(id="register_location"),
# html.H1("Create Account"),
# html.Div(id="register_response"),
# html.Div(dcc.Input(id="name", type="text", placeholder="Name")),
# html.Div(dcc.Input(id="username", type="text", placeholder="Username")),
# html.Div(dcc.Input(id="password", type="password", placeholder="Password")),
# html.Div(dcc.Input(id="confirm_password", type="password", placeholder="Confirm Password")),
# html.Div(dbc.Button("Submit", id="submit"))
# ])
layout = html.Div([
dcc.Location(id="register_location"),
html.H1("Create Account"),
dbc.Form([
dbc.FormGroup([
dbc.Label("Name", className="mr-2"),
dbc.Input(id="name", type="text", placeholder="Enter Name")
], className="mr-3"),
dbc.FormGroup([
dbc.Label("Username", className="mr-2"),
dbc.Input(id="register_username", type="text", placeholder="Enter Username")
], className="mr-3"),
dbc.FormGroup([
dbc.Label("Password", className="mr-2"),
dbc.Input(id="register_password", type="password", placeholder="Enter Password")
], className="mr-3"),
dbc.FormGroup([
dbc.Input(id="confirm_password", type="password", placeholder="Confirm Password")
], className="mr-3"),
html.Div(id="register_response"),
dbc.Button("Submit", id="register_submit", color="primary")
]),
])
@app.callback(
Output("register_response", "children"),
Output("register_location", "pathname"),
Input("register_submit", "n_clicks"),
State("register_username", "value"),
State("register_password", "value"),
State("name", "value"),
State("confirm_password", "value")
)
def on_sumbit(submit_clicks, username, password, name, confirm_password):
if username is None or password is None or name is None or confirm_password is None:
return [dash.no_update] * 2
if password != confirm_password:
error = dbc.Alert("Passwords do not match", color="danger")
return error, dash.no_update
user = db.users.find_one({"username": username})
if user is not None:
error = dbc.Alert("The username '%s' is already in use" % username, color="danger")
return error, dash.no_update
salt = os.urandom(32)
password = hashlib.pbkdf2_hmac('sha256', password.encode('utf-8'), salt, 100000)
preferences = []
users = list(db.users.find())
for task in db.tasks.find():
avg_pref = 0
for every_user in users:
for pref in every_user["preferences"]:
if pref["task"] == task["_id"]:
avg_pref += pref["preference"]
avg_pref /= len(users)
preferences.append({
"task": task["_id"],
"preference": avg_pref
})
db.users.insert_one({
"name": name,
"username": username,
"password": password,
"salt": salt,
"preferences": preferences,
"active": True,
"last_activated": datetime.now(),
"active_seconds": 0
})
return "", "/login"