-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.py
More file actions
125 lines (106 loc) · 4.44 KB
/
task.py
File metadata and controls
125 lines (106 loc) · 4.44 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
from datetime import datetime
import json
current_time = datetime.now()
current_time = f"{current_time.hour}:{current_time.minute} {current_time.day}-{current_time.month}-{current_time.year}"
def add_task(_task_desc, tasks_array):
greatest_id = 0
for task in tasks_array:
_task_id = task["id"]
if _task_id > greatest_id:
greatest_id = _task_id
tasks_array.append(
{
"id": greatest_id + 1,
"description": _task_desc,
"status": "todo",
"createdAt": current_time, # Turn this into more a more readable format later
"updatedAt": current_time
}
)
print(f"Task added successfully (ID: { greatest_id + 1 })")
def delete_task(_task_id, tasks_array):
task_exists = False
for task in tasks_array:
if task["id"] == _task_id:
tasks_array.remove(task)
task_exists = True
if not task_exists:
print(f"The task with the ID {_task_id} was not found.")
else:
print(f"Deleted task successfully (ID: {_task_id})")
def list_tasks(_tasks_json, sort=None):
_tasks = _tasks_json["tasks"]
sorted_tasks_exist = False
if not sort:
if len(_tasks) < 1:
print("No tasks!")
else:
for task in _tasks:
print(f"Task id: {task["id"]}")
print(f"Task description: {task["description"]}")
print(f"Task status: {task["status"]}")
print(f"Task creation date: {task["createdAt"]}")
print(f"Task last update date: {task["updatedAt"]}")
print("-------------------------------")
else:
if sort == "done":
for task in _tasks:
if task["status"] == "done":
print(f"Task id: {task["id"]}")
print(f"Task description: {task["description"]}")
print(f"Task status: {task["status"]}")
print(f"Task creation date: {task["createdAt"]}")
print(f"Task last update date: {task["updatedAt"]}")
print("-------------------------------")
sorted_tasks_exist = True
if sort == "todo":
for task in _tasks:
if task["status"] == "todo":
print(f"Task id: {task["id"]}")
print(f"Task description: {task["description"]}")
print(f"Task status: {task["status"]}")
print(f"Task creation date: {task["createdAt"]}")
print(f"Task last update date: {task["updatedAt"]}")
print("-------------------------------")
sorted_tasks_exist = True
if sort == "in-progress":
for task in _tasks:
if task["status"] == "in-progress":
print(f"Task id: {task["id"]}")
print(f"Task description: {task["description"]}")
print(f"Task status: {task["status"]}")
print(f"Task creation date: {task["createdAt"]}")
print(f"Task last update date: {task["updatedAt"]}")
print("-------------------------------")
sorted_tasks_exist = True
if not sorted_tasks_exist:
print("No tasks with the specified sorting were found.")
def mark_task(_task_id, tasks_array, new_status):
task_exists = False
for task in tasks_array:
if task["id"] == _task_id:
task["status"] = new_status
task["updatedAt"] = current_time
task_exists = True
if not task_exists:
print(f"The task with the ID {_task_id} was not found.")
else:
print(f"Marked {new_status} successfully (ID: {_task_id})")
def update_task(_task_id, tasks_array, new_desc):
task_exists = False
for task in tasks_array:
if task["id"] == _task_id:
task["description"] = new_desc
task["updatedAt"] = current_time
task_exists = True
if not task_exists:
print(f"The task with the ID {_task_id} was not found.")
else:
print(f"Updated task successfully (ID: {_task_id})")
def load_tasks(tasks_json_file):
with open("tasks.json", "r") as tasks_file:
_tasks_json = json.load(tasks_file)
return _tasks_json
def save_tasks(tasks_json_file, data):
with open("tasks.json", "w") as tasks_file:
json.dump(data, tasks_file, indent = 4)