-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsignupPage.py
More file actions
244 lines (227 loc) · 8.4 KB
/
signupPage.py
File metadata and controls
244 lines (227 loc) · 8.4 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import base64
import dash_bootstrap_components as dbc
import dash_html_components as html
from dash.dependencies import Input, Output, State
from dash.exceptions import PreventUpdate
from app import app, database
# ---------------------------------------- DATA ----------------------------------------
gender_int_to_str = {"1": "Female", "2": "Male"}
# --------------------------------------- IMAGES ---------------------------------------
logo_image = "logo.png"
ds_logo_encoded = base64.b64encode(open(logo_image, "rb").read())
ds_logo_decoded = f"data:image/png;base64,{ds_logo_encoded.decode()}"
# --------------------------------------- CARDS ----------------------------------------
# create signup card with text fields and buttons to create account and go back to login page
signup_card = dbc.Card(
[
dbc.CardImg(src=ds_logo_decoded, top=True),
dbc.CardBody(
[
# text fields for user to input information
dbc.Input(
id="input_user_name_signup",
placeholder="user name",
type="text",
className="mb-3",
),
dbc.Input(
id="input_password_signup",
placeholder="password",
type="text",
className="mb-3",
),
dbc.Input(
id="input_first_name_signup",
placeholder="first name",
type="text",
className="mb-3",
),
dbc.Input(
id="input_last_name_signup",
placeholder="last name",
type="text",
className="mb-3",
),
dbc.InputGroup(
[
# create dropdown menu for gender
dbc.InputGroupAddon(
"Gender",
addon_type="prepend",
),
dbc.Select(
id="input_gender_select",
options=[
{"label": "Female", "value": 1},
{"label": "Male", "value": 2},
],
),
],
className="mb-3",
),
# create buttons below text boxes
dbc.Row(
[
dbc.Col(
dbc.Button(
# back button
children="back",
id="button_back_signup",
color="primary",
className="m-3",
href="/login",
external_link=True,
),
width={"size": "auto"},
),
dbc.Col(
dbc.Button(
# create button
children="create",
id="button_create_signup",
color="primary",
className="m-3",
),
width={"size": "auto"},
),
],
justify="center",
),
]
),
],
color="light",
inverse=True,
)
# alert for visual confirmation
alert_signup = dbc.Alert(
id="alert_signup",
dismissable=True,
is_open=False,
)
# --------------------------------------- LAYOUT ---------------------------------------
layout = dbc.Container(
[
html.Div(
[
# row1: signup
dbc.Row(
dbc.Col(
signup_card,
width={"size": 6, "offset": 3},
),
align="center",
className="m-5",
),
# row2: alert
dbc.Row(
dbc.Col(
alert_signup,
width={"size": 6, "offset": 3},
),
align="center",
className="m-5",
),
]
)
]
)
# ------------------------------------- CALLBACKS --------------------------------------
# AppCallback to create new user
@app.callback(
[
Output("alert_signup", "is_open"),
Output("alert_signup", "color"),
Output("alert_signup", "children"),
],
[Input("button_create_signup", "n_clicks")],
[
State("input_user_name_signup", "value"),
State("input_password_signup", "value"),
State("input_first_name_signup", "value"),
State("input_last_name_signup", "value"),
State("input_gender_select", "value"),
],
)
def create_new_user(
button_create_signup_n_clicks,
input_user_name_signup_value,
input_password_signup_value,
input_first_name_signup_value,
input_last_name_signup_value,
input_gender_select_value,
):
if button_create_signup_n_clicks:
if (
# if user name is not blank
input_user_name_signup_value is not None
and ~input_user_name_signup_value.isspace()
):
if (
# if password is not blank
input_password_signup_value is not None
and ~input_password_signup_value.isspace()
):
if (
# if first name is not blank
input_first_name_signup_value is not None
and ~input_first_name_signup_value.isspace()
):
if (
# if last name is not blank
input_last_name_signup_value is not None
and ~input_last_name_signup_value.isspace()
):
if input_gender_select_value is not None:
# if gender is not blank
database.create_new_user(
# create new user with user inputs
first_name=input_first_name_signup_value,
last_name=input_last_name_signup_value,
gender=gender_int_to_str[input_gender_select_value],
email=input_user_name_signup_value,
password=input_password_signup_value,
)
return (
# changes alert attributes for visual feedback
True,
"success",
"Account created successfully, please go back and login with your new details",
)
else:
return (
# changes alert attributes for visual feedback
True,
"danger",
"Please enter your gender.",
)
else:
return (
# changes alert attributes for visual feedback
True,
"danger",
"Please enter your last name.",
)
else:
return (
# changes alert attributes for visual feedback
True,
"danger",
"Please enter your first name.",
)
else:
return (
# changes alert attributes for visual feedback
True,
"danger",
"Please enter your password.",
)
else:
return (
# changes alert attributes for visual feedback
True,
"danger",
"Please enter your username.",
)
else:
raise PreventUpdate