-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_class.py
More file actions
44 lines (37 loc) · 1.24 KB
/
Copy pathtask_class.py
File metadata and controls
44 lines (37 loc) · 1.24 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
import os, json, time
class Task():
""" This class is a template for Tasks objects """
FILE_PATH = f"{os.getenv('HOME')}/github/task_tracker/tasks.json"
if not os.path.exists(FILE_PATH):
with open(FILE_PATH, "w") as f:
json.dump([], f)
def __init__(self, task, notes=""):
self.task = task
self.state = "to-do"
self.creation_at = time.ctime()
self.update_at = time.ctime()
self.notes = notes
@staticmethod
def load_tasks():
""" Load tasks """
with open(Task.FILE_PATH,"r") as f:
return json.load(f)
@staticmethod
def save_tasks(tasks):
with open(Task.FILE_PATH, "w") as f:
json.dump(tasks, f)
def create_task(self):
""" Cretate task method """
tasks = self.load_tasks()
new_id = max([task["id"] for task in tasks], default=0) + 1
task = {
"id": new_id,
"title": self.task,
"status": self.state,
"createdAt": self.creation_at,
"updatedAt": self.update_at,
"notes": self.notes
}
tasks.append(task)
self.save_tasks(tasks)
print(f"Task {new_id} created successfully.")