Skip to content

Commit 8285d36

Browse files
committed
User Levels
- Added user levels to register function that limit what different users can do. - Fixed oversight where users could not change pet adoption status from true to false
1 parent e84a1ed commit 8285d36

File tree

3 files changed

+61
-26
lines changed

3 files changed

+61
-26
lines changed

FurEver_Friends.py

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@
1414
ANIMAL_DATA_FILE = "animals.json"
1515

1616
# Default user data if files do not exist
17-
DEFAULT_USER_DATA = {"ADMIN": "ADMIN"}
17+
DEFAULT_USER_DATA = {
18+
"ADMIN": {
19+
"password": "ADMIN",
20+
"level": 5
21+
}
22+
}
23+
1824
DEFAULT_ANIMAL_DATA = {}
1925

2026
def change_admin_password():
@@ -26,7 +32,10 @@ def change_admin_password():
2632
if new_password == confirm_password:
2733
with open(USER_DATA_FILE, 'r+') as user_file:
2834
data = json.load(user_file)
29-
data["ADMIN"] = new_password
35+
data["ADMIN"] = {
36+
"password": new_password,
37+
"level": 5
38+
}
3039
user_file.seek(0)
3140
json.dump(data, user_file, indent=4)
3241
user_file.truncate()
@@ -49,16 +58,20 @@ def login():
4958
users = json.load(user_file)
5059

5160
if username in users:
52-
if users[username] == password:
61+
if users[username]['password'] == password:
62+
user_level = users[username]['level'] # Retrieve the user level
5363
if username == "ADMIN" and password == "ADMIN":
5464
change_admin_password()
5565
admin_dashboard()
56-
return username # Ensure to return after admin login
66+
return username, user_level # Ensure to return after admin login
5767
elif username == "ADMIN":
5868
admin_dashboard()
59-
return username
69+
return username, user_level
6070
else:
61-
return username # Return username for non-admin users
71+
print("\nLogging in...")
72+
time.sleep(2)
73+
return username, user_level # Return username and user level for non-admin users
74+
6275
else:
6376
print(Fore.RED + "\nIncorrect password. Please try again." + Style.RESET_ALL)
6477
time.sleep(2)
@@ -89,31 +102,44 @@ def main():
89102

90103
if choice == '1':
91104
clear_screen()
92-
username = login()
105+
username, user_level = login()
93106
if username is not None:
94107
while True:
95108
clear_screen()
96109
print(Fore.CYAN + "\n📖 Main Menu 📖" + Style.RESET_ALL)
97-
print("\n1. " + Fore.GREEN + "🐶 Add a new animal" + Style.RESET_ALL)
98-
print("2. " + Fore.GREEN + "🔎 View all animals" + Style.RESET_ALL)
99-
print("3. " + Fore.GREEN + "🏡 Change animal adoption status" + Style.RESET_ALL)
100-
print("4. " + Fore.YELLOW + "🔐 Logout" + Style.RESET_ALL)
110+
print("\n1. " + Fore.GREEN + "🔎 View all animals" + Style.RESET_ALL)
111+
112+
# Initialize option counter
113+
option_counter = 2
114+
115+
# Adjust options based on user level
116+
if user_level >= 2:
117+
print(f"{option_counter}. " + Fore.GREEN + "🐶 Add a new animal" + Style.RESET_ALL)
118+
option_counter += 1
119+
if user_level >= 3:
120+
print(f"{option_counter}. " + Fore.GREEN + "🏡 Change animal adoption status" + Style.RESET_ALL)
121+
option_counter += 1
122+
123+
# Display Logout option with the correct number
124+
print (f"{option_counter}. " + Fore.YELLOW + "🔐 Logout" + Style.RESET_ALL)
101125
option = input("\nPlease select an option: ")
102126

103127
if option == '1':
104-
add_animal()
105-
elif option == '2':
106128
view_animals()
107-
elif option == '3':
129+
elif option == '2' and user_level >= 2:
130+
add_animal()
131+
elif option == '3' and user_level >= 3:
108132
change_adopted_status()
109-
elif option == '4':
133+
elif option == str(option_counter) and user_level >= 1:
110134
print("\nLogging out...")
135+
time.sleep(2)
111136
clear_screen()
112137
break
113138
else:
114-
print(Fore.RED + "\nInvalid option. Please try again." + Style.RESET_ALL)
139+
print(Fore.RED + "\nInvalid option. Please try again.")
115140
time.sleep(2)
116141
clear_screen()
142+
117143
elif choice == '2':
118144
print("\nExiting...")
119145
time.sleep(2)
@@ -125,3 +151,4 @@ def main():
125151

126152
if __name__ == "__main__":
127153
main()
154+

change_adopted_status.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@
99
def change_adopted_status():
1010
clear_screen()
1111
animals = load_data(ANIMAL_DATA_FILE)
12-
name = input("\nEnter the name of the animal to mark as adopted: ")
12+
name = input("\nEnter the name of the animal to toggle adoption status: ")
1313

1414
if name in animals:
15-
animals[name]['adopted'] = True
15+
# Toggle the adopted status
16+
animals[name]['adopted'] = not animals[name].get('adopted', False)
1617
save_data(animals, ANIMAL_DATA_FILE)
17-
print(Fore.GREEN + f"\n{name} has been marked as adopted!" + Style.RESET_ALL)
18+
if animals[name]['adopted']:
19+
print(Fore.GREEN + f"\n{name} has been marked as " + Fore.CYAN + "adopted!" + Style.RESET_ALL)
20+
else:
21+
print(Fore.GREEN + f"\n{name} has been marked as " + Fore.RED + "not adopted!" + Style.RESET_ALL)
1822
else:
1923
print(Fore.RED + f"\nNo animal found with the name {name}" + Style.RESET_ALL)
2024

21-
time.sleep(2)
25+
time.sleep(2)

register.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@ def register():
1818
password = getpass.getpass("Enter a password: ")
1919
confirm_password = getpass.getpass("Confirm your password: ")
2020
if password == confirm_password:
21-
users[username] = password
22-
save_data(users, USER_DATA_FILE)
23-
print(Fore.GREEN + "\nRegistration successful!" + Style.RESET_ALL)
24-
time.sleep(2)
25-
clear_screen()
26-
break # Exit the loop when registration is successful
21+
user_level = input("Enter your user level (1-3): ")
22+
if user_level.isdigit() and 1 <= int(user_level) <= 3:
23+
users[username] = {'password': password, 'level': int(user_level)}
24+
save_data(users, USER_DATA_FILE)
25+
print(Fore.GREEN + "\nRegistration successful!" + Style.RESET_ALL)
26+
time.sleep(2)
27+
clear_screen()
28+
break # Exit the loop when registration is successful
29+
else:
30+
print(Fore.RED + "\nInvalid user level. Please enter a number between 1 and 3." + Style.RESET_ALL)
2731
else:
2832
print(Fore.RED + "\nPasswords do not match. Please try again." + Style.RESET_ALL)
2933

0 commit comments

Comments
 (0)