-
Notifications
You must be signed in to change notification settings - Fork 0
/
commons.py
160 lines (141 loc) · 5.58 KB
/
commons.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
import datetime, os
def calculate_age(birthdate):
try:
birthdate = datetime.strptime(birthdate, "%dd-%mm-%YYYY")
today = datetime.now()
age = (
today.year
- birthdate.year
- ((today.month, today.day) < (birthdate.month, birthdate.day))
)
return age
except ValueError:
print("Incorrect date format, should be DD-MM-YYYY")
return None
def validate_password(password):
errors = []
password = password.strip()
if len(password) < 8:
errors.append("Your password must be at least 8 characters long.")
if not any(char.isupper() or char.islower() for char in password):
errors.append("Your password must include letters.")
if not any(char.isdigit() for char in password):
errors.append("Your password must include numbers.")
if any(char == " " for char in password):
errors.append("Your password must not have spaces.")
if not any(char in '[!@#$%^&*(),.?":{}|<>]' for char in password):
errors.append("Your password must have at least 1 symbol.")
if "@" in password:
print("Please do not use @!")
if errors:
print("Invalid Password!")
for error in errors:
print(error)
return False
return True
def login(username, password, tries):
tries += 1
try:
username, role = username.strip().split("@")
except Exception as e:
return [False, f"Error: {e}", tries]
with open("texts/users.txt", "r") as file:
users = file.readlines()
for user in users:
user_data = user.strip().split(", ")
if (
user_data[0] == username
and user_data[1] == password
and user_data[2] == role
):
tries = 1
return [True, (username, role), tries]
return [
False,
f"Incorrect Username or Password, You have {3-tries} tries remaining!",
tries,
]
def validate_username(username):
if len(username.split(" ")) != 1:
print("... Invalid Username! Your username must be a single word! ...")
return False
else:
return True
def signup(username, password):
try:
username, role = username.strip().split("@")
except Exception as e:
return [False, f"Error: {e}"]
valid_username = validate_username(username)
valid_password = validate_password(password)
if valid_password == True and valid_username == True:
try:
with open("texts/users.txt", "a+") as file:
users = file.readlines()
if any(
user.strip().split(", ")[0] == username
and user.strip().split(", ")[1] == role
for user in users
):
print("... User already exists ...")
print("Exiting...")
os.system("sleep 3")
os.system("clear")
return
file.write(f"{username}, {password}, {role}\n")
print("... User added successfully ...")
print("Exiting...")
os.system("sleep 3")
os.system("clear")
except FileNotFoundError:
with open("users.txt", "w") as file:
file.write(f"{username}, {password}, {role}\n")
print("... User added successfully ...")
print("Exiting...")
os.system("sleep 3")
os.system("clear")
except PermissionError:
print("... Permission denied. Cannot access the file ...")
print("Exiting...")
os.system("sleep 3")
os.system("clear")
except Exception as e:
print(f"Error: {e}")
print("Exiting...")
os.system("sleep 3")
os.system("clear")
return True
else:
return False
def change_password(username):
try:
password = input("Enter old password: ")
while True:
# Take the username and password from users.txt and compare username and password
with open("texts/users.txt", "r") as file:
users = file.readlines()
for user in users:
user_data = user.strip().split(", ")
if user_data[0] == username and user_data[1] == password:
new_password = input("Enter new password: ").strip()
if validate_password(new_password):
# Replace the old password with the new one
new_user_data = f"{username}, {new_password}, {user_data[2]}\n"
users[users.index(user)] = new_user_data
# Write the updated user data back to the file
with open("texts/users.txt", "w") as file:
file.writelines(users)
print("... Password changed successfully ...")
return
else:
print("Invalid new password. Password not changed.")
print("Exiting...")
os.system("sleep 3")
os.system("clear")
return
print("Username or password is incorrect. Password not changed.")
quit = input("Do you want to quit, Yes = q").lower()
if quit == "q":
break
except FileNotFoundError:
print("users.txt file not found.")