-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
82 lines (62 loc) · 2.3 KB
/
app.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
from flask import Flask, render_template, request, redirect
import sqlite3
import os
from dotenv import load_dotenv
load_dotenv()
current_dir = os.path.dirname(os.path.abspath(__file__))
app = Flask(__name__, static_url_path="/static")
app.secret_key = os.environ.get("SECRET_KEY")
@app.route("/", methods=["GET", "POST"])
def index():
connection = sqlite3.connect(current_dir + "/todo_app.db")
cursor = connection.cursor()
initial_query = "CREATE TABLE IF NOT EXISTS tasks (id INTEGER PRIMARY KEY AUTOINCREMENT, task TEXT, checked BOOLEAN DEFAULT false)"
cursor.execute(initial_query)
connection.commit()
if request.method == 'POST':
task = request.form['task']
query = "INSERT INTO tasks (task) VALUES ('{task}')".format(task=task)
cursor.execute(query)
connection.commit()
try:
query = "SELECT * FROM tasks ORDER BY id DESC"
cursor.execute(query)
tasks = cursor.fetchall()
connection.commit()
except:
tasks = list()
return render_template("index.html", tasks=tasks)
@app.route("/delete_task/<int:id>", methods=["POST"])
def delete(id):
connection = sqlite3.connect(current_dir + "/todo_app.db")
cursor = connection.cursor()
query = "DELETE FROM tasks WHERE id = {id}".format(id=id)
cursor.execute(query)
connection.commit()
return redirect("/")
@app.route("/delete_checked", methods=["POST"])
def delete_checked():
connection = sqlite3.connect(current_dir + "/todo_app.db")
cursor = connection.cursor()
query = "DELETE FROM tasks WHERE checked = 1"
cursor.execute(query)
connection.commit()
return redirect("/")
@app.route("/delete_all", methods=["POST"])
def delete_all():
connection = sqlite3.connect(current_dir + "/todo_app.db")
cursor = connection.cursor()
query = "DELETE FROM tasks"
cursor.execute(query)
connection.commit()
return redirect("/")
@app.route("/toggle_check/<int:id>", methods=["POST"])
def toggle_check(id):
connection = sqlite3.connect(current_dir + "/todo_app.db")
cursor = connection.cursor()
query = "UPDATE tasks SET checked = NOT checked WHERE id = {id}".format(id=id)
cursor.execute(query)
connection.commit()
return redirect("/")
if __name__ == "__main__":
app.run(debug=True)