-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo
executable file
·216 lines (182 loc) · 5.1 KB
/
todo
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/python3
from datetime import date, timedelta
import calendar
import pymysql
import sys
from collections import OrderedDict
import pyfiglet
from termcolor import colored
import os
import click
class Tasks:
def __init__(self, rows):
self.rows = rows
self.tasks = self.initialize_items()
def initialize_items(self):
tasks = OrderedDict()
n = len(self.rows)
for i, row in enumerate(self.rows):
t = TaskItem(row)
tasks[t.id] = t
return tasks
def display(self):
# keep track of previous day for output formatting
prev_day = 10
for id, task in self.tasks.items():
if prev_day != task.days:
if prev_day != 10:
click.echo()
prev_day = task.days
click.echo(task.show())
class TaskItem:
def __init__(self, data):
self.id = data[0]
self.task = data[1]
self.finish_by = data[2]
self.day_of_week = calendar.day_name[self.finish_by.weekday()]
self.days = (self.finish_by - date.today()).days
self.completed = data[3]
self.output = self.show()
def show(self):
output = "{:>2} | {:<10} | {}".format(
self.id,
self.day_of_week,
self.task
)
color = 'green' if self.completed else 'red'
return colored(output, color)
def get_cxn():
# Connect to the database
connection = pymysql.connect(host='localhost',
user='root',
password='wplqsym',
db='todo')
return connection
def welcome():
ascii_banner = pyfiglet.figlet_format("ToDo List")
click.echo(ascii_banner)
def clear():
os.system('clear')
def execute_query(query):
cxn = get_cxn()
cursor = cxn.cursor()
cursor.execute(query)
cxn.commit()
def show(query):
clear()
welcome()
cxn = get_cxn()
cursor = cxn.cursor()
cursor.execute(query)
rows = cursor.fetchall()
# initialize tasks list
tasks = Tasks(rows)
tasks.display()
@click.group(invoke_without_command=True, help='displays list of incomplete tasks')
@click.pass_context
def todo(ctx):
query = """
select id, task, finish_by, completed
from tasks
where completed = false
order by finish_by;
"""
if ctx.invoked_subcommand is None:
show(query)
else:
pass
@todo.command(help='USAGE: todo all [n]')
@click.argument('n', required=True)
def all(n):
query = """
with done as (
select id, task, finish_by, completed
from tasks
where completed = true
order by finish_by desc
limit {}
),
todo as (
select id, task, finish_by, completed
from tasks
where completed = false
)
select * from todo
union
select * from done
order by completed desc, finish_by;
""".format(n)
show(query)
@todo.command(help = 'USAGE: todo add "[task]" [days]')
@click.argument('task', required=True)
@click.argument('days', required=True)
def add(task, days):
query = """
insert into
tasks (task, finish_by, completed)
values ('{}', date_add(current_date, interval {} day), 0);
""".format(
task,
days
)
execute_query(query)
@todo.command(help = 'USAGE: todo delete [id]')
@click.argument('id', required=True)
def delete(id):
query = """
delete from tasks
where id = {};
""".format(
id
)
execute_query(query)
@todo.command(help = 'USAGE: todo edit [id] "[task]"')
@click.argument('id', required=True)
@click.argument('task', required=True)
def edit(id, task):
query = """
update tasks
set task = '{}'
where id = {};
""".format(
task,
id
)
execute_query(query)
@todo.command(help = 'USAGE: todo days [id] [days]')
@click.argument('id', required=True)
@click.argument('days', required=True)
def days(id, days):
query = """
update tasks
set finish_by = date_add(current_date, interval {} day)
where id = {};
""".format(
days,
id
)
execute_query(query)
@todo.command(help = 'USAGE: todo complete [id]')
@click.argument('id', required=True)
def complete(id):
query = """
update tasks
set completed = true
where id = {};
""".format(
id
)
execute_query(query)
@todo.command(help = 'USAGE: todo incomplete [id]')
@click.argument('id', required=True)
def incomplete(id):
query = """
update tasks
set completed = false
where id = {};
""".format(
id
)
execute_query(query)
if __name__ == '__main__':
todo()