-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
309 lines (244 loc) · 8 KB
/
main.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
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
import time
from prettytable import PrettyTable
from datetime import datetime
import os
from database import (
get_users,
get_user,
add_user_db,
add_book_db,
get_books,
get_gender_id,
get_role_id,
get_genre_id,
get_genre_name,
get_role_name,
issue_book,
get_book_id,
return_book,
get_issued_books,
get_user_from_id,
get_book_from_id,
update_book_db,
update_user_db,
get_genres
)
from utils import verify_password, clear_screen
if not os.path.exists(".env"):
print("Config file not found!")
exit(1)
def add_user_cli(role="Student"):
firstname = input("Enter your first name : ")
surname = input("Enter your surname : ")
email = input("Enter your email : ")
mobile_phone = input("Enter your mobile number : (+91) ")
gender = input("Select your gender : ")
while not get_gender_id(gender):
gender = input("Please select valid gender : ")
address = input("Enter your address : ")
if role == "Admin":
password = input("Select a strong password : ")
confirm_password = input("Confirm your password : ")
while password != confirm_password:
confirm_password = input("Please enter correct password : ")
add_user_db(firstname,surname,email,mobile_phone,password,get_gender_id(gender),address,get_role_id(role))
else:
add_user_db(firstname,surname,email,mobile_phone,None,get_gender_id(gender),address,get_role_id(role))
def update_user_cli():
email = input("Enter the email of the user : ")
field = input("Enter the field to update : ")
new_value = input("Enter the new value : ")
if get_user(email):
update_user_db(get_user(email)[0], field, new_value)
else:
print("[ERROR] - User not found")
def display_users_cli():
users = get_users()
if users:
table = PrettyTable()
table.field_names = ["ID", "Name", "Email", "Role"]
for user in users:
table.add_row(
[user[0], f"{user[1]} {user[2]}", user[3], get_role_name(user[8])]
)
print(table)
else:
print("[INFO] - No users found")
def users_menu():
print(" Users Menu ")
print(" ---------- ")
while True:
print("1. Add User")
print("2. Update User")
print("3. Display Users")
print("4. Back")
choice = input("Enter your choice : ")
if choice == "1":
add_user_cli()
elif choice == "2":
update_user_cli()
elif choice == "3":
display_users_cli()
elif choice == "4":
break
else:
print("Invalid choice")
def add_book_cli():
title = input("Enter the title of the book : ")
author = input("Enter the author of the book : ")
genre = input("Enter the genre of the book : ")
while not get_genre_id(genre):
genre = input("Please select valid genre : ")
description = input("Enter the description of the book : ")
total_amount = input("Enter the total amount of the book : ")
add_book_db(title, author, get_genre_id(genre), description, total_amount)
def update_book_cli():
title = input("Enter the title of the book : ")
field = input("Enter the field to update : ")
new_value = input("Enter the new value : ")
if get_book_id(title):
update_book_db(get_book_id(title), field, new_value)
else:
print("[ERROR] - Book not found")
def display_books_cli():
books = get_books()
if books:
table = PrettyTable()
table.field_names = ["ID","Title","Author","Genre","Description","Total Amount"]
for book in books:
table.add_row([book[0], book[1], book[2], get_genre_name(book[3]), book[4], book[5]])
print(table)
else:
print("[INFO] - No books found")
def show_genres_cli():
genres = get_genres()
if genres:
table = PrettyTable()
table.field_names = ["ID", "Genre"]
for genre in genres:
table.add_row([genre[0], genre[1]])
print(table)
else:
print("[INFO] - No genres found")
def books_menu():
print(" Books Menu ")
print(" ---------- ")
while True:
print("1. Add Book")
print("2. Update Book")
print("3. Display Books")
print("4. Show Genres")
print("5. Back")
choice = input("Enter your choice : ")
if choice == "1":
add_book_cli()
elif choice == "2":
update_book_cli()
elif choice == "3":
display_books_cli()
elif choice == "4":
show_genres_cli()
elif choice == "5":
break
else:
print("Invalid choice")
def issue_book_cli():
email = input("Enter the email of the user : ")
title = input("Enter the title of the book : ")
if get_user(email):
if get_book_id(title):
issue_book(get_book_id(title), get_user(email)[0])
else:
print("[ERROR] - Book not found")
else:
print("[ERROR] - User not found")
def return_book_cli():
email = input("Enter the email of the user : ")
title = input("Enter the title of the book : ")
if get_user(email):
if get_book_id(title):
return_book(get_book_id(title), get_user(email)[0])
else:
print("[ERROR] - Book not found")
else:
print("[ERROR] - User not found")
def get_issued_books_cli():
books = get_issued_books()
if books:
table = PrettyTable()
table.field_names = ["ID", "Book", "User", "Issued Date", "Return Date"]
for book in books:
user = get_user_from_id(book[2])
table.add_row([book[0],get_book_from_id(book[1])[1],f"{user[1]} {user[2]}",book[3],book[4],])
print(table)
else:
print("[INFO] - No transaction found")
def issue_return_menu():
print(" Issue/Return Menu ")
print(" ----------------- ")
while True:
print("1. Issue Book")
print("2. Return Book")
print("3. Display Issued Books")
print("4. Back")
choice = input("Enter your choice : ")
if choice == "1":
issue_book_cli()
elif choice == "2":
return_book_cli()
elif choice == "3":
get_issued_books_cli()
elif choice == "4":
break
else:
print("Invalid choice")
def dashboard():
while True:
clear_screen()
print(" Welcome to LMS - Library Management System ")
print(" ----------------------------------------- ")
print("1. Users Menu")
print("2. Books Menu")
print("3. Issue/Return")
print("4. Exit")
choice = input("Enter your choice : ")
if choice == "1":
clear_screen()
users_menu()
elif choice == "2":
clear_screen()
books_menu()
elif choice == "3":
clear_screen()
issue_return_menu()
elif choice == "4":
clear_screen()
break
else:
print("Invalid choice")
def login():
email = input("Enter your email : ")
password = input("Enter your password : ")
user = get_user(email, role="Admin")
if user:
if verify_password(password, user[5]):
print("[SUCCESS] - Login successful")
time.sleep(1)
clear_screen()
dashboard()
else:
print("[ERROR] - Incorrect password")
else:
print("[ERROR] - Admin not found")
def main():
clear_screen()
while True:
if not get_users("Admin"):
print("Please add an admin to continue")
add_user_cli("Admin")
time.sleep(1)
clear_screen()
print("Please Login to continue")
login()
if __name__ == "__main__":
main()