-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpyweb.py
More file actions
277 lines (207 loc) · 7.16 KB
/
Copy pathpyweb.py
File metadata and controls
277 lines (207 loc) · 7.16 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
"""IziPost main module"""
import sqlite3
import os
import hashlib
from flask.helpers import flash
from flask import (
Flask,
render_template,
url_for,
redirect,
request,
session,
)
app = Flask(__name__)
app.secret_key = "t\xdd\xe7\xe2\xda\xa2\xc0^\xd7%\x19t`\xfeg\x1e\xbe"
TITRE = "IziPost"
if not os.path.exists("instance"):
os.makedirs("instance")
def get_database():
"""Get database"""
database = sqlite3.connect(
os.path.join(app.instance_path, "flaskr.sqlite"),
detect_types=sqlite3.PARSE_DECLTYPES,
)
database.row_factory = sqlite3.Row
return database
def init_database():
"""Initialization of database"""
database = get_database()
with app.open_resource("schema.sql") as file:
database.executescript(file.read().decode("utf8"))
if not os.path.isfile("instance/flaskr.sqlite"):
init_database()
def hash_mdp(password):
"""Encrypt a string
Keyword arguments:\n
password -- String to encrypt
"""
not_hashed = hashlib.sha256()
not_hashed.update(password.encode("utf-8"))
return not_hashed.digest()
def database_insert_task(name, desc, owner_id, status):
"""Insert a task in the database
Keyword arguments:\n
name -- lastname of the user as string\n
desc -- description of the task as string\n
owner_id -- the ID of the owner as int\n
status -- status of the task (0 = normal, 1 = important, 2 = urgent, 3 = done)"""
database = get_database()
database.execute(
"INSERT INTO tasks (name, description, owner) VALUES (?, ?, ?, ?)",
(name, desc, owner_id, status),
)
database.commit()
def database_fetch_tasks():
"""get all tasks from a user"""
database = get_database()
tasks = database.execute(
"SELECT * FROM tasks t INNER JOIN users u on t.owner = u.id WHERE u.username = ? ORDER BY id desc",
(session["username"],),
).fetchall()
return tasks
def database_update_task(task_id, name, description, status):
"""Update a task in the database
Keyword arguments:\n
name -- lastname of the user as string\n
desc -- description of the task as string\n
owner_id -- the ID of the owner as int\n
status -- status of the task (0 = normal, 1 = important, 2 = urgent, 3 = done)"""
database = get_database()
database.execute(
"UPDATE tasks SET name = ?, description = ?, status = ? WHERE id = ?",
(
name,
description,
status,
task_id,
),
)
database.commit()
def database_get_task(task_id):
"""Get a task in the database
Keyword arguments:\n
task_id -- id of the task as int"""
database = get_database()
task = database.execute(
"SELECT * FROM tasks WHERE id = ?",
(task_id,),
).fetchall()
return task
@app.route("/createTask", methods=("GET", "POST"))
# def createTask(name, desc, status, owner_id):
def createTask():
if request.method == "POST":
task_status = request.form.get("selectSection")
name = request.form.get("TaskTitle")
desc = request.form.get("TaskContent")
database = get_database()
database.execute(
"INSERT INTO tasks (name, description, status, owner) VALUES (?, ?, ?, ?)",
# (name, desc, status, owner_id),
(name, desc, task_status, session["id"]),
)
database.commit()
return redirect(url_for("show_app"))
@app.route("/deleteTask", methods=("GET", "POST"))
def database_delete_tasks():
"""delete a task with a specific ID"""
if request.method == "POST":
database = get_database()
database.execute("DELETE FROM tasks WHERE id = ?", (request.form["task"],))
database.commit()
return redirect(url_for("show_app"))
### Index HTML ###
@app.route("/")
def index():
"""Index routing"""
# if "username" in session:
return render_template("index.html", title=TITRE)
### about HTML ###
@app.route("/about")
def about():
"""About routing"""
return render_template("about.html", title=TITRE)
@app.route("/iziPostApp")
def show_app():
"""App routing"""
return render_template(
"IziPostApp.html", tasksList=database_fetch_tasks(), title=TITRE
)
@app.route("/editPage", methods=("GET", "POST"))
def editPage():
t_ID = request.form["taskEditBtn"]
# database_update_task(task_id, name, description, status)
return render_template(
"editTaskPage.html", task=database_get_task(t_ID), title=TITRE
)
@app.route("/update_task", methods=("GET", "POST"))
def update_task():
task_id = request.form["btnSubmit"]
title = request.form.get("NewTaskTitle")
desc = request.form.get("NewTaskContent")
status = request.form.get("selectSection")
database_update_task(task_id, title, desc, status)
return render_template(
"IziPostApp.html", title=TITRE, tasksList=database_fetch_tasks()
)
@app.route("/createNewTaskPage")
def create_new_task_page():
"""Task Creation Routing"""
return render_template("createNewTaskPage.html", title=TITRE)
@app.route("/register", methods=("GET", "POST"))
def register():
"""Register routing"""
print("Register method called")
if request.method == "POST":
username = request.form["username"]
password = request.form["password"]
firstname = request.form["firstname"]
lastname = request.form["lastname"]
database = get_database()
error = None
if not username:
flash("Username is required.")
elif not password:
flash("Password is required.")
if error is None and username and password:
try:
database.execute(
"INSERT INTO users (username, password, firstname, name) VALUES (?, ?, ?, ?)",
(username, hash_mdp(password), firstname, lastname),
)
database.commit()
return render_template("loginForm.html", title=TITRE)
except database.IntegrityError:
error = "Pseudo already used."
flash(error)
return render_template("signupForm.html", title=TITRE)
@app.route("/login", methods=("GET", "POST"))
def login():
"""Login routing"""
if request.method == "POST":
username = request.form["username"]
password = request.form["password"]
database = get_database()
error = None
user = database.execute(
"SELECT * FROM users WHERE username = ?", (username,)
).fetchone()
if user is None:
error = "Incorrect username"
elif not hash_mdp(password) == user["password"]:
error = "Incorrect password"
if error is None:
session.clear()
session["username"] = user["username"]
session["id"] = user["id"]
return redirect(url_for("index"))
flash(error)
return redirect(url_for("login"))
return render_template("loginForm.html", title=TITRE)
@app.route("/logout")
def logout():
"""Logout routing"""
session.clear()
return redirect(url_for("index"))
###################### End Route ##########################