-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
411 lines (349 loc) · 12.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
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import sys, datetime
from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog
from PyQt5.QtWidgets import QMessageBox, QTableWidgetItem
from PyQt5.uic import loadUi
import mysql.connector
class LoginWindow(QDialog):
def __init__(self):
super().__init__()
loadUi("Login.ui", self)
self.setWindowTitle("Login")
self.login_button.clicked.connect(self.check_login)
# Initialize a database connection
self.db = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="lms1",
)
def check_login(self):
username = self.username_input.text()
password = self.password_input.text()
# Replace with your actual table name and column names
cursor = self.db.cursor()
cursor.execute(
"SELECT * FROM login_admin WHERE username = %s AND password = %s",
(username, password),
)
result = cursor.fetchone()
if result:
self.accept() # Close the login window if login is successful
else:
QMessageBox.warning(self, "Login Failed", "Invalid username or password")
# ``````````````````````````````````````
# ``````````````````````````````````````
class LibraryApp(QMainWindow):
def __init__(self):
super().__init__()
loadUi("LMS1.ui", self)
self.setWindowTitle("Library Management System")
self.Handel_Buttons()
def Handel_Buttons(self):
self.pushButton_5.clicked.connect(self.open_Status_Tab)
self.pushButton_8.clicked.connect(self.open_Books_Tab)
self.pushButton_7.clicked.connect(self.open_Student_Tab)
self.pushButton_6.clicked.connect(self.open_Admin_Tab)
self.pushButton.clicked.connect(self.Add_Status)
self.pushButton_14.clicked.connect(self.Show_All_Status)
self.pushButton_13.clicked.connect(self.Show_All_Students)
self.pushButton_2.clicked.connect(self.Add_New_Books)
self.pushButton_4.clicked.connect(self.Search_Books)
self.pushButton_3.clicked.connect(self.Delete_Books)
self.pushButton_9.clicked.connect(self.Show_All_Books)
self.pushButton_12.clicked.connect(self.Add_New_User)
self.pushButton_11.clicked.connect(self.Search_User)
self.pushButton_10.clicked.connect(self.Delete_User)
# Tab Opening Btns -------------------------------------
def open_Status_Tab(self):
self.tabWidget.setCurrentIndex(0)
def open_Books_Tab(self):
self.tabWidget.setCurrentIndex(1)
def open_Student_Tab(self):
self.tabWidget.setCurrentIndex(2)
def open_Admin_Tab(self):
self.tabWidget.setCurrentIndex(3)
# Books Tab Functions ----------------------------------
# ---------------------
def Add_New_Books(self):
self.db = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="lms1",
)
book_code = self.lineEdit_5.text()
book_title = self.lineEdit_2.text()
book_author = self.lineEdit_4.text()
book_price = self.lineEdit_7.text()
cursor = self.db.cursor()
cursor.execute(
"""
INSERT INTO books (book_code,book_title,book_author,book_price)
VALUES (%s , %s , %s , %s)
""",
(
book_code,
book_title,
book_author,
book_price,
),
)
self.db.commit()
self.plainTextEdit.setPlainText(
f"""
Added Book : {book_title}\n
Added Book Code : {book_code}\n
Added Book Author : {book_author}\n
Added Book price : {book_price}\n
"""
)
self.statusBar().showMessage("New Book Added")
self.db.close()
# ---------------------
def Search_Books(self):
book_title_inpt = self.book_title_srch.text()
self.db = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="lms1",
)
cursor = self.db.cursor()
cursor.execute("SELECT * FROM books WHERE book_title = %s", [(book_title_inpt)])
data = cursor.fetchone()
if data:
self.lineEdit_8.setText(data[2])
self.lineEdit_6.setText(data[1])
self.lineEdit_9.setText(data[3])
self.plainTextEdit_2.setPlainText(
f"""
Book Found\n
Book Code : {data[1]}\n
Book Author : {data[2]}\n
Book price : {data[3]}\n
"""
)
else:
self.plainTextEdit_2.setPlainText("No Book Found")
self.db.close()
# ---------------------
def Delete_Books(self):
self.db = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="lms1",
)
cursor = self.db.cursor()
book_title_inpt = self.book_title_srch.text()
warning = QMessageBox.warning(
self, "Delete Book", "Are you sure", QMessageBox.Yes | QMessageBox.No
)
if warning == QMessageBox.Yes:
cursor.execute(
"DELETE FROM books WHERE book_title = %s", [(book_title_inpt)]
)
self.db.commit()
self.statusBar().showMessage("Book Deleted")
self.db.close()
# ---------------------
def Show_All_Books(self):
self.db = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="lms1",
)
cursor = self.db.cursor()
cursor.execute("SELECT * FROM books")
data = cursor.fetchall()
# print(data)
self.tableWidget_3.setRowCount(0)
self.tableWidget_3.insertRow(0)
for row, form in enumerate(data):
for column, item in enumerate(form):
self.tableWidget_3.setItem(row, column, QTableWidgetItem(str(item)))
column += 1
row_position = self.tableWidget_3.rowCount()
self.tableWidget_3.insertRow(row_position)
self.db.close()
# Admins Tab Functions ----------------------------------
# ---------------------
def Add_New_User(self):
self.db = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="lms1",
)
cursor = self.db.cursor()
username = self.lineEdit_10.text()
email = self.lineEdit_11.text()
password = self.lineEdit_13.text()
cpassword = self.lineEdit_12.text()
if password == cpassword:
cursor.execute(
"""
INSERT INTO login_admin (username , email , password)
VALUES (%s , %s , %s)
""",
(username, email, password),
)
self.db.commit()
self.label_22.setText("New Admin Added")
# self.statusBar().showMessage("New Admin Added")
self.lineEdit_10.setText("")
self.lineEdit_11.setText("")
self.lineEdit_12.setText("")
self.lineEdit_13.setText("")
else:
self.label_22.setText("please add a valid password twice")
self.db.close()
# ---------------------
def Search_User(self):
username_inpt = self.lineEdit_17.text()
self.db = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="lms1",
)
cursor = self.db.cursor()
cursor.execute(
"SELECT * FROM login_admin WHERE username = %s", [(username_inpt)]
)
data = cursor.fetchone()
if data:
self.lineEdit_19.setText(data[1])
self.lineEdit_16.setText(data[2])
else:
self.statusBar().showMessage("No Admin Found")
self.lineEdit_19.setText("")
self.lineEdit_16.setText("")
self.db.close()
# ---------------------
def Delete_User(self):
self.db = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="lms1",
)
cursor = self.db.cursor()
username_inpt = self.lineEdit_17.text()
warning = QMessageBox.warning(
self, "Delete User", "Are you sure", QMessageBox.Yes | QMessageBox.No
)
if warning == QMessageBox.Yes:
cursor.execute(
"DELETE FROM login_admin WHERE username = %s", [(username_inpt)]
)
self.db.commit()
self.statusBar().showMessage("User Deleted")
self.lineEdit_19.setText("")
self.lineEdit_17.setText("")
self.lineEdit_16.setText("")
self.db.close()
# ---------------------
def Add_Status(self):
book_title_inpt = self.lineEdit.text()
self.db = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="lms1",
)
cursor = self.db.cursor()
cursor.execute("SELECT * FROM books WHERE book_title = %s", [(book_title_inpt)])
data = cursor.fetchone()
if not data:
warning = QMessageBox.warning(
self, "Sorry", "This book is not Available", QMessageBox.Ok
)
return
student_Name = self.lineEdit_3.text()
student_Email = self.lineEdit_14.text()
duration_days = self.comboBox_2.currentIndex() + 1
from_date = datetime.date.today()
to_date = from_date + datetime.timedelta(days=duration_days)
cursor.execute(
"""
INSERT INTO lib_status
(student_name,student_email,rent_book,rent_duration_days,from_day,to_day)
VALUES (%s , %s , %s , %s, %s , %s)
""",
(
student_Name,
student_Email,
book_title_inpt,
duration_days,
from_date,
to_date,
),
)
self.db.commit()
self.statusBar().showMessage("New Status Added")
self.lineEdit.setText("")
self.lineEdit_3.setText("")
self.lineEdit_14.setText("")
self.comboBox_2.setCurrentIndex(0)
self.db.close()
# ---------------------
def Show_All_Status(self):
self.db = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="lms1",
)
cursor = self.db.cursor()
cursor.execute(
"SELECT student_name, rent_book , from_day, to_day FROM lib_status"
)
data = cursor.fetchall()
# print(data)
self.tableWidget.setRowCount(0)
self.tableWidget.insertRow(0)
for row, form in enumerate(data):
for column, item in enumerate(form):
# print(row, column, (str(item)))
self.tableWidget.setItem(row, column, QTableWidgetItem(str(item)))
column += 1
row_position = self.tableWidget.rowCount()
self.tableWidget.insertRow(row_position)
self.db.close()
# ---------------------
def Show_All_Students(self):
self.db = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="lms1",
)
cursor = self.db.cursor()
cursor.execute(
"""SELECT student_name, student_email, rent_book ,rent_duration_days
FROM lib_status"""
)
data = cursor.fetchall()
# print(data)
self.tableWidget_4.setRowCount(0)
self.tableWidget_4.insertRow(0)
for row, form in enumerate(data):
for column, item in enumerate(form):
self.tableWidget_4.setItem(row, column, QTableWidgetItem(str(item)))
column += 1
row_position = self.tableWidget_4.rowCount()
self.tableWidget_4.insertRow(row_position)
self.db.close()
# ---------------------
def m1(self):
pass
# Utility Functions
if __name__ == "__main__":
app = QApplication(sys.argv)
login_window = LoginWindow()
if login_window.exec_() == QDialog.Accepted:
main_window = LibraryApp()
main_window.show()
sys.exit(app.exec_())