-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask-cli.py
More file actions
413 lines (337 loc) · 13.9 KB
/
Copy pathtask-cli.py
File metadata and controls
413 lines (337 loc) · 13.9 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import sys
import json
import os
from time import sleep
from datetime import datetime
import hashlib
import secrets
# Global Variables:
TASKS_FILE = "tasks.json"
USERS_FILE = "users.json"
def save_data(data, filename):
with open(filename, "w") as f:
json.dump(data, f, indent=2)
def load_data(filename):
if not os.path.exists(filename):
with open(filename, "w") as f:
json.dump([], f)
with open(filename, "r") as f:
data = json.load(f)
return data
# Function calls placed here as it's not defining the functions
ALL_TASKS = load_data(TASKS_FILE)
ALL_USERS = load_data(USERS_FILE)
def error(*messages):
clear_screen()
for message in messages:
print(message)
sleep(1)
sleep(3)
clear_screen()
exit()
def clear_screen():
os.system("cls" if os.name == "nt" else "clear")
def user_login(username, password):
global ALL_USERS
if not ALL_USERS:
error(
"No users exist!",
"Please create one with the following syntax:",
"task-cli.py new_user [username] [password].",
)
if username.strip() == "":
error("Please enter username")
if password.strip() == "":
error("Please enter your password")
for user in ALL_USERS:
if user["username"] == username:
attempt = hashlib.pbkdf2_hmac(
"sha256", password.encode(), user["salt"].encode(), 100000
).hex()
if attempt == user["password"]:
return user
else:
error("Password Incorrect, Try again!")
else:
error("Username doesn't exist!")
def new_user(username, password):
global ALL_USERS
for user in ALL_USERS:
if user["username"] == username:
error("Username Already Exists, try again!")
user_id = max((user["id"] for user in ALL_USERS), default=0) + 1
now = str(datetime.now().replace(microsecond=0))
if username.strip() == "" or password.strip() == "":
error("Please enter a valid username and password to register an account!")
username = username.strip()
password = password.strip()
salt = secrets.token_hex(16)
hashed_password = hashlib.pbkdf2_hmac(
"sha256", password.encode(), salt.encode(), 100000
).hex()
add_user = {
"id": int(user_id),
"username": username,
"password": hashed_password,
"created_at": now,
"salt": salt,
"last_updated": None,
}
ALL_USERS.append(add_user)
save_data(ALL_USERS, USERS_FILE)
error("New User Added!")
def change_password(username, old_password, new_password):
global ALL_USERS
if not ALL_USERS:
error(
"No Existing Users!",
"Please create a new user first with 'new_user' [username] [password]",
)
current_user = user_login(username, old_password)
if new_password.strip() == "":
error("Please enter the new password")
new_salt = secrets.token_hex(16)
new_password_hashed = hashlib.pbkdf2_hmac(
"sha256", new_password.encode(), new_salt.encode(), 100000
).hex()
now = str(datetime.now().replace(microsecond=0))
for user in ALL_USERS:
if str(user["id"]) == str(current_user["id"]):
user["password"] = new_password_hashed
user["salt"] = new_salt
user["updated_at"] = now
save_data(ALL_USERS, USERS_FILE)
error("Password hass been updated!")
def add_task(username, password, task_title):
global ALL_TASKS
global ALL_USERS
current_user = user_login(username, password)
current_user_tasks = []
for task in ALL_TASKS:
if (
str(task["user_id"]) == str(current_user["id"])
and task["title"].lower() == task_title.lower()
):
error("You already have a task with that title!")
for task in ALL_TASKS:
if str(task["user_id"]) == str(current_user["id"]):
current_user_tasks.append(task)
task_id = max((task["id"] for task in current_user_tasks), default=0) + 1
new_task = {
"id": int(task_id),
"user_id": int(current_user["id"]),
"title": task_title,
"status": "To-Do",
"created_at": f"{datetime.now().replace(microsecond=0)}",
"updated_at": None,
}
ALL_TASKS.append(new_task)
save_data(ALL_TASKS, TASKS_FILE)
error(f"Task added for user '{current_user['username']}'!")
def delete_task(username, password, task_id):
global ALL_TASKS
# implement the search function via string also
current_user = user_login(username, password)
if current_user:
for i, task in enumerate(ALL_TASKS):
if str(task["user_id"]) == str(current_user["id"]):
if str(task["id"]) == task_id:
del ALL_TASKS[i]
save_data(ALL_TASKS, TASKS_FILE)
error("Task deleted successfully!")
else:
error("No task with this ID, try again!")
def update_task(username, password, task_search, new_title):
global ALL_TASKS
current_user = user_login(username, password)
now = str(datetime.now().replace(microsecond=0))
if current_user:
if task_search.isnumeric():
for task in ALL_TASKS:
if str(task["id"]) == str(task_search) and str(task["user_id"]) == str(
current_user["id"]
):
old = task["title"]
task["title"] = new_title
task["updated_at"] = now
save_data(ALL_TASKS, TASKS_FILE)
error(
f"Updated task for '{current_user['username']}' from '{old}' to '{new_title}'"
)
else:
counter = 0
for task in ALL_TASKS:
if task["title"].lower() == task_search.lower() and str(
task["user_id"]
) == str(current_user["id"]):
counter += 1
if counter == 1:
for task in ALL_TASKS:
if task["title"].lower() == task_search.lower() and str(
task["user_id"]
) == str(current_user["id"]):
old = task["title"]
task["title"] = new_title
task["updated_at"] = now
save_data(ALL_TASKS, TASKS_FILE)
error(
f"Updating task for '{current_user['username']}' from '{old}' to '{new_title}'"
)
else:
error(
"You have more than one task with the same title, please use task id instead!"
)
def view_tasks_print(user, category):
global ALL_TASKS
for task in ALL_TASKS:
if (
str(task["user_id"]) == str(user["id"])
and task["status"].lower() == category.lower()
):
print("-" * 50)
print(f"Task ID: {task['id']}")
print(f"Title: {task['title']}")
print(f"Status: {task['status']}")
print(f"Created at: {task['created_at']}")
print(f"Last updated: {task['updated_at']}")
print("-" * 50)
def list_task(username, password, list_type):
global ALL_TASKS
current_user = user_login(username, password)
if list_type == "all":
view_tasks_print(current_user, "all")
elif list_type in ["done", "completed", "finished", "complete"]:
view_tasks_print(current_user, "completed")
elif list_type in [
"new",
"new tasks",
"just added",
"recent",
"recently added",
"to-do",
"todo",
]:
view_tasks_print(current_user, "to-do")
elif list_type in [
"doing",
"current",
"currently active",
"active",
"active tasks",
"current tasks",
]:
view_tasks_print(current_user, "in-progress")
else:
error(
"To view tasks you can enter a number of phrases that correlate to the task type you would like to view. Here is a list of the ones you can use:",
"For tasks that are completed: ['done', 'complete', 'completed', 'finished']",
"For tasks that are in progress: ['doing', 'current', 'currently active', 'active', 'active tasks', 'current tasks']",
"For tasks that recently added or awaiting to be started ['new', 'new tasks', 'just added', 'recent', 'recently added']",
"To view all tasks, just exclude a parameter for the type of tasks you want to view or type 'all'",
)
def mark_as_complete(username, password, task_search):
global ALL_TASKS
now = str(datetime.now().replace(microsecond=0))
current_user = user_login(username, password)
if task_search.isnumeric():
for task in ALL_TASKS:
if (
str(task["user_id"]) == str(current_user["id"])
and str(task["id"]) == str(task_search)
and task["status"].lower() != "COMPLETED".lower()
):
task["status"] = "COMPLETED"
task["updated_at"] = now
save_data(ALL_TASKS, TASKS_FILE)
print(f"Task '{task['title']}' has now been marked as completed!")
break
else:
error("This task is already marked as completed!")
else:
for task in ALL_TASKS:
if (
task["title"].lower() == task_search.lower()
and str(task["user_id"]) == str(current_user["id"])
and task["status"].lower() != "COMPLETED".lower()
):
task["status"] = "COMPLETED"
task["updated_at"] = now
save_data(ALL_TASKS, TASKS_FILE)
print(f"Task '{task['title']}' has now been marked as completed!")
break
else:
error("This task is already marked as completed!")
def mark_in_progress(username, password, task_search):
global ALL_TASKS
now = str(datetime.now().replace(microsecond=0))
current_user = user_login(username, password)
if task_search.isnumeric():
for task in ALL_TASKS:
if (
str(task["user_id"]) == str(current_user["id"])
and str(task["id"]) == str(task_search)
and task["status"].lower() != "IN-PROGRESS".lower()
):
task["status"] = "IN-PROGRESS"
task["updated_at"] = now
save_data(ALL_TASKS, TASKS_FILE)
print(f"Task '{task['title']}' has now been marked as in-progress!")
break
else:
error("This task is already marked as in-progress!")
else:
for task in ALL_TASKS:
if (
task["title"].lower() == task_search.lower()
and str(task["user_id"]) == str(current_user["id"])
and task["status"].lower() != "IN-PROGRESS".lower()
):
task["status"] = "IN-PROGRESS"
task["updated_at"] = now
save_data(ALL_TASKS, TASKS_FILE)
print(f"Task '{task['title']}' has now been marked as in-progress!")
break
else:
error("This task is already marked as in-progress!")
def main():
if len(sys.argv) <= 2 or "--help" in sys.argv:
error(
"To use the Task Manager CLI, please enter with the following syntax:",
"To add new user: task-cli.py new-user [new username] [new password]",
"To change password: task-cli.py change-password [username] [old password] [new password]",
"To add a task: task-cli.py [username] [password] add-task [task title]",
"To update a task: task-cli.py [username] [password] update-task [task id number or 'task title']",
"To delete a task: task-cli.py [username] [password] delete-task [task id number or 'task title']",
"To marks a task as complete: task-cli.py [username] [password] mark-complete [task id number or 'task title']",
"To marks a task as complete: task-cli.py [username] [password] mark-in-progress [task id number or 'task title']",
"To view all tasks: task-cli.py [username] [password] view",
"To view specific type of tasks: task-cli.py [username] [password] view [type of task i.e. 'to be completed']",
"NOTE: Where you could use more than one word for a criteria please enclose in speech marks ('')",
"Type task-cli.py --help to see this menu again",
)
elif (
len(sys.argv) == 4
and sys.argv[1].lower() == "new-user"
or sys.argv[1].lower() == "add-user"
):
new_user(sys.argv[2], sys.argv[3])
elif len(sys.argv) == 5 and sys.argv[1].lower() == "change-password":
change_password(sys.argv[2], sys.argv[3], sys.argv[4])
elif len(sys.argv) == 5 and sys.argv[3].lower() == "add-task":
add_task(sys.argv[1], sys.argv[2], sys.argv[4])
elif len(sys.argv) == 6 and sys.argv[3].lower() == "update-task":
update_task(sys.argv[1], sys.argv[2], sys.argv[4], sys.argv[5])
elif len(sys.argv) == 5 and sys.argv[3].lower() == "delete-task":
delete_task(sys.argv[1], sys.argv[2], sys.argv[4])
elif len(sys.argv) == 4 and sys.argv[3].lower() == "view":
list_task(sys.argv[1], sys.argv[2], "all".lower())
elif len(sys.argv) == 5 and sys.argv[3].lower() == "view":
list_task(sys.argv[1], sys.argv[2], sys.argv[4].lower())
elif len(sys.argv) == 5 and sys.argv[3].lower() == "mark-complete":
mark_as_complete(sys.argv[1], sys.argv[2], sys.argv[4].lower())
elif len(sys.argv) == 5 and sys.argv[3].lower() == "mark-in-progress":
mark_in_progress(sys.argv[1], sys.argv[2], sys.argv[4].lower())
else:
error("Invalid command!", "Type task-cli.py --help for manual")
if __name__ == "__main__":
main()