|
1
|
|
|
import re |
|
2
|
|
|
import secrets |
|
3
|
|
|
from datetime import datetime |
|
4
|
|
|
|
|
5
|
|
|
from flask import Flask, render_template, redirect, url_for, request, session |
|
6
|
|
|
from flask_sqlalchemy import SQLAlchemy |
|
7
|
|
|
|
|
8
|
|
|
from src.security import sha512 |
|
9
|
|
|
|
|
10
|
|
|
app = Flask(__name__) |
|
11
|
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///bday.db' |
|
12
|
|
|
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False |
|
13
|
|
|
db = SQLAlchemy(app) |
|
14
|
|
|
|
|
15
|
|
|
app.config.from_object(__name__) |
|
16
|
|
|
app.secret_key = secrets.token_urlsafe(32) |
|
17
|
|
|
|
|
18
|
|
|
USERNAME_PATTERN = r'^([\w -]){4,32}$' |
|
19
|
|
|
PASSWORD_PATTERN = r'^(.*(?=.{8,})(?=.*[a-zA-Z])(?=.*\d).*){8,32}$' |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
# DB Model |
|
23
|
|
|
class User(db.Model): |
|
24
|
|
|
id = db.Column(db.Integer, primary_key=True, autoincrement=True) |
|
25
|
|
|
pseudo = db.Column(db.String(32)) |
|
26
|
|
|
password = db.Column(db.String(128)) |
|
27
|
|
|
birthday = db.Column(db.String(10), default=None) |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
class Birthday(db.Model): |
|
31
|
|
|
id = db.Column(db.Integer, primary_key=True, autoincrement=True) |
|
32
|
|
|
user_id = db.Column(db.Integer) |
|
33
|
|
|
person_name = db.Column(db.String(32)) |
|
34
|
|
|
person_birthday = db.Column(db.String(10)) |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
db.drop_all() |
|
38
|
|
|
db.create_all() |
|
39
|
|
|
|
|
40
|
|
|
dummy_user = User( |
|
41
|
|
|
pseudo="dummy", |
|
42
|
|
|
password=( |
|
43
|
|
|
"ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185" |
|
44
|
|
|
"f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff" |
|
45
|
|
|
), |
|
46
|
|
|
birthday="2001-12-11" |
|
47
|
|
|
) |
|
48
|
|
|
db.session.add(dummy_user) |
|
49
|
|
|
db.session.commit() |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
@app.route('/', methods=('GET', 'POST')) |
|
53
|
|
|
def index_page(): |
|
54
|
|
|
return render_template('index.jinja2') |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
@app.route('/auth/login', methods=('GET', 'POST')) |
|
58
|
|
|
def login_page(): |
|
59
|
|
|
if session.get('user'): |
|
|
|
|
|
|
60
|
|
|
return redirect(url_for('dashboard_page')) |
|
61
|
|
|
if request.method == 'POST': |
|
62
|
|
|
username = request.form['username'] |
|
63
|
|
|
password = request.form['password'] |
|
64
|
|
|
|
|
65
|
|
|
if username and password: |
|
66
|
|
|
login = User.query.filter_by( |
|
67
|
|
|
pseudo=username, |
|
68
|
|
|
password=sha512(password) |
|
69
|
|
|
).first() |
|
70
|
|
|
|
|
71
|
|
|
if login is not None: |
|
72
|
|
|
session['user'] = { |
|
73
|
|
|
'name': username, |
|
74
|
|
|
'id': login.id |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return redirect(url_for('dashboard_page')) |
|
78
|
|
|
|
|
79
|
|
|
return render_template('auth/login.jinja2') |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
@app.route('/auth/register', methods=('GET', 'POST')) |
|
83
|
|
|
def register_page(): |
|
84
|
|
|
if session.get('user'): |
|
|
|
|
|
|
85
|
|
|
return redirect(url_for('dashboard_page')) |
|
86
|
|
|
if request.method == 'POST': |
|
87
|
|
|
username = request.form['username'] |
|
88
|
|
|
password = request.form['password'] |
|
89
|
|
|
confirm_password = request.form['confirm_password'] |
|
90
|
|
|
birthday = request.form['date'] |
|
91
|
|
|
|
|
92
|
|
|
if ( |
|
93
|
|
|
re.match(USERNAME_PATTERN, username) |
|
94
|
|
|
and re.match(PASSWORD_PATTERN, password) |
|
95
|
|
|
and confirm_password |
|
96
|
|
|
and confirm_password == password |
|
97
|
|
|
and birthday |
|
98
|
|
|
): |
|
99
|
|
|
new_user = User( |
|
100
|
|
|
pseudo=username, |
|
101
|
|
|
password=sha512(password), |
|
102
|
|
|
birthday=birthday |
|
103
|
|
|
) |
|
104
|
|
|
|
|
105
|
|
|
db.session.add(new_user) |
|
106
|
|
|
db.session.commit() |
|
107
|
|
|
|
|
108
|
|
|
session['user'] = {'name': username} |
|
109
|
|
|
return redirect(url_for('dashboard_page')) |
|
110
|
|
|
|
|
111
|
|
|
return render_template( |
|
112
|
|
|
'auth/register.jinja2', |
|
113
|
|
|
USERNAME_PATTERN=USERNAME_PATTERN, |
|
114
|
|
|
PASSWORD_PATTERN=PASSWORD_PATTERN |
|
115
|
|
|
) |
|
116
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
@app.route('/dashboard', methods=('GET', 'POST')) |
|
119
|
|
|
def dashboard_page(): |
|
120
|
|
|
user = session.get('user') |
|
121
|
|
|
|
|
122
|
|
|
if not user: |
|
123
|
|
|
return redirect(url_for('login_page')) |
|
124
|
|
|
|
|
125
|
|
|
if not user.get('name'): |
|
126
|
|
|
return redirect(url_for('login_page')) |
|
127
|
|
|
|
|
128
|
|
|
if request.method == 'POST': |
|
129
|
|
|
username = request.form['username'] |
|
130
|
|
|
date = request.form['date'] |
|
131
|
|
|
|
|
132
|
|
|
if username and date: |
|
133
|
|
|
new_birthday = Birthday( |
|
134
|
|
|
person_name=username, |
|
135
|
|
|
person_birthday=date, |
|
136
|
|
|
user_id=user.get('id') |
|
137
|
|
|
) |
|
138
|
|
|
|
|
139
|
|
|
db.session.add(new_birthday) |
|
140
|
|
|
db.session.commit() |
|
141
|
|
|
|
|
142
|
|
|
birthdays = Birthday.query.filter_by(user_id=user.get('id')).all() |
|
143
|
|
|
now = datetime.now() |
|
144
|
|
|
|
|
145
|
|
|
return render_template( |
|
146
|
|
|
'dashboard.jinja2', |
|
147
|
|
|
birthdays=birthdays, |
|
148
|
|
|
today_birthdays=[ |
|
149
|
|
|
birthday |
|
150
|
|
|
for birthday in birthdays |
|
151
|
|
|
if birthday.person_birthday.endswith( |
|
152
|
|
|
f'-{now.month:02}-{now.day:02}' |
|
153
|
|
|
) |
|
154
|
|
|
] |
|
155
|
|
|
) |
|
156
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
@app.route('/auth/delete', methods=('GET', 'POST')) |
|
159
|
|
|
def delete_account_page(): |
|
160
|
|
|
user = session.get('user') |
|
161
|
|
|
|
|
162
|
|
|
if not user: |
|
163
|
|
|
return redirect(url_for('login_page')) |
|
164
|
|
|
|
|
165
|
|
|
if not user.get('name'): |
|
166
|
|
|
return redirect(url_for('login_page')) |
|
167
|
|
|
if request.method == 'POST' and user.get('name') == request.form.get( |
|
168
|
|
|
'account_name' |
|
169
|
|
|
): |
|
170
|
|
|
db.session.delete(User.query.filter_by(pseudo=user.get('name')).first()) |
|
171
|
|
|
db.session.commit() |
|
172
|
|
|
return redirect(url_for('logout')) |
|
173
|
|
|
return render_template('auth/delete.jinja2') |
|
174
|
|
|
|
|
175
|
|
|
|
|
176
|
|
|
@app.route('/legal') |
|
177
|
|
|
def legal_page(): |
|
178
|
|
|
return render_template('legal.jinja2') |
|
179
|
|
|
|
|
180
|
|
|
|
|
181
|
|
|
@app.route('/auth/edit', methods=('GET', 'POST')) |
|
182
|
|
|
def edit_page(): |
|
183
|
|
|
user = session.get('user') |
|
|
|
|
|
|
184
|
|
|
|
|
185
|
|
|
if not user: |
|
186
|
|
|
return redirect(url_for('login_page')) |
|
187
|
|
|
|
|
188
|
|
|
if not user.get('name'): |
|
189
|
|
|
return redirect(url_for('login_page')) |
|
190
|
|
|
|
|
191
|
|
|
if request.method == 'POST': |
|
192
|
|
|
if request.form.get('new_password'): |
|
193
|
|
|
new_password = request.form['new_password'] |
|
194
|
|
|
confirm_password = request.form['confirm_new_password'] |
|
195
|
|
|
old_password = request.form['old_password'] |
|
196
|
|
|
|
|
197
|
|
|
if ( |
|
198
|
|
|
User.query.filter_by( |
|
199
|
|
|
pseudo=user.get('name'), password=sha512(old_password) |
|
200
|
|
|
).first() |
|
201
|
|
|
and new_password == confirm_password |
|
202
|
|
|
): |
|
203
|
|
|
user = User.query.filter_by(pseudo=user.get('name')).first() |
|
204
|
|
|
user.password = sha512(new_password) |
|
205
|
|
|
db.session.commit() |
|
206
|
|
|
|
|
207
|
|
|
elif request.form.get('new_username'): |
|
208
|
|
|
new_username = request.form['new_username'] |
|
209
|
|
|
confirm_username = request.form['confirm_new_username'] |
|
210
|
|
|
|
|
211
|
|
|
if ( |
|
212
|
|
|
new_username == confirm_username |
|
213
|
|
|
and not User.query.filter_by(pseudo=new_username).first() |
|
214
|
|
|
): |
|
215
|
|
|
user = User.query.filter_by(pseudo=user.get('name')).first() |
|
216
|
|
|
user.pseudo = new_username |
|
217
|
|
|
db.session.commit() |
|
218
|
|
|
|
|
219
|
|
|
session['user'] = {'name': new_username} |
|
220
|
|
|
|
|
221
|
|
|
return render_template('auth/edit.jinja2') |
|
222
|
|
|
|
|
223
|
|
|
|
|
224
|
|
|
@app.route('/delete/<index>') |
|
225
|
|
|
def delete_user(index): |
|
226
|
|
|
user = session.get('user') |
|
227
|
|
|
|
|
228
|
|
|
if not user: |
|
229
|
|
|
return {} |
|
230
|
|
|
|
|
231
|
|
|
if not user.get('name'): |
|
232
|
|
|
return {} |
|
233
|
|
|
|
|
234
|
|
|
db.session.delete( |
|
235
|
|
|
Birthday.query.filter_by( |
|
236
|
|
|
user_id=user.get('id'), |
|
237
|
|
|
).all()[int(index)] |
|
238
|
|
|
) |
|
239
|
|
|
db.session.commit() |
|
240
|
|
|
return {} |
|
241
|
|
|
|
|
242
|
|
|
|
|
243
|
|
|
@app.route('/api/search/<user>') |
|
244
|
|
|
def search_user(user): |
|
245
|
|
|
found_user: User = User.query.filter_by(pseudo=user).first() |
|
246
|
|
|
if not found_user: |
|
247
|
|
|
return {} |
|
248
|
|
|
|
|
249
|
|
|
return { |
|
250
|
|
|
'id': found_user.id, |
|
251
|
|
|
'name': found_user.pseudo, |
|
252
|
|
|
'birthday': found_user.birthday |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
|
|
256
|
|
|
@app.route('/logout/') |
|
257
|
|
|
def logout(): |
|
258
|
|
|
if session.get('user'): |
|
259
|
|
|
session.pop('user') |
|
260
|
|
|
|
|
261
|
|
|
return redirect(url_for('index_page')) |
|
262
|
|
|
|
|
263
|
|
|
|
|
264
|
|
|
if __name__ == '__main__': |
|
265
|
|
|
app.run(debug=True) |
|
266
|
|
|
|