-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
39 lines (31 loc) · 1.03 KB
/
model.py
File metadata and controls
39 lines (31 loc) · 1.03 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
import os
import json
DATA_FILE = "tasks.json"
class TaskModel:
def __init__(self):
self.tasks = []
self.load()
def add_task(self, text, due_date=None, priority="Medium", category="Personal"):
self.tasks.append({
"text": text,
"done": False,
"due": due_date,
"priority": priority,
"category": category
})
self.save()
def toggle_task(self, index):
self.tasks[index]["done"] = not self.tasks[index]["done"]
self.save()
def delete_task(self, index):
del self.tasks[index]
self.save()
def sort_by_due(self):
self.tasks.sort(key = lambda task: task.get("due") or "")
def save(self):
with open(DATA_FILE, "w") as f:
json.dump(self.tasks, f)
def load(self):
if os.path.exists(DATA_FILE):
with open(DATA_FILE, "r") as f:
self.tasks = json.load(f)