-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo.py
255 lines (226 loc) · 8.46 KB
/
todo.py
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
# todo.py
from datetime import datetime
from models import Session, User, Task
from sqlalchemy.exc import IntegrityError
import sys
import getpass
import re
import bcrypt
def login():
session = Session()
username = input("Enter your username: ")
password = getpass.getpass("Enter your password: ")
user = session.query(User).filter(User.username == username).first()
if user:
if user.check_password(password):
print("Login successful!")
return user
else:
print("Invalid password.")
else:
print("Invalid username.")
return None
def register():
session = Session()
username = input("Enter a username: ")
while True:
password = getpass.getpass("Enter a password: ")
if len(password) < 8 or not re.search("[A-Z]", password) or not re.search("[a-z]", password) or not re.search("[0-9]", password) or not re.search("[!@#$%^&*()\[\]\-+=?]", password):
print("Password must be at least 8 characters long and contain at least one uppercase letter, one lowercase letter, one digit, and one special character.")
else:
break
try:
new_user = User(username=username)
new_user.set_password(password)
session.add(new_user)
session.commit()
print("Registration successful! You can now login.")
except IntegrityError:
session.rollback()
print("Username already exists. Please choose another one.")
def add_task():
session = Session()
while True:
title = input("Enter the title for the new task: ")
if title:
break
else:
print("Title cannot be empty. Please enter a title.")
while True:
description = input("Enter the description for the new task: ")
if description:
break
else:
print("Description cannot be empty. Please enter a description.")
while True:
due_date_str = input("Enter the due date for the task (YYYY-MM-DD HH:MM) or leave blank if none: ")
if not due_date_str:
break
try:
due_date = datetime.strptime(due_date_str, '%Y-%m-%d %H:%M')
break
except ValueError:
print("Invalid date format. Please enter the due date in the format YYYY-MM-DD HH:MM or leave blank if none.")
if due_date_str:
task = Task(title=title, description=description, due_date=due_date)
else:
task = Task(title=title, description=description)
session.add(task)
session.commit()
print("Task added successfully.")
def delete_task():
session = Session()
tasks = session.query(Task).all()
if tasks:
print("Task List:")
for task in tasks:
print(f"Task #{task.id}: {task.title}")
while True:
task_id_str = input("Enter the ID of the task you want to delete: ")
if task_id_str.isdigit():
task_id = int(task_id_str)
if any(task.id == task_id for task in tasks):
break
else:
print("Task with provided ID not found. Please enter a valid ID.")
else:
print("Invalid input. Please enter a numerical ID.")
task = session.query(Task).filter(Task.id == task_id).first()
if task:
session.delete(task)
session.commit()
print("Task deleted successfully.")
else:
print("Task not found.")
else:
print("No tasks found.")
def update_task():
session = Session()
tasks = session.query(Task).all()
if tasks:
print("Task List:")
for task in tasks:
print(f"Task #{task.id}: {task.title}")
while True:
task_id_str = input("Enter the ID of the task you want to update: ")
if task_id_str.isdigit():
task_id = int(task_id_str)
if any(task.id == task_id for task in tasks):
break
else:
print("Task with provided ID not found. Please enter a valid ID.")
else:
print("Invalid input. Please enter a numerical ID.")
task = session.query(Task).filter(Task.id == task_id).first()
if task:
while True:
new_title = input("Enter the new title for the task: ")
if new_title:
break
else:
print("Title cannot be empty. Please enter a title.")
while True:
new_description = input("Enter the new description for the task: ")
if new_description:
break
else:
print("Description cannot be empty. Please enter a description.")
while True:
new_due_date_str = input("Enter the new due date for the task (YYYY-MM-DD HH:MM) or leave blank if unchanged: ")
if not new_due_date_str or (new_due_date_str and len(new_due_date_str) == 16 and new_due_date_str[4] == '-' and new_due_date_str[7] == '-' and new_due_date_str[10] == ' ' and new_due_date_str[13] == ':'):
break
else:
print("Invalid date format. Please enter the due date in the format YYYY-MM-DD HH:MM or leave blank if unchanged.")
task.title = new_title
task.description = new_description
if new_due_date_str:
new_due_date = datetime.strptime(new_due_date_str, '%Y-%m-%d %H:%M')
task.due_date = new_due_date
session.commit()
print("Task updated successfully.")
else:
print("Task not found.")
else:
print("No tasks found.")
def list_tasks():
session = Session()
tasks = session.query(Task).all()
if tasks:
print("All Tasks:")
for task in tasks:
print(f"Task #{task.id}:")
print(f" Title: {task.title}")
print(f" Description: {task.description}")
print(f" Due Date: {task.due_date.strftime('%Y-%m-%d %H:%M') if task.due_date else 'None'}")
print(f" Is Completed: {'Yes' if task.is_completed else 'No'}")
if task.user:
print(f" Assigned to: {task.user.username}")
else:
print(" Assigned to: None")
else:
print("No tasks found.")
def main():
while True:
print("\nWhat would you like to do?")
print("1. Login")
print("2. Register")
print("3. Exit")
choice = input("Enter your choice (1-3): ")
if choice == "1":
user = login()
if user:
task_menu(user)
elif choice == "2":
register()
elif choice == "3":
print("Exiting...")
break
else:
print("Invalid choice. Please enter a number between 1 and 3.")
def view_task_details():
session = Session()
tasks = session.query(Task).all()
if tasks:
print("Task List:")
for task in tasks:
print(f"Task #{task.id}: {task.title}")
task_id = int(input("Enter the ID of the task you want to view: "))
task = session.query(Task).filter(Task.id == task_id).first()
if task:
print(f"Task #{task.id}:")
print(f" Title: {task.title}")
print(f" Description: {task.description}")
print(f" Created At: {task.created_at}")
print(f" Due Date: {task.due_date}")
print(f" Is Completed: {task.is_completed}")
else:
print("Task not found.")
else:
print("No tasks found.")
def task_menu(user):
while True:
print("\nTask Management Menu:")
print("1. Add a new task")
print("2. Delete a task")
print("3. Update a task")
print("4. List all tasks")
print("5. View task details")
print("6. Logout")
choice = input("Enter your choice (1-6): ")
if choice == "1":
add_task()
elif choice == "2":
delete_task()
elif choice == "3":
update_task()
elif choice == "4":
list_tasks()
elif choice == "5":
view_task_details()
elif choice == "6":
print("Logging out...")
break
else:
print("Invalid choice. Please enter a number between 1 and 6.")
if __name__ == "__main__":
main()