Repository navigation
Expand file tree
/
Copy pathtasks.py
More file actions
116 lines (82 loc) · 2.78 KB
/
Copy pathtasks.py
File metadata and controls
116 lines (82 loc) · 2.78 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
import json
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
TASKS_FILE = os.path.join(BASE_DIR, "task.json")
tasks = []
def save_tasks():
os.makedirs(os.path.dirname(TASKS_FILE), exist_ok=True)
with open(TASKS_FILE, "w", encoding="utf-8") as file:
json.dump(tasks, file, indent=2)
file.write("\n")
def load_tasks():
global tasks
try:
with open(TASKS_FILE, "r", encoding="utf-8") as file:
loaded_tasks = json.load(file)
if isinstance(loaded_tasks, list):
tasks[:] = loaded_tasks
else:
tasks.clear()
save_tasks()
except FileNotFoundError:
tasks.clear()
save_tasks()
except json.JSONDecodeError:
tasks.clear()
save_tasks()
return tasks
def add_task(user_task):
user_task = user_task.strip()
if not user_task:
return False, "Task description cannot be empty."
if any(task["task"].strip().lower() == user_task.lower() for task in tasks):
return False, "This task is already added."
task_id = 1 if not tasks else tasks[-1]["id"] + 1
new_task = {"id": task_id, "task": user_task, "status": "pending"}
tasks.append(new_task)
save_tasks()
return True, f"Task '{user_task}' added successfully."
STATUS_FLOW = ["pending", "in progress", "completed"]
def update_status(task_id):
for task in tasks:
if task["id"] == task_id:
current = task.get("status", "pending").strip().lower()
if current not in {status.lower() for status in STATUS_FLOW}:
current = "pending"
current_index = next(
(
index
for index, status in enumerate(STATUS_FLOW)
if status.lower() == current
),
0,
)
next_index = (current_index + 1) % len(STATUS_FLOW)
task["status"] = STATUS_FLOW[next_index]
save_tasks()
return True, task["status"]
return False, None
def delete_tasks(task_id):
for task in tasks:
if task["id"] == task_id:
tasks.remove(task)
break
save_tasks()
def delete_completed_task():
pending_task = []
for task in tasks:
if task.get("status", "pending") != "completed":
pending_task.append(task)
tasks[:] = pending_task
save_tasks()
return True, "Completed tasks deleted successfully."
def delete_all_tasks():
tasks.clear()
save_tasks()
def get_active_tasks_count(tasks_database=None):
if tasks_database is None:
tasks_database = tasks
active_statuses = {"pending", "in progress"}
return sum(
1 for task in tasks_database if task.get("status", "pending") in active_statuses
)