-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLMS.py
More file actions
387 lines (364 loc) · 13.6 KB
/
Copy pathLMS.py
File metadata and controls
387 lines (364 loc) · 13.6 KB
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import mysql.connector
from mysql.connector import Error
import datetime
def connection_creator(hostname,username,password,db):
connection = None
try:
connection = mysql.connector.connect(host = hostname,user = username,password=password,database=db)
print("connection to MYSQL is successfull")
except Error as err:
print("The error ",{err} ," occured")
return connection
def create_database(connection ,query):
mycursor = connection.cursor()
try:
mycursor.execute(query)
print("Database created")
except Error as err:
print(f"The error '{err}' occurred")
class Books:
def __init__(self,name,author,copies):
self.name = name
self.author = author
self.status = False
self.copies = copies
self.issuedate = None
self.returndate = None
def add_copies(self,n):
self.copies+=n
def set_status(self,status):
self.status = status
class library:
def __init__(self):
self.__librarianAccount={'admin':'admin'}
self.db = connection_creator("localhost" ,"root" ,"imranfrommoradabad231321","lmsDBMS")
self.mycursor = self.db.cursor(buffered=True)
self.displayTables
self.Id = None
# create_database_query = "CREATE DATABASE lmsDBMS"
# create_database(self.db, create_database_query)
# self.mycursor.execute(f"CREATE DATABASE lmsDBMS")
# print(self.db)
# self.createusertable()
# self.createtransactionTable()
# self.books = {}
self.mainMenu()
def createbooksTable(self):
query= """
CREATE TABLE Books (
BookID INT AUTO_INCREMENT PRIMARY KEY,
BookName VARCHAR(100),
BookAuthor VARCHAR(100),
BookStatus BOOLEAN,
TotalCopies INT,
AvailableCopies INT
)
"""
self.mycursor.execute(query)
self.db.commit()
def createusertable(self):
query= """
CREATE TABLE Users (
UserID INT AUTO_INCREMENT PRIMARY KEY,
UserName VARCHAR(100),
UserAddress VARCHAR(100),
blockStatus BOOLEAN
)
"""
self.mycursor.execute(query)
self.db.commit()
def createtransactionTable(self):
create_table_query = """
CREATE TABLE transactions (
transaction_id INT AUTO_INCREMENT PRIMARY KEY,
userID INT,
bookID INT,
issue_date DATE,
return_date DATE,
return_status VARCHAR(20),
FOREIGN KEY (userID) REFERENCES users(userID),
FOREIGN KEY (bookID) REFERENCES books(bookID)
)
"""
# Execute the query
self.mycursor.execute(create_table_query)
# Commit the changes
self.db.commit()
#func to identify librarian or user
def loginInfo(self):
print('Who are you?\n1.Librarian 2.LMS user 3.Quit' )
return input('Enter (1,2 or 3)\n')
def displayTables(self):
show_tables_query = "SHOW TABLES;"
self.mycursor.execute(show_tables_query)
tables = self.mycursor.fetchall()
for table in tables:
print(table[0])
describetable = f"DESCRIBE {table[0]}"
self.mycursor.execute(describetable)
table_description = self.mycursor.fetchall()
for column_info in table_description:
print(column_info)
def mainMenu(self):
print('Welcome to Namal Library System')
# self.createbooksTable()
self.displayTables()
self.DisplayData('books')
while True:
ques = self.loginInfo()
if ques=='1':
login = self.librarian_login()
if login:
self.LibrarianMenu()
else:
print('Incorrect Credentials')
elif ques== '2':
while self.UserPage():
pass
elif ques =='3':
break
else:
print('Enter correct Number please')
#librarian login
def librarian_login(self):
while True:
username = input('Enter Your username:')
password = input('Enter Your Password:')
if username in self.__librarianAccount and self.__librarianAccount[username]==password:
print('Access granted')
return True
else:
print('Access Denied')
relogin= input('Want to login again?(y/n)')
if relogin=='y':
continue
else:
return False
#Librarian functions ADD_book,Delete_book
def LibrarianMenu(self):
print('Welcome to LMS')
print('Type followin number to perform respective actions:')
print('1.Add Book')
task = input()
if task=='1':
self.AddBook()
elif task=='2':
self.DeleteBook()
def DisplayData(self,table):
select_query = f"SELECT * FROM {table}"
self.mycursor.execute(select_query)
rows = self.mycursor.fetchall()
for row in rows:
print(row)
def AddBook(self):
try:
bookname = input('Enter book Name: ')
bookauthor = input('Enter Author Name:')
try:
copies = int(input('how many copies are you adding?'))
except ValueError:
raise ValueError('Incorrect number of copies')
AddBookQuery = """
INSERT INTO books (BookName, BookAuthor, BookStatus, TotalCopies, AvailableCopies)
VALUES (%s, %s, %s, %s, %s)
"""
values = (bookname, bookauthor, False, copies, copies)
self.mycursor.execute(AddBookQuery, values)
self.db.commit()
print('Book is Successfully added.')
except mysql.connector.Error as err:
print(f"Error: {err}")
def AreAvailableAndTotalcopiesEqual(self,bookname,bookauthor):
select_query = """
SELECT TotalCopies, AvailableCopies
FROM books
WHERE BookName = %s
AND BookAuthor = %s
"""
values = (bookname, bookauthor)
self.mycursor.execute(select_query, values)
copies = self.mycursor.fetchone()
return copies[0]==copies[1],copies[0]
def DeleteBook(self):
try:
bookname = input('Enter book Name: ')
bookauthor = input('Enter Author Name:')
try:
copies = input('how many copies do you want to delete(type A to delete All copies)?')
except ValueError:
raise ValueError('Incorrect number of copies')
if copies =='A':
DeleteBookQuery = """
DELETE FROM books
WHERE BookName = %s
AND BookAuthor = %s
"""
values = (bookname, bookauthor)
self.mycursor.execute(DeleteBookQuery, values)
self.db.commit()
print('Successfully Deleted.')
else:
check,Totalcopies = self.AreAvailableAndTotalcopiesEqual()
if check():
updateQuery = """
UPDATE books
SET TotalCopies = %s, AvailableCopies = %s
WHERE BookName = %s
AND BookAuthor = %s
"""
values = (Totalcopies, Totalcopies, bookname, bookauthor)
self.mycursor.execute(updateQuery, values)
self.db.commit()
print('Successfully Deleted.')
else:
print(f'Kindly recieve all issued books from users,to modify copies of {bookname} Book')
except mysql.connector.Error as err:
print(f"Error: {err}")
def UserPage(self):
while True:
task = input('1.Login\t\t2.SignUp\t\t3.Quit')
if task =='1':
self.Userlogin()
elif task=='2':
self.UserSignUp()
elif task =='3':
break
else:
print('Enter Correct Number:)')
def Userlogin(self):
while True:
username = input('Enter Your username:')
password = input('Enter Your Password:')
self.userId=self.UserloginCheck(username,password)
if self.userId==None:
print('Incorrect Credentials:)')
if input('Loin aain?(y/n)')=='y':
pass
else:
break
else:
print('Login Successfull')
self.UserMenu()
def AddUserInDBMS(self,username,useraddress,blockstatus,password):
try :
insert_query = """
INSERT INTO Users (UserName, UserAddress, blockStatus,Password)
VALUES (%s, %s, %s,%s)
"""
values = (username, useraddress, blockstatus,password)
self.mycursor.execute(insert_query, values)
self.db.commit()
print('User added successfully.')
except mysql.connector.Error as err:
print(f"Error: {err}")
def Usernamecheck(self,username):
try:
select_query = """
SELECT COUNT(*)
FROM Users
WHERE UserName = %s
"""
values = (username,)
self.mycursor.execute(select_query, values)
user_count = self.mycursor.fetchone()
return user_count[0] > 0
except mysql.connector.Error as err:
print(f"Error: {err}")
return None
def UserSignUp(self):
while True:
username = input('Enter your Username:')
if self.Usernamecheck(username):
print('Username Already Exist!')
else:
break
password = input('Enter your Password:')
location = input('Enter your Location:')
self.AddUserInDBMS(username,location,False,password)
def UserloginCheck(self,username,password):
try:
select_query = """
SELECT UserID
FROM Users
WHERE UserName = %s AND Password = %s
"""
values = (username, password)
self.mycursor.execute(select_query, values)
user_id = self.mycursor.fetchone()
return user_id[0] if user_id else None
except mysql.connector.Error as err:
print(f"Error: {err}")
return None
def UserMenu(self):
while True:
task = input('Enter following Number to perform following Tasks\n1.searc Book\n2.Return Book')
if task =='1':
self.SearchBook()
elif task=='2':
self.ReturnBook()
else:
print('Enter correct Number')
def SearchBook(self):
try:
keyword= input("Enter Name of Book or Author:")
search_query = """
SELECT *
FROM books
WHERE BookName LIKE %s OR BookAuthor LIKE %s
"""
keyword = f"%{keyword}%"
values = (keyword, keyword)
self.mycursor.execute(search_query, values)
books = self.mycursor.fetchall()
if books:
for book in books:
print("Name of Book :",book[1])
print("Name of Author :",book[2])
print("Available copies :",book[5])
issue = input("Want to issue tis book?(y/n)")
if issue=="y":
self.IssueBook(book[0])
else:
print('No matching books found.')
except mysql.connector.Error as err:
print(f"Error: {err}")
return None
def IssueBook(self,bookid):
try:
today = datetime.today()
return_date = today + datetime.timedelta(days=5)
insert_query = """
INSERT INTO transactions (userID, bookID, issue_date, return_date, return_status)
VALUES (%s, %s, %s, %s, %s)
"""
values = (self.userId, bookid,today, return_date, False)
self.mycursor.execute(insert_query, values)
self.db.commit()
print('Book Issue successfully.')
except mysql.connector.Error as err:
print(f"Error: {err}")
def ReturnBook(self,bookid):
try:
select_query = """
SELECT *
FROM transactions
WHERE userID = %s AND bookID = %s
"""
values = (self.userId,bookid)
self.mycursor.execute(select_query, values)
transaction_details = self.mycursor.fetchone()
except mysql.connector.Error as err:
print(f"Error: {err}")
return None
if transaction_details:
print(f'Transaction Details: {transaction_details}')
alter_query = """
ALTER TABLE transactions
MODIFY COLUMN return_status BOOLEAN DEFAULT TRUE
"""
self.mycursor.execute(alter_query)
self.db.commit()
print('Book returned successfully.')
else:
print('No matching transaction found.')
library()