-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
168 lines (142 loc) · 4.7 KB
/
db.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
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
import sqlite3
import sys
import datetime
DB_NAME = 'todo.db'
conn = sqlite3.connect(DB_NAME, check_same_thread=False)
class DB:
def __init__(self):
self.conn = sqlite3.connect(DB_NAME)
self.cursor = conn.cursor()
def commit(self):
self.cursor.execute('commit')
def reset(self):
self.cursor.execute('''DROP TABLE tasks''')
def seed(self):
self.cursor.execute(
'''
CREATE TABLE IF NOT EXISTS tasks
(
id INTEGER PRIMARY KEY,
description string,
detail string,
locations string,
times string,
people string,
deadline timestamp,
deleted_at timestamp
)
''')
# self.cursor.execute('''INSERT INTO tasks (description, detail) VALUES ('this is description', 'more...')''')
# self.cursor.execute(
# '''INSERT INTO tasks (description, detail) VALUES ('finish proj on wednesday', 'just do it')''')
# self.commit()
def read_all(self, deadline=None):
query = '''SELECT * FROM tasks WHERE deleted_at is NULL ORDER BY deadline DESC'''
if deadline == True:
query = '''SELECT * FROM tasks WHERE deleted_at is NULL AND deadline is not NULL ORDER BY deadline DESC'''
if deadline == False:
query = '''SELECT * FROM tasks WHERE deleted_at is NULL AND deadline is NULL ORDER BY deadline DESC'''
cursor = self.cursor.execute(query)
res = cursor.fetchall()
tasks = [{
"id": row[0],
"description": row[1],
"detail": row[2],
"locations": row[3],
"times": row[4],
"people": row[5],
"deadline": row[6],
} for row in res]
return tasks
def read(self, day):
if day is None:
return day
today = day.replace(hour=0, minute=0, second=0, microsecond=0)
tomorrow = today + datetime.timedelta(days=1)
cursor = self.cursor.execute(
'''SELECT * FROM tasks WHERE deadline is not null AND deadline >= ? and deadline < ?'''
, (today, tomorrow))
res = cursor.fetchall()
tasks = [{
"id": row[0],
"description": row[1],
"detail": row[2],
"locations": row[3],
"times": row[4],
"people": row[5],
"deadline": row[6],
} for row in res]
return tasks
def create(self, action):
description = action['description'] if 'description' in action else None
detail = action['detail'] if 'detail' in action else None
locations = action['locations'] if 'locations' in action else None
times = action['times'] if 'times' in action else None
people = action['people'] if 'people' in action else None
deadline = action['deadline'] if 'deadline' in action else None
self.cursor.execute(
'''INSERT INTO tasks (
description,
detail,
locations,
times,
people,
deadline)
VALUES (?, ?, ?, ?, ?, ?)''',
(
description,
detail,
locations,
times,
people,
deadline,
)
)
self.commit()
def update(self, action):
print('updating >>', action)
index = action['index']
task = action['tasks'][index - 1]
self.cursor.execute(
'''
UPDATE tasks
SET
detail = ?,
locations = ?,
times = ?,
people = ?,
deadline = ?
WHERE id = ?
''', (
task['detail'],
task['locations'],
task['times'],
task['people'],
task['deadline'],
task['id'],
))
# self.cursor.execute('''UPDATE tasks SET description = ? WHERE id = ?''', ("---", task['id']))
self.commit()
def delete(self, task_id):
self.cursor.execute('''
UPDATE tasks
SET deleted_at = ?
where id = ?
''', (
datetime.datetime.now(), str(task_id)
))
self.commit()
db = DB()
# python db.py reset
if 'reset' in sys.argv:
db.reset()
# python db.py seed
if 'seed' in sys.argv:
db.seed()
tasks = db.read_all()
print(tasks)
if 'all' in sys.argv:
tasks = db.read_all()
print(tasks)
if 'temp' in sys.argv:
db.read(datetime.datetime.now() - datetime.timedelta(days=5))