/
todo.py
62 lines (42 loc) · 1.71 KB
/
todo.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
from __future__ import unicode_literals
import sqlite3
import tempfile
from pathlib import Path
# This creates the database in a temporary directory
db_path = Path(tempfile.gettempdir()) / "bitecode_htmx_todo.db"
conn = sqlite3.connect(str(db_path))
conn.row_factory = sqlite3.Row
c = conn.cursor()
c.execute(
"CREATE TABLE IF NOT EXISTS todos (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, done BOOL NOT NULL)"
)
conn.commit()
# From there, each function is an operation on the list of things to do
# The texts between """ tell you what each function does.
def add_todo(title):
"""Add a task to the things to do in the database and return it"""
c.execute("INSERT INTO todos (title, done) VALUES (?, 0)", (title,))
conn.commit()
return {"id": c.lastrowid, "title": title, "done": False}
def set_task_status(task_id, done):
"""Set a task as done, or to be done, and return it partially"""
c.execute("UPDATE todos SET done=? WHERE id=?", (done, task_id))
conn.commit()
return {"id": task_id, "done": done}
def delete_todo(task_id):
"""Delete a task completely"""
c.execute("DELETE FROM todos WHERE id=?", (task_id,))
conn.commit()
def list_todos():
"""Return all tasks"""
return c.execute("SELECT * FROM todos ORDER BY id").fetchall()
def get_task(task_id):
"""Return one single task"""
return c.execute("SELECT * FROM todos WHERE id=?", (task_id,)).fetchone()
def count_todos():
"""Return a count of all tasks and a count of the ones remaining to be done"""
c.execute("SELECT COUNT(*) FROM todos")
total = c.fetchone()[0]
c.execute("SELECT COUNT(*) FROM todos WHERE done")
done = c.fetchone()[0]
return {"done": done, "total": total}