-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.py
More file actions
249 lines (199 loc) · 8.57 KB
/
controller.py
File metadata and controls
249 lines (199 loc) · 8.57 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
from auth_system import AuthSystem
from task_manager import TaskManager
from project_manager import ProjectManager
class TaskMaster:
def __init__(self):
self.auth = AuthSystem()
self.projects = ProjectManager()
self.tasks = TaskManager()
self.current_user = None
self.current_project_id = None
def run(self):
while True:
self._show_main_menu()
def _collect_non_blank_input(self, value, prompt):
while True:
choice = input(f"{prompt}").strip()
if choice: return choice
print(f"\n{value} cannot be blank")
def _collect_id(self, target_id, action):
return self._collect_non_blank_input(f"{target_id.title()} ID", f"\nEnter {target_id} ID to {action}: ")
def _collect_username_and_password(self):
username = self._collect_non_blank_input("Username", "\nUsername: ")
password = self._collect_non_blank_input("Password", "Password: ")
return username, password
def _show_main_menu(self):
print("\n=== TaskMaster ===")
print("1. Login")
print("2. Register")
print("0. Exit")
choice = self._collect_non_blank_input("Choice", "> ")
if choice == "1":
self._login()
elif choice == "2":
self._register()
elif choice == "0":
print("\nGoodbye!")
exit()
else:
print("\nInvalid selection. Please make a selection between 0 and 2")
def _login(self):
username, password = self._collect_username_and_password()
result = self.auth.authenticate(username, password)
if result[0]:
self.current_user = username
print(f"\nWelcome {username}!")
self._project_menu()
else:
print(f"\nLogin failed. {result[1]}")
def _register(self):
username, password = self._collect_username_and_password()
result = self.auth.add_user(username, password)
if result[0]:
print("\nRegistration Successful. Please log in.")
else:
print(f"\nRegistration failed. {result[1]}")
def _project_menu(self):
while True:
print(f"\n--- Project Menu [{self.current_user}] ---")
print("1. Create Project")
print("2. Select Project")
print("3. Delete Project")
print("4. List Projects")
print("0. Logout")
choice = self._collect_non_blank_input("Choice", "> ")
if choice == "1":
self._create_project()
elif choice == "2":
self._select_project()
elif choice == "3":
self._delete_project()
elif choice == "4":
self._list_projects()
elif choice == "0":
print(f"\nLogging out {self.current_user}...")
self.current_user = None
break
else:
print("\nInvalid selection. Please make a selection between 0 and 4")
def _create_project(self):
name = self._collect_non_blank_input("Project name", "Project Name: ")
desc = self._collect_non_blank_input("Description", "Description (optional): ")
pid = self.projects.add_project(self.current_user, name, desc)
if pid[0]:
print(f"\nProject created with project ID: {pid[0]}")
else:
print(f"Failed to create project. {pid[1]}")
def _select_project(self):
self._list_projects()
pid = self._collect_id("project", "select")
projects = self.projects.list_projects(self.current_user)
if pid in projects:
self.current_project_id = pid
print(f"\nSelected project: {projects[pid]['name']}")
self._task_menu()
else:
print("\nInvalid project ID")
def _delete_project(self):
self._list_projects()
pid = self._collect_id("project", "delete")
if self._collect_non_blank_input("Confirmation",
"Are you sure you would like to delete this project (y/n): ").lower() == "y":
result = self.projects.delete_project(self.current_user, pid)
if result[0]:
print("\nProject deleted.")
else:
print(f"\nFailed to delete project. {result[1]}")
else:
print("Cancelled")
def _list_projects(self):
projects = self.projects.list_projects(self.current_user)
if not projects:
print("\nNo projects found")
return
print(f"\nProjects for {self.current_user}:")
for pid, meta in projects.items():
print(f"[{pid}] {meta['name']} - {meta.get('description', '')}")
def _task_menu(self):
while True:
print(f"\n --- Task Menu [{self.current_user} / {self.current_project_id}] ---")
print("1. Add Task")
print("2. View Tasks")
print("3. Edit Tasks")
print("4. Delete Task")
print("5. Mark Complete/Incomplete")
print("0. Back to Project Menu")
choice = self._collect_non_blank_input("Choice", "> ")
if choice == "1":
self._add_task()
elif choice == "2":
self._list_tasks()
elif choice == "3":
self._edit_task()
elif choice == "4":
self._delete_task()
elif choice == "5":
self._toggle_task_status()
elif choice == "0":
break
else:
print("\nInvalid selection. Please make a selection between 0 and 5")
def _add_task(self):
name = self._collect_non_blank_input("Task name", "Task name: ")
desc = input("Task description (optional): ").strip()
due = input("Due date (optional, YYYY-MM-DD): ").strip()
due = due if due else None
tid = self.tasks.add_task(self.current_user, self.current_project_id, name, desc, due)
if tid[0]:
print(f"\nTask added with ID: {tid[0]}")
else:
print(f"\nFailed to add task. {tid[1]}")
def _list_tasks(self):
task_list = self.tasks.list_tasks(self.current_user, self.current_project_id)
if not task_list:
print("\nNo tasks found.")
return
print("\nTasks:")
for tid, meta in task_list.items():
status = "✅" if meta["done"] else "❌"
due = meta['due'] or "None"
print(f"[{tid}] {meta['name']} (Due: {meta['due']}) {status}")
def _edit_task(self):
self._list_tasks()
tid = self._collect_id("task", "edit")
name = input("New name (leave blank to skip): ").strip()
desc = input("New description (leave blank to skip): ").strip()
due = input("New due date (leave blank to skip): ").strip()
updated = self.tasks.edit_task(self.current_user, self.current_project_id, tid,
name = name if name else None,
description = desc if desc else None,
due = due if due else None)
if updated[0]:
print("\nTask updated")
else:
print(f"\nFailed to update task {updated[1]}")
def _delete_task(self):
self._list_tasks()
tid = self._collect_id("task", "delete")
if self._collect_non_blank_input("Confirmation",
"Are you sure you would like to delete this task (y/n): ").lower() == 'y':
deleted = self.tasks.delete_task(self.current_user, self.current_project_id, tid)
if deleted[0]:
print("\nTask deleted")
else:
print(f"Failed to delete task. {deleted[1]}")
else:
print("Cancelled")
def _toggle_task_status(self):
self._list_tasks()
tid = self._collect_id("task", "toggle status")
task_list = self.tasks.list_tasks(self.current_user, self.current_project_id)
if not tid in task_list:
print("\nInvalid task ID")
return
current = task_list[tid]['done']
success = self.tasks.mark_task(self.current_user, self.current_project_id, tid, not current)
if success[0]:
print("\nTask status updated")
else:
print(f"\nFailed to update task status. {success[1]}")