-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.py
75 lines (67 loc) · 1.99 KB
/
helper.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
import sqlite3
DB_PATH = './todo.db'
NOTSTARTED = 'Not Started'
INPROGRESS = 'In Progress'
COMPLETED = 'Completed'
def add_to_list(item):
try:
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute('insert into items(item, status) values(?,?)', (item, NOTSTARTED))
conn.commit()
return {"item": item, "status": NOTSTARTED}
except Exception as e:
print('Error: ', e)
return None
todo_list = {}
def get_all_items():
try:
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute('select * from items')
rows = c.fetchall()
return { "count": len(rows), "items": rows }
except Exception as e:
print('Error: ', e)
return None
def get_item(item):
try:
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute("select status from items where item='%s'" % item)
status = c.fetchone()[0]
print(status)
return status
except Exception as e:
print('Error: ', e)
return None
def update_status(item, status):
#Check if the passed status is a valid value
if(status.lower().strip() == 'not started'):
status = NOTSTARTED
elif(status.lower().strip() == 'in progress'):
status = INPROGRESS
elif(status.lower().strip() == 'completed'):
status = COMPLETED
else:
print("Invalid Status - " + status)
return None
try:
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute('update items set status=? where item=?', (status, item))
conn.commit()
return {item: status}
except Exception as e:
print('Error: ', e)
return None
def delete_item(item):
try:
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute('delete from items where item=?', (item,))
conn.commit()
return {'item': item}
except Exception as e:
print('Error: ', e)
return None