[main] Outline for error handling of member creation

This commit is contained in:
Thastertyn 2025-01-14 20:16:18 +01:00
parent 84df581763
commit 3fa44ccaf3
5 changed files with 32 additions and 7 deletions

View File

@ -1,9 +1,12 @@
from sqlalchemy.exc import IntegrityError
from sqlalchemy.exc import IntegrityError, SQLAlchemyError
from models.member import Member
from utils.database import DatabaseManager
from utils.errors.database.database_error import DatabaseError
from utils.errors.database.duplicate_entry_error import DuplicateEntryError
def create_new_member(new_member: Member):
try:
with DatabaseManager.get_session() as session:
@ -12,3 +15,9 @@ def create_new_member(new_member: Member):
except IntegrityError as e:
print(e)
session.rollback()
raise DuplicateEntryError from e
except SQLAlchemyError as e:
print(e)
session.rollback()
raise DatabaseError from e

View File

@ -11,7 +11,7 @@ from utils.database import DatabaseManager
from ui.member_editor.member_editor import MemberEditor
class MemberDashboard(QWidget):
class MemberList(QWidget):
def __init__(self):
super().__init__()

View File

@ -1,17 +1,23 @@
import logging
from PySide6.QtGui import QGuiApplication, QAction
from PySide6.QtQml import QQmlApplicationEngine
from PySide6 import QtWidgets, QtCore
from PySide6.QtWidgets import QVBoxLayout, QFormLayout, QLineEdit, QHBoxLayout, QPushButton, QDialog
from PySide6.QtWidgets import QVBoxLayout, QFormLayout, QLineEdit, QHBoxLayout, QPushButton, QDialog, QMessageBox
from models.member import Member
from database.member import create_new_member
from utils.errors.database.database_error import DatabaseError
from utils.errors.database.duplicate_entry_error import DuplicateEntryError
class MemberEditor(QDialog):
def __init__(self, member: Member = None):
super().__init__()
self.logger = logging.getLogger(__name__)
self.create_layout()
if member:
@ -75,7 +81,11 @@ class MemberEditor(QDialog):
self.member.email = self.email_input.text()
self.member.phone = self.phone_number_input.text()
if self.create_new:
create_new_member(self.member)
try:
if self.create_new:
self.logger.debug("Creating new member")
create_new_member(self.member)
except DuplicateEntryError:
QMessageBox.critical(None, "Details already in use", "Cannot create a new user", QMessageBox.StandardButton.Ok, QMessageBox.StandardButtons.NoButton)
self.accept()

View File

@ -5,7 +5,7 @@ from PySide6.QtWidgets import QMessageBox, QFileDialog
from PySide6.QtCore import QStandardPaths
from ui.dashboard.dashboard import LibraryDashboard
from ui.main_window_tabs.member_list.member_list import MemberDashboard
from ui.main_window_tabs.member_list.member_list import MemberList
from ui.book_editor.book_editor import BookEditor
from ui.member_editor.member_editor import MemberEditor
@ -40,7 +40,7 @@ class LibraryWindow(QtWidgets.QMainWindow):
self.dashboard = LibraryDashboard()
self.member_list = MemberDashboard()
self.member_list = MemberList()
central_widget.addTab(self.dashboard, "Dashboard")
central_widget.addTab(self.member_list, "Members")

View File

@ -0,0 +1,6 @@
from .database_error import DatabaseError
class DuplicateEntryError(DatabaseError):
def __init__(self, message: str):
super().__init__(message)
self.message = message