-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanage.py
More file actions
260 lines (215 loc) · 8.02 KB
/
manage.py
File metadata and controls
260 lines (215 loc) · 8.02 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import json
from beautifultable import BeautifulTable
from termcolor import colored
import colorama
import datetime
# MONGO DB INFO:
# USERNAME: Mitra
# PASSWORD: MZZeVXCwY9SBf9F
class Manage:
functionMapping = {
"add": "addTask",
"help": "commandList",
"change": "changeStatus",
"show": "displayCourse",
"showall": "displayAll",
"new": "newCourse",
"list": "listCourses",
"remove": "removeCourse",
"display": "displayTasksForDay",
"displayToday": "displayTasksDueToday"
}
def writeToFile(data: dict):
with open('tasks.json', 'w') as json_file:
json.dump(data, json_file)
def readFile():
with open("tasks.json") as f:
data = json.load(f)
return data
def findTask(course: str, description: str, data: dict):
for task in data[course]:
if task["description"] == description:
return data[course].index(task)
def getDueDate():
isValidDate = False
while(not isValidDate):
isValidDate = True
dueDate = input("Due Date (DD/MM/YY): ")
dueTime = input("Due Time (HH:MM): ")
dueDate = dueDate + " " + dueTime
try:
datetime.datetime.strptime(dueDate, "%d/%m/%y %H:%M")
except ValueError:
print("Please enter a valid date and time.")
isValidDate = False
return dueDate
def addTask(self):
course = Manage.getCourse()
description = input("Task Description: ")
dueDate = Manage.getDueDate()
status = input("Status: ")
data = Manage.readFile()
data[course].append({"description": description,
"dueDate": dueDate, "status": status})
Manage.writeToFile(data)
def getCourse():
data = Manage.readFile()
isValidCourse = False
while not isValidCourse:
isValidCourse = True
course = input("Course: ")
if course in data.keys():
return course
else:
addCourse = Manage.checkValidCourse(course)
if addCourse:
Manage.addNewCourse(course)
return course
else:
print("Please enter a valid course.")
isValidCourse = False
def removeCourse(self):
data = Manage.readFile()
course = input("Course: ")
if course not in data.keys():
print("Course {} does not exist".format(course))
return
data.pop(course)
Manage.writeToFile(data)
def displayTasksForDay(self):
data = Manage.readFile()
table = Manage.createTable()
for course in data.keys():
for task in data[course]:
if datetime.datetime.now() < datetime.datetime.strptime(task["dueDate"], "%d/%m/%y %H:%M"):
table = Manage.addToTable(table, task, course)
print(table)
print('\n')
return table
def displayTasksDueToday(self):
data = Manage.readFile()
table = Manage.createTable()
for course in data.keys():
for task in data[course]:
if datetime.datetime.now().date() == datetime.datetime.strptime(task["dueDate"], "%d/%m/%y %H:%M").date():
table = Manage.addToTable(table, task, course)
print(table)
print('\n')
return table
def checkValidCourse(course):
print("The course {} does not exist.".format(course))
answered = False
while not answered:
answer = input(
"Would you like to add {} as a new course? (Y/N):".format(course))
if(answer == 'Y' or answer == 'y'):
Manage.addNewCourse(course)
return True
elif(answer == 'N' or answer == 'n'):
return False
def addNewCourse(course):
data = Manage.readFile()
newCourseDict = {
course: []
}
data.update(newCourseDict)
Manage.writeToFile(data)
def newCourse(self):
course = input("Course: ")
data = Manage.readFile()
if course in data.keys():
print("The course {} already exists.".format(course))
else:
Manage.addNewCourse(course)
def changeStatus(self):
course = input("Course: ")
description = input("Description: ")
status = input("Status: ")
data = Manage.readFile()
taskIndex = Manage.findTask(course, description, data)
data[course][taskIndex]["status"] = status
Manage.writeToFile(data)
def createTable():
table = BeautifulTable()
table.columns.header = ["Course", "Description",
"Due Date", "Due Time", "Status"]
return table
def listCourses(self):
data = Manage.readFile()
table = BeautifulTable()
for course in data.keys():
table.rows.append([course])
print(table)
def addToTable(table, task, course):
date = datetime.datetime.strptime(task["dueDate"], "%d/%m/%y %H:%M")
table.rows.append([course, task['description'], date.strftime("%d/%m/%y"), date.strftime("%H:%M"), task["status"]])
# if(task['status'] == 'COMPLETE'):
# table.rows.append([colored(course, 'green'), colored(task["description"], 'green'),
# colored(date.strftime("%d/%m/%y"), 'green'), colored(date.strftime("%H:%M"), 'green'), colored(task["status"], 'green')])
# elif(task['status'] == "INCOMPLETE"):
# table.rows.append([colored(course, 'red'), colored(task["description"], 'red'),
# colored(date.strftime("%d/%m/%y"), 'red'), colored(date.strftime("%H:%M"), 'red'), colored(task["status"], 'red')])
return table
def displayCourse(self):
course = input("Course: ")
data = Manage.readFile()
if course not in data.keys():
print("COURSE NOT FOUND!")
return
table = Manage.createTable()
for task in data[course]:
table = Manage.addToTable(table, task, course)
print(table)
print('\n')
def displayAll(self):
data = Manage.readFile()
table = Manage.createTable()
for course in data.keys():
for task in data[course]:
table = Manage.addToTable(table, task, course)
print(table)
print('\n')
def commandList(self):
print('='*40)
print("add: add a task")
print("change: change the status of a task")
print("show: displays the table for a course")
print("showall: displays the table for all the courses")
print("new: add a new course to the course list")
print("list: lists all courses")
print("exit: terminates the program")
print("remove: deletes the specified course")
print("display: displays all the tasks due in the future")
print("displayToday: displays all the tasks due today")
print('='*40)
def modifyExistingTask(course):
data = Manage.readFile()
print("MODIFY")
def exit():
return
def check():
command = input(">")
manageClass = Manage()
if command == "exit":
return True
if command not in Manage.functionMapping.keys():
print("Please enter a valid command.")
return False
try:
getattr(manageClass, Manage.functionMapping[command])()
except AttributeError:
raise NotImplementedError("Command does not exist")
return False
def start():
colorama.init()
closeProgram = False
m = Manage()
table = BeautifulTable()
print("="*21)
print("||" + colored("Welcome to Manage", 'red', 'on_yellow') + "||")
print("="*21)
while not closeProgram:
print('-'*40)
print("Type {} to view all commands".format(
colored("'help'", 'yellow')))
closeProgram = check()