-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
54 lines (43 loc) · 1.47 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
import datetime
from cs50 import SQL
from flask import Flask, redirect, render_template, request, url_for
# Configure application
app = Flask(__name__)
# Ensure templates are auto-reloaded
app.config["TEMPLATES_AUTO_RELOAD"] = True
# Configure CS50 Library to use SQLite database
db = SQL("sqlite:///todo.db")
#homepage that shows all todos
@app.route('/')
def index():
tasks = []
rows = db.execute("SELECT * FROM tasks")
if rows:
for row in rows:
tasks.append(row['name_of_task'])
return render_template('index.html', tasks=tasks)
else:
return render_template('index.html')
@app.route('/', methods=["POST"])
#function to add a todo to db
def add():
if request.method == "POST":
task = request.form.get("task")
if task:
#insert tasks into todo table
date = datetime.datetime.now()
params = (task, date.strftime('%Y-%m-%d %H:%M:%S'))
rows = db.execute("INSERT INTO 'tasks' ('name_of_task', 'date') VALUES (?, ?)", params)
return redirect("/")
else:
return render_template("index.html")
@app.route('/delete/<string:name_of_task>')
#function to delete a todo from db
def delete(name_of_task):
if not name_of_task:
return redirect('/')
else:
rows = db.execute("DELETE FROM tasks WHERE name_of_task = :name_of_task", name_of_task=name_of_task)
return redirect('/')
if __name__ == '__main__':
app.run(debug=True)