14
14
ANIMAL_DATA_FILE = "animals.json"
15
15
16
16
# 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
+
18
24
DEFAULT_ANIMAL_DATA = {}
19
25
20
26
def change_admin_password ():
@@ -26,7 +32,10 @@ def change_admin_password():
26
32
if new_password == confirm_password :
27
33
with open (USER_DATA_FILE , 'r+' ) as user_file :
28
34
data = json .load (user_file )
29
- data ["ADMIN" ] = new_password
35
+ data ["ADMIN" ] = {
36
+ "password" : new_password ,
37
+ "level" : 5
38
+ }
30
39
user_file .seek (0 )
31
40
json .dump (data , user_file , indent = 4 )
32
41
user_file .truncate ()
@@ -49,16 +58,20 @@ def login():
49
58
users = json .load (user_file )
50
59
51
60
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
53
63
if username == "ADMIN" and password == "ADMIN" :
54
64
change_admin_password ()
55
65
admin_dashboard ()
56
- return username # Ensure to return after admin login
66
+ return username , user_level # Ensure to return after admin login
57
67
elif username == "ADMIN" :
58
68
admin_dashboard ()
59
- return username
69
+ return username , user_level
60
70
else :
61
- return username # Return username for non-admin users
71
+ print ("\n Logging in..." )
72
+ time .sleep (2 )
73
+ return username , user_level # Return username and user level for non-admin users
74
+
62
75
else :
63
76
print (Fore .RED + "\n Incorrect password. Please try again." + Style .RESET_ALL )
64
77
time .sleep (2 )
@@ -89,31 +102,44 @@ def main():
89
102
90
103
if choice == '1' :
91
104
clear_screen ()
92
- username = login ()
105
+ username , user_level = login ()
93
106
if username is not None :
94
107
while True :
95
108
clear_screen ()
96
109
print (Fore .CYAN + "\n 📖 Main Menu 📖" + Style .RESET_ALL )
97
- print ("\n 1. " + 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 ("\n 1. " + 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 )
101
125
option = input ("\n Please select an option: " )
102
126
103
127
if option == '1' :
104
- add_animal ()
105
- elif option == '2' :
106
128
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 :
108
132
change_adopted_status ()
109
- elif option == '4' :
133
+ elif option == str ( option_counter ) and user_level >= 1 :
110
134
print ("\n Logging out..." )
135
+ time .sleep (2 )
111
136
clear_screen ()
112
137
break
113
138
else :
114
- print (Fore .RED + "\n Invalid option. Please try again." + Style . RESET_ALL )
139
+ print (Fore .RED + "\n Invalid option. Please try again." )
115
140
time .sleep (2 )
116
141
clear_screen ()
142
+
117
143
elif choice == '2' :
118
144
print ("\n Exiting..." )
119
145
time .sleep (2 )
@@ -125,3 +151,4 @@ def main():
125
151
126
152
if __name__ == "__main__" :
127
153
main ()
154
+
0 commit comments