-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathissue_books.py
More file actions
96 lines (87 loc) · 4.26 KB
/
Copy pathissue_books.py
File metadata and controls
96 lines (87 loc) · 4.26 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
import sys
import dateutil.parser as dparser
from PySide6.QtCore import QDate
from PySide6.QtWidgets import QApplication, QLineEdit, QMainWindow, QMessageBox
from sqlalchemy.exc import IntegrityError
from sqlalchemy import select, insert
from backend import t_issues, session, Students, Book
import issue_ui
class Issue(QMainWindow, issue_ui.Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.issue_date.setDate(QDate.currentDate())
self.ret_date.setDate(QDate.currentDate().addDays(6))
self.issue_book.clicked.connect(self.issue)
def issue(self):
student_id = int(self.stu_no.text())
book_id = self.book_no.text()
# Validate Student
statement = select(Students).filter_by(Admno=student_id)
result = session.execute(statement).scalars().all()
if not result:
error = QMessageBox()
error.setStandardButtons(QMessageBox.StandardButton.Ok)
error.setWindowTitle("Student Not Found")
error.setText(f"The Student with Admission Number {student_id} is not present in the school")
error.setIcon(QMessageBox.Icon.Critical)
error = error.exec()
if error == QMessageBox.StandardButton.Ok:
for widget in self.findChildren(QLineEdit):
widget.clear()
else:
# Validate Book
statement = select(Book).filter_by(Acc_no=book_id)
result = session.execute(statement).scalars().all()
if not result:
error = QMessageBox()
error.setStandardButtons(QMessageBox.StandardButton.Ok)
error.setWindowTitle("Book Not Found")
error.setText(f"The book with book number {book_id} is not present in the library")
error.setIcon(QMessageBox.Icon.Critical)
error = error.exec()
if error == QMessageBox.StandardButton.Ok:
for widget in self.findChildren(QLineEdit):
widget.clear()
else:
# Validate If Issued
statement = select(t_issues).filter_by(book_no=book_id)
result = session.execute(statement).scalars().all()
if result:
error = QMessageBox()
error.setStandardButtons(QMessageBox.StandardButton.Ok)
error.setWindowTitle("Book Not Found")
error.setText(f"The book with book number {book_id} is already issued")
error.setIcon(QMessageBox.Icon.Critical)
error = error.exec()
if error == QMessageBox.StandardButton.Ok:
for widget in self.findChildren(QLineEdit):
widget.clear()
else:
iss_date = dparser.parse(
self.issue_date.text(), fuzzy=True, ignoretz=True).date()
return_date = dparser.parse(
self.issue_date.text(), fuzzy=True, ignoretz=True).date()
self.submit(book_id, student_id, iss_date, return_date)
def submit(self, book_id, student_id, iss_date, return_date):
try:
stmt = insert(t_issues).values(
book_no=book_id, student_id=student_id, issue_date=iss_date, return_date=return_date)
session.execute(stmt)
session.commit()
success = QMessageBox()
success.setText("Book Issued")
success.setWindowTitle("Successful")
success.setIcon(QMessageBox.Icon.Information)
success.setStandardButtons(QMessageBox.StandardButton.Ok)
success = success.exec()
if success == QMessageBox.StandardButton.Ok:
for widget in self.findChildren(QLineEdit):
widget.clear()
except IntegrityError:
session.rollback()
if __name__ == "__main__":
app = QApplication(sys.argv)
win = Issue()
win.show()
sys.exit(app.exec())