add admin dialog:
from view.admin.dialogs.form_modal import (
FormModal
)
from view.shared.components.inputs import (
TextInput,
PasswordInput,
Dropdown
)
class AddAdminDialog(FormModal):
def __init__(self):
super().__init__(
title="Add Admin"
)
self.first_name_input = TextInput(
"First Name"
)
self.last_name_input = TextInput(
"Last Name"
)
self.email_input = TextInput(
"Email"
)
self.password_input = PasswordInput(
"Password"
)
self.department_input = TextInput(
"Department"
)
self.access_level_combo = Dropdown([
"Basic",
"Advanced",
"Super Admin"
])
self.add_field(
self.first_name_input
)
self.add_field(
self.last_name_input
)
self.add_field(
self.email_input
)
self.add_field(
self.password_input
)
self.add_field(
self.department_input
)
self.add_field(
self.access_level_combo
)
def validate(self):
return (
self.require(
self.first_name_input.text(),
"First name"
)
or self.require(
self.last_name_input.text(),
"Last name"
)
or self.require_email(
self.email_input.text()
)
or self.require_password(
self.password_input.text()
)
)
def accept(self):
self.user_data = {
"first_name":
self.first_name_input.text(),
"last_name":
self.last_name_input.text(),
"email":
self.email_input.text(),
"password":
self.password_input.text(),
"role":
"Admin",
"department":
self.department_input.text(),
"access_level":
self.access_level_combo.currentText()
}
super().accept()
add classroom dialog:
from view.admin.dialogs.form_modal import (
FormModal
)
from view.shared.components.inputs import (
TextInput,
Dropdown,
NumberInput
)
class AddClassroomDialog(FormModal):
def __init__(self):
super().__init__(
title="Add Classroom"
)
self.name_input = TextInput(
"Classroom Name"
)
self.teacher_combo = Dropdown([
"John Doe",
"Jane Doe",
"Mike Smith"
])
self.capacity_input = NumberInput(
minimum=1,
maximum=100
)
self.add_field(
self.name_input
)
self.add_field(
self.teacher_combo
)
self.add_field(
self.capacity_input
)
def validate(self):
return self.require(
self.name_input.text(),
"Classroom name"
)
def accept(self):
self.classroom_data = {
"name": self.name_input.text(),
"teacher": self.teacher_combo.currentText(),
"students": str(
self.capacity_input.value()
)
}
super().accept()
add student:
from view.admin.dialogs.form_modal import (
FormModal
)
from view.shared.components.inputs import (
TextInput,
PasswordInput
)
class AddStudentDialog(FormModal):
def __init__(self):
super().__init__(
title="Add Student"
)
self.first_name_input = TextInput(
"First Name"
)
self.last_name_input = TextInput(
"Last Name"
)
self.email_input = TextInput(
"Email"
)
self.password_input = PasswordInput(
"Password"
)
self.father_initial_input = TextInput(
"Father Initial"
)
self.add_field(
self.first_name_input
)
self.add_field(
self.last_name_input
)
self.add_field(
self.email_input
)
self.add_field(
self.password_input
)
self.add_field(
self.father_initial_input
)
def validate(self):
return (
self.require(
self.first_name_input.text(),
"First name"
)
or self.require(
self.last_name_input.text(),
"Last name"
)
or self.require_email(
self.email_input.text()
)
or self.require_password(
self.password_input.text()
)
)
def accept(self):
self.user_data = {
"first_name":
self.first_name_input.text(),
"last_name":
self.last_name_input.text(),
"email":
self.email_input.text(),
"password":
self.password_input.text(),
"role":
"Student",
"father_initial":
self.father_initial_input.text()
}
super().accept()
add teacher dialog:
from view.admin.dialogs.form_modal import (
FormModal
)
from view.shared.components.inputs import (
TextInput,
PasswordInput,
Dropdown,
DatePicker
)
class AddTeacherDialog(FormModal):
def __init__(self):
super().__init__(
title="Add Teacher"
)
self.first_name_input = TextInput(
"First Name"
)
self.last_name_input = TextInput(
"Last Name"
)
self.email_input = TextInput(
"Email"
)
self.password_input = PasswordInput(
"Password"
)
self.specialization_combo = Dropdown([
"Mathematics",
"Physics",
"Chemistry",
"Biology",
"History",
"Geography",
"Computer Science"
])
self.hire_date_input = DatePicker()
self.add_field(
self.first_name_input
)
self.add_field(
self.last_name_input
)
self.add_field(
self.email_input
)
self.add_field(
self.password_input
)
self.add_field(
self.specialization_combo
)
self.add_field(
self.hire_date_input
)
def validate(self):
return (
self.require(
self.first_name_input.text(),
"First name"
)
or self.require(
self.last_name_input.text(),
"Last name"
)
or self.require_email(
self.email_input.text()
)
or self.require_password(
self.password_input.text()
)
)
def accept(self):
self.user_data = {
"first_name":
self.first_name_input.text(),
"last_name":
self.last_name_input.text(),
"email":
self.email_input.text(),
"password":
self.password_input.text(),
"role":
"Teacher",
"specialization":
self.specialization_combo.currentText(),
"hire_date":
self.hire_date_input.date().toString(
"yyyy-MM-dd"
)
}
super().accept()
edit admin:
from view.admin.dialogs.form_modal import (
FormModal
)
from view.shared.components.inputs import (
TextInput,
Dropdown
)
class EditAdminDialog(FormModal):
def __init__(
self,
user_data
):
super().__init__(
title="Edit Admin",
confirm_text="Save Changes"
)
self.first_name_input = TextInput(
"First Name"
)
self.first_name_input.setText(
user_data["first_name"]
)
self.last_name_input = TextInput(
"Last Name"
)
self.last_name_input.setText(
user_data["last_name"]
)
self.department_input = TextInput(
"Department"
)
self.department_input.setText(
user_data.get(
"department",
""
)
)
self.access_level_combo = Dropdown([
"Basic",
"Advanced",
"Super Admin"
])
self.access_level_combo.setCurrentText(
user_data.get(
"access_level",
""
)
)
self.add_field(
self.first_name_input
)
self.add_field(
self.last_name_input
)
self.add_field(
self.department_input
)
self.add_field(
self.access_level_combo
)
def validate(self):
return (
self.require(
self.first_name_input.text(),
"First name"
)
or self.require(
self.last_name_input.text(),
"Last name"
)
)
def accept(self):
self.user_data = {
"first_name":
self.first_name_input.text(),
"last_name":
self.last_name_input.text(),
"department":
self.department_input.text(),
"access_level":
self.access_level_combo.currentText()
}
super().accept()
edit classroom:
from view.admin.dialogs.form_modal import (
FormModal
)
from view.shared.components.inputs import (
TextInput,
Dropdown,
NumberInput
)
class EditClassroomDialog(FormModal):
def __init__(
self,
classroom_name,
teacher,
students
):
super().__init__(
title="Edit Classroom",
confirm_text="Save Changes"
)
self.name_input = TextInput(
"Classroom Name"
)
self.name_input.setText(
classroom_name
)
self.teacher_combo = Dropdown([
"John Doe",
"Jane Doe",
"Mike Smith",
"Alex Green"
])
self.teacher_combo.setCurrentText(
teacher
)
self.students_input = NumberInput(
minimum=1,
maximum=100
)
self.students_input.setValue(
int(students)
)
self.add_field(
self.name_input
)
self.add_field(
self.teacher_combo
)
self.add_field(
self.students_input
)
def validate(self):
return self.require(
self.name_input.text(),
"Classroom name"
)
def accept(self):
self.classroom_data = {
"name": self.name_input.text(),
"teacher": self.teacher_combo.currentText(),
"students": str(
self.students_input.value()
)
}
super().accept()
edit student:
from view.admin.dialogs.form_modal import (
FormModal
)
from view.shared.components.inputs import (
TextInput
)
class EditStudentDialog(FormModal):
def __init__(
self,
user_data
):
super().__init__(
title="Edit Student",
confirm_text="Save Changes"
)
self.first_name_input = TextInput(
"First Name"
)
self.first_name_input.setText(
user_data["first_name"]
)
self.last_name_input = TextInput(
"Last Name"
)
self.last_name_input.setText(
user_data["last_name"]
)
self.father_initial_input = TextInput(
"Father Initial"
)
self.father_initial_input.setText(
user_data.get(
"father_initial",
""
)
)
self.add_field(
self.first_name_input
)
self.add_field(
self.last_name_input
)
self.add_field(
self.father_initial_input
)
def validate(self):
return (
self.require(
self.first_name_input.text(),
"First name"
)
or self.require(
self.last_name_input.text(),
"Last name"
)
)
def accept(self):
self.user_data = {
"first_name":
self.first_name_input.text(),
"last_name":
self.last_name_input.text(),
"father_initial":
self.father_initial_input.text()
}
super().accept()
edit teacher:
from PySide6.QtCore import QDate
from view.admin.dialogs.form_modal import (
FormModal
)
from view.shared.components.inputs import (
TextInput,
Dropdown,
DatePicker
)
class EditTeacherDialog(FormModal):
def __init__(
self,
user_data
):
super().__init__(
title="Edit Teacher",
confirm_text="Save Changes"
)
self.first_name_input = TextInput(
"First Name"
)
self.first_name_input.setText(
user_data["first_name"]
)
self.last_name_input = TextInput(
"Last Name"
)
self.last_name_input.setText(
user_data["last_name"]
)
self.specialization_combo = Dropdown([
"Mathematics",
"Physics",
"Chemistry",
"Biology",
"History",
"Geography",
"Computer Science"
])
self.specialization_combo.setCurrentText(
user_data.get(
"specialization",
""
)
)
self.hire_date_input = DatePicker()
hire_date = user_data.get(
"hire_date",
"2025-01-01"
)
self.hire_date_input.setDate(
QDate.fromString(
hire_date,
"yyyy-MM-dd"
)
)
self.add_field(
self.first_name_input
)
self.add_field(
self.last_name_input
)
self.add_field(
self.specialization_combo
)
self.add_field(
self.hire_date_input
)
def validate(self):
return (
self.require(
self.first_name_input.text(),
"First name"
)
or self.require(
self.last_name_input.text(),
"Last name"
)
)
def accept(self):
self.user_data = {
"first_name":
self.first_name_input.text(),
"last_name":
self.last_name_input.text(),
"specialization":
self.specialization_combo.currentText(),
"hire_date":
self.hire_date_input.date().toString(
"yyyy-MM-dd"
)
}
super().accept()
Form modal:
import re
from PySide6.QtWidgets import (
QLabel,
QApplication
)
from view.shared.components.modal import (
Modal
)
EMAIL_PATTERN = re.compile(
r"^[^@\s]+@[^@\s]+\.[^@\s]+$"
)
class FormModal(Modal):
def __init__(
self,
title="",
confirm_text="Save",
cancel_text="Cancel",
parent=None,
):
super().__init__(
title=title,
message="",
confirm_text=confirm_text,
cancel_text=cancel_text,
parent=parent,
)
# Dialogul e fereastra separata: daca
# tema nu e aplicata pe QApplication
# (ci pe o fereastra/pagina), nu ar
# mosteni niciun stil si ar aparea
# nestilizat. Il preluam explicit.
self._inherit_stylesheet(parent)
# Formularele nu au mesaj descriptiv.
self.message_label.hide()
# Eticheta de eroare, sub titlu.
self.error_label = QLabel()
self.error_label.setWordWrap(True)
self.error_label.setStyleSheet(
"background-color: #fee2e2;"
"color: #991b1b;"
"border-radius: 8px;"
"padding: 8px 12px;"
"font-size: 12px;"
"font-weight: 600;"
)
self.error_label.hide()
self.container.layout().insertWidget(
1,
self.error_label
)
# Campurile intra dupa titlu (0),
# eroare (1) si mesaj (2), inaintea
# stretch-ului.
self._field_index = 3
def _inherit_stylesheet(self, parent):
if self.styleSheet():
return
stylesheet = ""
application = QApplication.instance()
if application:
stylesheet = application.styleSheet()
if not stylesheet and parent is not None:
stylesheet = (
parent.window().styleSheet()
)
if not stylesheet:
# Ultima varianta: orice fereastra
# de nivel superior care are tema.
for window in (
QApplication.topLevelWidgets()
):
if window.styleSheet():
stylesheet = window.styleSheet()
break
if stylesheet:
self.setStyleSheet(stylesheet)
def add_field(self, widget):
layout = self.container.layout()
layout.insertWidget(
self._field_index,
widget
)
self._field_index += 1
#
# Validare
#
def validate(self):
"""
Suprascrisa de dialoguri. Intoarce
un mesaj de eroare sau None.
"""
return None
def set_error(self, message):
if message:
self.error_label.setText(message)
self.error_label.show()
else:
self.error_label.hide()
def accept(self):
error = self.validate()
if error:
self.set_error(error)
return
self.set_error(None)
super().accept()
#
# Reguli reutilizabile (aceleasi pe
# care le va impune si serverul)
#
@staticmethod
def require(value, field_name):
if not value.strip():
return f"{field_name} is required."
return None
@staticmethod
def require_email(value):
if not value.strip():
return "Email is required."
if not EMAIL_PATTERN.match(
value.strip()
):
return (
"Enter a valid email address."
)
return None
@staticmethod
def require_password(value):
if not value:
return "Password is required."
if len(value) < 8:
return (
"Password must be at least "
"8 characters long."
)
return None