-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_manager.py
More file actions
491 lines (474 loc) · 20.1 KB
/
task_manager.py
File metadata and controls
491 lines (474 loc) · 20.1 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
import os
import json
import sys
from datetime import datetime, timedelta
import platform
script_dir = os.path.dirname(os.path.abspath(__file__))
tasks_file = os.path.join(script_dir, "tasks.json")
def adjust_priority(task):
now = datetime.now()
due_date = datetime.strptime(task["due_date"], "%d-%m-%Y %H:%M")
priority_map = {"Low":1, "Med":2, "High":3,}
reverse_map = {1: "Low", 2:"Med", 3:"High",}
current_priority = priority_map.get(task["priority"], 1)
if now >= due_date - timedelta(days=1):
current_priority = min(current_priority + 1, 3,)
if now >= due_date - timedelta(hours=1):
current_priority = min(current_priority + 1, 3,)
task["priority"] = reverse_map[current_priority]
return task
def get_due_date():
while True:
date_input = input("Enter the due date & time (DD-MM-YYYY HH:MM):\n")
if date_input.lower() == "menu":
clear_screen()
beep_error()
print("Aborting entry, taking you back to main menu...")
main_menu()
try:
due_date = datetime.strptime(date_input, "%d-%m-%Y %H:%M")
now = datetime.now()
if due_date <= now:
beep_error()
clear_screen()
print("Must be a future date & time, try again...\n")
print("You can type 'menu' to abort\n")
continue
return due_date
except ValueError:
beep_error()
clear_screen()
print("Invalid format! Please enter date like DD-MM-YYYY HH:MM")
def get_next_id(tasks):
if not tasks:
return 1
else:
max_id = max(task["id"] for task in tasks)
return max_id + 1
def load_data():
if not os.path.exists(tasks_file):
with open(tasks_file, 'w') as f:
json.dump({"tasks":[]},f, indent=4)
with open(tasks_file,'r') as f:
return json.load(f)
def save_data(data):
with open(tasks_file, 'w') as f:
json.dump(data, f, indent=4)
def clear_screen():
if platform.system() == "Windows":
os.system("cls")
else:
os.system("clear")
def beep_error():
if platform.system() == "Windows":
import winsound
winsound.Beep(1000,150)
else:
print("\a")
def beep_success():
if platform.system() == "Windows":
import winsound
winsound.Beep(400,500)
else:
print("\a\a\a")
def add_task():
clear_screen()
while True:
print("*ADDING A TASK*\n")
task_name = input("What's the title of the task?:\n").capitalize()
while True:
task_priority = input("How important is this task? (Low, Med, High)?:\n").capitalize()
if task_priority in ["Low","Med","High"]:
break
elif task_priority == 'Menu':
clear_screen()
print("Aborting function, taking you back to main menu...")
main_menu()
else:
clear_screen()
beep_error()
print("Invalid entry, enter 'Low','Med', or 'High':\n")
print("You can also type 'menu' to go back to main menu and abort...\n")
task_description = input("Describe the task in more detail or include some notes:\n").capitalize()
task_date = get_due_date()
task_date_str = task_date.strftime("%d-%m-%Y %H:%M")
clear_screen()
print(f"Task Title: {task_name}")
print(f"Task Priority: {task_priority}")
print(f"Description: {task_description}")
print(f"Due Date: {task_date_str}")
print(" ")
while True:
option = input("Would you like to add this task?\n").upper()
clear_screen()
if option == "YES":
data = load_data()
task_list = data["tasks"]
new_task = {
"id": get_next_id(task_list),
"title": task_name,
"priority": task_priority,
"description": task_description,
"due_date": task_date_str,
"completed": False
}
data["tasks"].append(new_task)
save_data(data)
beep_success()
print("This task has now been added, taking you back to menu...")
main_menu()
elif option == "NO":
print("This task has NOT been added.\n")
while True:
option1 = input("Would you like to try adding the task again?\n").upper()
if option1 == "YES":
clear_screen()
print("Great, let's try adding the task again...")
add_task()
elif option1 == "NO":
clear_screen()
print("Let's take you back to the main menu")
main_menu()
else:
beep_error()
clear_screen()
print("Please enter either 'Yes' or 'No'\n")
else:
beep_error()
print("Please enter either 'Yes' or 'No'\n")
def view_all_tasks():
data = load_data()
tasks = data["tasks"]
tasks = [adjust_priority(task) for task in tasks]
priority_map = {"Low": 1, "Med": 2, "High": 3}
tasks.sort(key=lambda t: (-priority_map[t["priority"]], datetime.strptime(t["due_date"], "%d-%m-%Y %H:%M")))
if not tasks:
clear_screen()
beep_error()
print("You don't have any tasks at all, try adding a new task.\n")
main_menu()
else:
for task in tasks:
print("-" * 50)
print(f'ID: {task["id"]} | Title: {task["title"]} | Priority: {task["priority"]} | Due: {task["due_date"]} | Completed: {task["completed"]}')
print("Task Description:\n")
print(f"{task['description']}")
print("-" * 50)
input("Press enter to return to main menu")
clear_screen()
def find_task(tasks, search):
if search.isdigit():
task_id = int(search)
for task in tasks:
if task["id"] == task_id:
return task
else:
for task in tasks:
if task["title"].lower() == search.lower():
return task
return None
def delete_task():
print("*DELETING A TASK*\n")
data = load_data()
tasks = data["tasks"]
if not tasks:
beep_error()
print("No tasks found, please add tasks first.\n")
main_menu()
while True:
search = input("Enter the Task ID or the task title to delete:\n").capitalize()
if search.lower() == "menu":
beep_error()
clear_screen()
print("Taking you back to main menu...")
main_menu()
task = find_task(tasks, search)
if task is None:
beep_error()
clear_screen()
print("No tasks found with those details, try again.\n")
else:
clear_screen()
beep_success()
print("Task found!:")
print(f"Task ID: {task['id']}")
print(f"Task Title: {task['title']}")
print(f"Task Description: {task['description']}")
print(f"Task Due Date: {task['due_date']}")
print(f"Task Complete Status: {task['completed']}\n")
while True:
if task["completed"] == True:
print("Do you wish to continue to delete this task?")
confirm = input("Type 'Yes' or 'No'\n").upper()
if confirm == "YES":
beep_success()
tasks.remove(task)
save_data(data)
clear_screen()
print(f"The task '{task['title']}' has been deleted!\n")
print("Taking you back to the main menu\n")
main_menu()
elif confirm == "NO":
beep_error()
clear_screen()
print(f"The task '{task['title']}' has NOT been deleted\n")
print("Taking you back to main menu...\n")
main_menu()
else:
beep_error()
clear_screen()
print("You are deleting the following task:\n")
print(f"Task ID: {task['id']}")
print(f"Task Title: {task['title']}\n")
print("Please type either 'Yes' or 'No':\n")
else:
print("This task is still incomplete")
confirm = input("Are you sure you would like to delete?\n").upper()
if confirm == "YES":
beep_success()
tasks.remove(task)
save_data(data)
clear_screen()
print(f"The task '{task['title']}' has been deleted!\n")
print("Taking you back to the main menu\n")
main_menu()
elif confirm == "NO":
beep_error()
clear_screen()
print(f"The task '{task['title']}' has NOT been deleted\n")
print("Taking you back to main menu...\n")
main_menu()
else:
beep_error()
print("Please enter either 'Yes' or 'No'")
def task_complete():
clear_screen()
print("*MARKING TASK AS COMPLETE*\n")
data = load_data()
tasks = data["tasks"]
if not tasks:
clear_screen()
print("No tasks available to mark as complete :(.\n")
main_menu()
search = input("Enter the task ID or title to mark as complete: \n")
task = find_task(tasks, search)
if task is None:
beep_error()
clear_screen()
print("No tasks found with those details, try again.")
main_menu()
if task in tasks and task['completed'] == True:
clear_screen()
beep_error()
print(f"Task '{task['title']}' is already completed :).")
main_menu()
if task in tasks and task["completed"] == False:
task["completed"] = True
save_data(data)
beep_success()
clear_screen()
print(f"Task '{task['title']}' has been updated to complete! :)")
main_menu()
def modify_task():
clear_screen()
print("*MODIFYING A TASK*")
print("Hint: you can type 'menu' to abort")
data = load_data()
tasks = data["tasks"]
if not tasks:
beep_error()
clear_screen()
print("You don't have any tasks right now, try adding a task first.")
main_menu()
while True:
search = input("Enter task name or ID: \n").capitalize()
if search == "Menu":
clear_screen()
print("Taking you back to the main menu...\n")
main_menu()
task = find_task(tasks, search)
if task is None:
beep_error()
clear_screen()
print("There is no task with those details, try again or enter 'menu' to go back.")
elif task in tasks:
clear_screen()
print(f"*MODIFYING TASK DETAILS*")
print("Found it!\n")
print(f"Task ID: {task['id']}")
print(f"Task Title: {task['title']}\n")
print("1. Change Task Title.")
print("2. Change Task Due Date.")
print("3. Change Task Description.")
print("4. Change Task Priority.")
print("5. Change task to edit.")
print("6. Main Menu\n")
option = input("Select from the option above:\n")
while True:
if option == '1':
clear_screen()
print("*CHANGING TASK TITLE*\n")
print(f"Changing the title of '{task['title']}'\n")
new_title = input(f"What would you like the new title to be?\n").capitalize()
print(" ")
confirm = input(f"Just to confirm, you would like to change: '{task['title']}' to '{new_title}'?\n").upper()
if confirm == 'YES':
task["title"] = new_title
save_data(data)
beep_success()
clear_screen()
print("Task updated successfully.")
main_menu()
if confirm == 'NO':
clear_screen()
print("You have chose to abort change, back to main menu.")
main_menu()
elif option == '2':
clear_screen()
print("*CHANGING DUE DATE*\n")
print("Tasks' Current Details:\n")
print(f"Task ID: {task['id']}")
print(f"Task Title: {task['title']}")
print(f"Task Due Date: {task['due_date']}")
print(" ")
print("Provide the new date below:")
task_date = get_due_date()
new_date = task_date.strftime("%d-%m-%Y %H:%M")
clear_screen()
print("Just to confirm, you would like to change the dates")
print(f"FROM: '{task['due_date']}' TO: '{new_date}'\n")
print(f"Of task: {task['title']} with ID: {task['id']}?\n")
confirm = input("Enter 'Yes' or 'No'\n").upper()
if confirm == 'YES':
clear_screen()
task["due_date"] = new_date
save_data(data)
beep_success()
print("Task updated successfully.")
main_menu()
elif confirm == 'NO':
clear_screen()
print("You have chose to abort, let's take you back to main menu")
main_menu()
else:
print("Invalid entry, type 'Yes' or 'No'")
elif option == '3':
clear_screen()
print("*CHANGING DESCRIPTION*\n")
print("Tasks details:")
print(f"Task ID: {task['id']}")
print(f"Task Title: {task['title']}")
print(f"Current Task Description:\n")
print(f"{task['description']}")
print("-" * 50)
new_description = input(f"Type new task description:\n").capitalize()
clear_screen()
print("The old task description:\n")
print(f"'{task['description']}'")
print("-" * 50)
print("The new task description:\n")
print(f"'{new_description}'\n")
while True:
confirm = input(f"Confirm change ('Yes' or 'No')?\n").upper()
if confirm == 'YES':
clear_screen()
task["description"] = new_description
save_data(data)
beep_success()
print("Task updated successfully.")
main_menu()
elif confirm == 'NO':
clear_screen()
beep_error()
print("You have chose to cancel this, let's take you back to main menu")
main_menu()
else:
beep_error()
clear_screen()
print("New task description to be:\n")
print(f"'{new_description}'\n")
print(f"For task:")
print(f"ID: {task['id']}")
print(f"TITLE: {task['title']}\n")
print("Invalid entry, please type either 'Yes' or 'No'")
elif option == '4':
clear_screen()
print("*CHANGING PRIORITY LEVEL*\n")
print("Task selected:\n")
print(f"Task ID: {task['id']}")
print(f"Task Title: {task['title']}\n")
print(f"Current Priority Level: {task['priority']}\n")
while True:
print("What's the new priority level?")
new_priority = input(f"(Low, Med, or High)\n").capitalize()
if new_priority in ["Low", "Med", "High"]:
clear_screen()
print("You would like to update the priority of the following task:")
print(f"Task ID: {task['id']}")
print(f"Task Title: {task['title']}\n")
print(f"FROM: {task['priority']}")
print(f"TO: {new_priority}?\n")
while True:
confirm = input(f"Please type either 'Yes' or 'No'\n").upper()
if confirm == 'YES':
task["priority"] = new_priority
save_data(data)
beep_success()
clear_screen()
print("Task updated successfully.")
main_menu()
if confirm == 'NO':
beep_error()
clear_screen()
print("You have chose to abort change, back to main menu...")
main_menu()
else:
beep_error()
clear_screen()
print("Invalid entry!\n")
else:
beep_error()
clear_screen()
print("Invalid entry, please try again")
elif option == '5':
clear_screen()
modify_task()
elif option == '6':
clear_screen()
main_menu()
else:
beep_error()
print("Invalid entry!")
modify_task()
else:
print("Invalid entry!")
main_menu()
def main_menu():
while True:
print(" ")
print("1. Add New Task")
print("2. Modify a Task")
print("3. Mark Task as Complete")
print("4. Delete a Task")
print("5. View All Tasks")
print("6. Close Application.")
print(" ")
option = input("What would you like to do? :\n").lower()
if option == '1':
add_task()
elif option == '2':
modify_task()
elif option == '3':
task_complete()
elif option == '4':
clear_screen()
delete_task()
elif option == '5':
view_all_tasks()
elif option in ["exit", '6']:
exit()
else:
clear_screen()
beep_error()
print("!!! Please only enter from the list below: !!!")
main_menu()