-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtaskHandling.py
More file actions
118 lines (90 loc) · 3.37 KB
/
taskHandling.py
File metadata and controls
118 lines (90 loc) · 3.37 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
from datetime import datetime
import jsonHandling
DESCRIPTION = 'description'
STATUS = 'status'
TODO = 'todo'
IN_PROGRESS = 'in-progress'
Done = 'done'
def add_task(description: str, status=TODO):
tasks = read_tasks_from_json()
last_task: dict[int, str, str, str, str] = tasks[-1]
next_id = 1
if last_task != {}:
next_id = last_task['id'] + 1
if status != TODO and status != IN_PROGRESS and status != Done:
status = TODO
task = {"id": next_id, DESCRIPTION: description, STATUS: status,
'created': datetime.now().strftime("%Y-%m-%d %H:%M"), 'updated': '-'}
tasks.append(task)
jsonHandling.write_json(tasks)
print(f"Task added: {task['id']} {task[DESCRIPTION]} {task['status']}")
def update_task(identification: int, description: str, status: str):
tasks = read_tasks_from_json()
task = get_task_by_id(tasks, identification)
if task is not None:
task[DESCRIPTION] = description
if status is not None:
task[STATUS] = status
task['updated'] = datetime.now().strftime("%Y-%m-%d %H:%M")
jsonHandling.write_json(tasks)
print(f"Task whit id {identification} updated.")
else:
print(f"The task with id {id} don't exist.")
def delete_task(identification: int):
tasks = read_tasks_from_json()
task = get_task_by_id(tasks, identification)
if task is not None:
tasks.remove(task)
jsonHandling.write_json(tasks)
print(f"Task {task['id']} {task[DESCRIPTION]} deleted.")
else:
print(f"The task with id {identification} don't exist.")
def get_task_by_id(tasks: list, identification: int) -> dict | None:
for task in tasks:
if task['id'] == identification:
return task
return None
def mark_in_progress(identification: int):
change_type_of_task(identification, IN_PROGRESS)
def mark_done(identification: int):
change_type_of_task(identification, Done)
def mark_todo(identification: int):
change_type_of_task(identification, TODO)
def change_type_of_task(identification: int, type_task: str):
tasks = read_tasks_from_json()
task = get_task_by_id(tasks, identification)
if task is not None:
task[STATUS] = type_task
jsonHandling.write_json(tasks)
print(f"Task wit id {identification} marked as {type_task}.")
else:
print(f"The task with id {identification} don't exist.")
def list_todo_task():
list_task(TODO)
def list_in_progress_task():
list_task(IN_PROGRESS)
def list_done_task():
list_task(Done)
def list_task(status="all"):
tasks_list = read_tasks_from_json()
count = 0
if len(tasks_list) != 0:
for task in tasks_list:
if status == "all" or task[STATUS] == status:
count += 1
print(
'ID: ' + str(task['id']) + ' DESCRIPTION: ' + task[DESCRIPTION] + ' STATUS: ' + task[
STATUS] + ' CREATED: ' +
task['created'] + ' UPDATED: ' + task['updated'])
if count == 0:
print(f"There are no tasks with type:{status}.")
else:
print("There are no tasks.")
def read_tasks_from_json():
try:
data_json = jsonHandling.read_json()
if data_json is not None:
return data_json
return []
except ValueError:
raise ValueError("Failed to read te json file.")