-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
232 lines (188 loc) · 7 KB
/
app.py
File metadata and controls
232 lines (188 loc) · 7 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
from flask import Flask, render_template, redirect, url_for, Response, request, flash, get_flashed_messages
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import UserMixin, LoginManager, login_required, current_user, login_user, logout_user
from flask_bcrypt import Bcrypt
from sqlalchemy import and_
app = Flask(
__name__,
template_folder='templates',
static_folder='static',
static_url_path='/'
)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///todolist.sqlite3'
app.secret_key = "WOOoW, it's a super secret key you've ever seen!"
# === database-related ===
db = SQLAlchemy(app)
migrate = Migrate(app, db)
class User(db.Model, UserMixin):
__tablename__ = 'users'
uid = db.Column(db.Integer, primary_key=True)
name = db.Column(db.Text, nullable=False)
username = db.Column(db.Text, nullable=False, unique=True)
hashed_password = db.Column(db.Text, nullable=False)
def __init__(self, name: str, username: str, hashed_password: str):
self.name = name
self.username = username
self.hashed_password = hashed_password
def get_id(self) -> str:
return str(self.uid)
class Todo(db.Model):
__tablename__ = 'todos'
tid = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, nullable=False)
title = db.Column(db.Text)
description = db.Column(db.Text)
def __init__(self, user_id: int, title: str, description: str):
self.user_id = user_id
self.title = title
self.description = description
current_user: User
# === end of database-related ===
# === basic pages-related ===
@app.get('/')
def index() -> str:
todos: list[Todo] = None
if current_user.is_authenticated:
todos = Todo.query.filter(Todo.user_id == current_user.uid).all()
return render_template(
'index.html',
messages=get_flashed_messages(),
user=current_user,
todos=todos
)
@app.get('/about')
def about() -> str:
return render_template('about.html')
# === end of basic pages-related ===
# === login management-related ===
login_manager = LoginManager(app)
bcrypt = Bcrypt(app)
# returns the definition of "load a user"
@login_manager.user_loader
def load_user(uid: int) -> User:
return db.session.get(User, str(uid))
@login_manager.unauthorized_handler
def unauthorized_callback() -> Response:
flash("You're unauthorized!", 'warning')
return redirect(url_for('index'))
@app.route('/signup', methods=['GET', 'POST'])
def signup() -> str | Response:
if request.method == 'GET':
return render_template('signup.html', messages=get_flashed_messages())
# signup mechanism
username = request.form.get('username')
name = request.form.get('name')
password = request.form.get('password')
confirm_password = request.form.get('confirm-password')
# there are some empty inputs
if not username or not name or not password or not confirm_password:
flash('Fill all the required inputs!', 'warning')
return redirect(url_for('signup'))
# if the password and password confirmation are different
if password != confirm_password:
flash('The password and password confirmation have to be the same!', 'warning')
return redirect(url_for('signup'))
# if there is a same username in the database
if User.query.filter(User.username == username).first():
flash('Username already exists!', 'warning')
return redirect(url_for('signup'))
hashed_password = bcrypt.generate_password_hash(password)
db.session.add(User(
username=username,
name=name,
hashed_password=hashed_password
))
db.session.commit()
flash('Successfully added an account!', 'message')
return redirect(url_for('index'))
@app.route('/login', methods=['GET', 'POST'])
def login() -> str | Response:
# the user must be logout first
if current_user.is_authenticated:
flash("You're already login!", 'warning')
return redirect(url_for('index'))
if request.method == 'GET':
return render_template('login.html', messages=get_flashed_messages())
# login mechanism
username = request.form.get('username')
password = request.form.get('password')
targeted_user: User = User.query.filter(User.username == username).first()
# check the password
if targeted_user and bcrypt.check_password_hash(targeted_user.hashed_password, password):
login_user(targeted_user)
flash('Successfully login!', 'message')
else:
flash('Wrong username / password!', 'warning')
return redirect(url_for('login'))
return redirect(url_for('index'))
@app.get('/logout')
@login_required
def logout() -> Response:
logout_user()
flash('Successfully logout!', 'message')
return redirect(url_for('index'))
# === end of login management-related ===
# === todos-related ===
@app.post('/add-todo')
@login_required
def add_todo() -> Response:
title = request.form.get('title')
description = request.form.get('description')
db.session.add(Todo(
user_id=current_user.uid,
title=title,
description=description
))
db.session.commit()
return redirect(url_for('index'))
@app.route('/edit-todo/<string:tid>', methods=['GET', 'POST'])
@login_required
def edit_todo(tid: str) -> str | Response:
if request.method == 'GET':
# IMPORTANT !!!
# make sure the user's id and todo's id match
targeted_todo: Todo = Todo.query \
.filter(and_(Todo.tid == tid, Todo.user_id == current_user.uid)) \
.first()
# todo is not found
if not targeted_todo:
flash('The todo is not found!', 'warning')
return redirect(url_for('index'))
return render_template(
'edit-todo.html',
tid=tid,
old_title=targeted_todo.title,
old_description=targeted_todo.description
)
# IMPORTANT !!!
# make sure the user's id and todo's id match
todo: Todo = Todo.query \
.filter(and_(Todo.tid == tid, Todo.user_id == current_user.uid)) \
.first()
if todo:
todo.title = request.form.get('new-title')
todo.description = request.form.get('new-description')
db.session.commit()
flash('Successfully edited a todo!', 'message')
else:
flash('The todo is not found!', 'warning')
return redirect(url_for('index'))
@app.post('/delete-todo/<string:tid>')
@login_required
def delete_todo(tid: str) -> Response:
# IMPORTANT !!!
# make sure the user's id and todo's id match
todo: Todo = Todo.query \
.filter(and_(Todo.tid == tid, Todo.user_id == current_user.uid)) \
.first()
if todo:
db.session.delete(todo)
db.session.commit()
flash('Successfully deleted a todo!', 'message')
else:
flash('Todo was not found!', 'warning')
return redirect(url_for('index'))
# === end of todos-related ===
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)