-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_manager.py
More file actions
71 lines (56 loc) · 1.72 KB
/
Copy pathtask_manager.py
File metadata and controls
71 lines (56 loc) · 1.72 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
import storage
from datetime import datetime
def generate_id(tasks):
if not tasks:
return 1
return max(task['id'] for task in tasks) + 1
def add_task(title, description="", due_date=None):
tasks = storage.load_tasks()
new_task = {
'id': generate_id(tasks),
'title': title,
'description': description,
'status': 'Pending',
'created_at': datetime.now().isoformat()
}
if due_date:
new_task['due_date'] = due_date
tasks.append(new_task)
storage.save_tasks(tasks)
return new_task
def list_tasks(sort_by_due_date=False):
all_tasks = storage.load_tasks()
if sort_by_due_date:
with_dates = []
without_dates = []
for t in all_tasks:
if 'due_date' in t:
with_dates.append(t)
else:
without_dates.append(t)
with_dates.sort(key=lambda x: x['due_date'])
all_tasks = with_dates + without_dates
return all_tasks
def get_task(task_id):
tasks = storage.load_tasks()
for task in tasks:
if task['id'] == task_id:
return task
return None
def mark_complete(task_id):
tasks = storage.load_tasks()
for task in tasks:
if task['id'] == task_id:
task['status'] = 'Completed'
task['completed_at'] = datetime.now().isoformat()
storage.save_tasks(tasks)
return True
return False
def delete_task(task_id):
tasks = storage.load_tasks()
initial_len = len(tasks)
tasks = [task for task in tasks if task['id'] != task_id]
if len(tasks) < initial_len:
storage.save_tasks(tasks)
return True
return False