-
Notifications
You must be signed in to change notification settings - Fork 0
/
todolist.py
137 lines (109 loc) · 3.67 KB
/
todolist.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
import db # db.py contains the codes dealing with database,schema, and datetime
from sqlalchemy.orm import sessionmaker
Session = sessionmaker(bind=db.engine)
session = Session()
def todays_tasks():
# Shows tasks with today's deadline
today = db.datetime.today()
print("Today", today.day, today.strftime('%b'), ':')
tasks = session.query(db.Task).filter(db.Task.deadline == today.date()).all()
if len(tasks) == 0:
print("Nothing to do")
else:
i = 1
for task in tasks:
print(str(i) + '.', task)
i += 1
def weeks_tasks():
''' Shows tasks with deadlines in the coming week.
Coming week means the next 7 days, excluding the current day
'''
today = db.datetime.today()
for i in range(7):
this_day = today + db.timedelta(days=i)
print()
print(this_day.strftime('%A'), this_day.day, this_day.strftime('%b'))
tasks = session.query(db.Task).filter(db.Task.deadline == this_day.date()).all()
if len(tasks) == 0:
print("Nothing to do!")
else:
i = 1
for task in tasks:
print(str(i) + '.', task)
i += 1
def all_tasks():
# Shows all the tasks present in the database
print('All tasks:')
tasks = session.query(db.Task).order_by(db.Task.deadline).all()
if len(tasks) == 0:
print('No task to do')
else:
i = 1
for task in tasks:
print(str(i) + '. ', task, '. ', task.dead_line().day, ' ', task.dead_line().strftime('%b'), sep='')
i += 1
def add_task():
'''Adds a new task with a task name, and the corresponding deadline.
Deadline should be in YYYY-MM-DD format
'''
new_task = input('Enter task\n')
deadline = input('Enter deadline\n')
deadline = [int(x) for x in deadline.split('-')]
deadline = db.datetime(deadline[0], deadline[1], deadline[2])
new_task = db.Task(task=new_task, deadline=deadline)
session.add(new_task)
session.commit()
def missed_tasks():
# Shows the tasks whose deadlines lie before the current (today's) date
today = db.datetime.today().date()
tasks = session.query(db.Task).filter(db.Task.deadline < today).order_by(db.Task.deadline).all()
if len(tasks) == 0:
print('No missed tasks!')
else:
i = 1
for task in tasks:
print(str(i) + '. ', task, '. ', task.dead_line().day, ' ', task.dead_line().strftime('%b'), sep='')
i += 1
print()
def delete_task():
'''Deletes a specified task by its index.
First, all the tasks present in the database are printed with index from [1, 2, ..., n]
Then, the indexed entry is removed from the database permanently
'''
tasks = session.query(db.Task).order_by(db.Task.deadline).all()
if len(tasks) == 0:
print('Nothing to delete!')
else:
i = 1
print('Choose the number of the task you want to delete:')
for task in tasks:
print(str(i) + '. ', task, '. ', task.dead_line().day, ' ', task.dead_line().strftime('%b'), sep='')
i += 1
task_number = int(input())
session.delete(tasks[task_number-1])
session.commit()
while True:
# MENU
print('''1) Today's tasks
2) Week's tasks
3) All tasks
4) Missed tasks
5) Add task
6) Delete task
0) Exit''')
option = int(input())
if option == 0:
print('Bye!')
break
if option == 1:
todays_tasks()
if option == 2:
weeks_tasks()
if option == 3:
all_tasks()
if option == 4:
missed_tasks()
if option == 5:
add_task()
if option == 6:
delete_task()