Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
3.9 kB
4
Indexable
Never
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QVBoxLayout, QTableWidget, \
    QTableWidgetItem, QComboBox, QFormLayout, QInputDialog, QMessageBox
from sqlalchemy import create_engine, Column, Integer, String, Enum
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

Base = declarative_base()

class PaymentStatus(Enum):
    PAID = 'Paid'
    PAY_DOWN_PAYMENT = 'Pay Down Payment'
    UNPAID = 'Unpaid'

class SpecificClass(Enum):
    A1 = 'A1'
    A2 = 'A2'
    B1 = 'B1'
    B2 = 'B2'

class StudentStatus(Enum):
    JOINED_GROUP = 'Joined Group'
    NOT_JOINED_YET = "Haven't Joined Yet"

class Student(Base):
    __tablename__ = 'students'
    id = Column(Integer, primary_key=True)
    name = Column(String, nullable=False)
    class_type = Column(String, nullable=True)
    payment_status = Column(Enum(PaymentStatus), nullable=True)
    specific_class = Column(Enum(SpecificClass), nullable=True)
    phone_number = Column(String, nullable=True)
    student_status = Column(Enum(StudentStatus), nullable=True)

class StudentManager(QWidget):
    def __init__(self):
        super().__init__()

        self.init_db()
        self.init_ui()

    def init_db(self):
        engine = create_engine('sqlite:///students.db', echo=False)
        Base.metadata.create_all(engine)  # Create tables
        Session = sessionmaker(bind=engine, autoflush=False)
        self.session = Session()

    def init_ui(self):
        # ... (Your existing UI code)

    def load_students(self):
        students = self.session.query(Student).all()

        self.student_table.setRowCount(len(students))
        for row, student in enumerate(students):
            self.student_table.setItem(row, 0, QTableWidgetItem(str(student.id)))
            self.student_table.setItem(row, 1, QTableWidgetItem(student.name))
            self.student_table.setItem(row, 2, QTableWidgetItem(student.class_type))
            self.student_table.setItem(row, 3, QTableWidgetItem(str(student.payment_status)))
            self.student_table.setItem(row, 4, QTableWidgetItem(str(student.specific_class)))
            self.student_table.setItem(row, 5, QTableWidgetItem(student.phone_number))
            self.student_table.setItem(row, 6, QTableWidgetItem(str(student.student_status)))
            edit_button = QPushButton('Edit')
            edit_button.clicked.connect(lambda _, student=student: self.edit_student(student))
            self.student_table.setCellWidget(row, 7, edit_button)

    def add_student(self):
        student_name = self.name_entry.text()
        class_type = self.class_combobox.currentText()
        payment_status = self.payment_status_combobox.currentText()
        specific_class = self.specific_class_combobox.currentText()
        phone_number = self.phone_number_entry.text()
        student_status = self.student_status_combobox.currentText()

        if student_name:
            new_student = Student(
                name=student_name,
                class_type=class_type,
                payment_status=payment_status,
                specific_class=specific_class,
                phone_number=phone_number,
                student_status=student_status
            )
            self.session.add(new_student)
            self.session.commit()

            self.name_entry.clear()
            self.phone_number_entry.clear()
            self.load_students()
            QMessageBox.information(self, 'Success', f'Student {student_name} added!')
        else:
            QMessageBox.critical(self, 'Error', 'Please enter a student name.')

    def edit_student(self, student):
        # ... (Your existing edit_student method)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = StudentManager()
    window.show()
    sys.exit(app.exec_())
Leave a Comment