[main] Outline for error handling of member creation
This commit is contained in:
parent
84df581763
commit
3fa44ccaf3
@ -1,9 +1,12 @@
|
|||||||
from sqlalchemy.exc import IntegrityError
|
from sqlalchemy.exc import IntegrityError, SQLAlchemyError
|
||||||
|
|
||||||
from models.member import Member
|
from models.member import Member
|
||||||
|
|
||||||
from utils.database import DatabaseManager
|
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):
|
def create_new_member(new_member: Member):
|
||||||
try:
|
try:
|
||||||
with DatabaseManager.get_session() as session:
|
with DatabaseManager.get_session() as session:
|
||||||
@ -12,3 +15,9 @@ def create_new_member(new_member: Member):
|
|||||||
except IntegrityError as e:
|
except IntegrityError as e:
|
||||||
print(e)
|
print(e)
|
||||||
session.rollback()
|
session.rollback()
|
||||||
|
raise DuplicateEntryError from e
|
||||||
|
except SQLAlchemyError as e:
|
||||||
|
print(e)
|
||||||
|
session.rollback()
|
||||||
|
raise DatabaseError from e
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ from utils.database import DatabaseManager
|
|||||||
from ui.member_editor.member_editor import MemberEditor
|
from ui.member_editor.member_editor import MemberEditor
|
||||||
|
|
||||||
|
|
||||||
class MemberDashboard(QWidget):
|
class MemberList(QWidget):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
|
@ -1,17 +1,23 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
from PySide6.QtGui import QGuiApplication, QAction
|
from PySide6.QtGui import QGuiApplication, QAction
|
||||||
from PySide6.QtQml import QQmlApplicationEngine
|
from PySide6.QtQml import QQmlApplicationEngine
|
||||||
from PySide6 import QtWidgets, QtCore
|
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 models.member import Member
|
||||||
|
|
||||||
from database.member import create_new_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):
|
class MemberEditor(QDialog):
|
||||||
def __init__(self, member: Member = None):
|
def __init__(self, member: Member = None):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
|
self.logger = logging.getLogger(__name__)
|
||||||
self.create_layout()
|
self.create_layout()
|
||||||
|
|
||||||
if member:
|
if member:
|
||||||
@ -75,7 +81,11 @@ class MemberEditor(QDialog):
|
|||||||
self.member.email = self.email_input.text()
|
self.member.email = self.email_input.text()
|
||||||
self.member.phone = self.phone_number_input.text()
|
self.member.phone = self.phone_number_input.text()
|
||||||
|
|
||||||
if self.create_new:
|
try:
|
||||||
create_new_member(self.member)
|
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()
|
self.accept()
|
@ -5,7 +5,7 @@ from PySide6.QtWidgets import QMessageBox, QFileDialog
|
|||||||
from PySide6.QtCore import QStandardPaths
|
from PySide6.QtCore import QStandardPaths
|
||||||
|
|
||||||
from ui.dashboard.dashboard import LibraryDashboard
|
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.book_editor.book_editor import BookEditor
|
||||||
from ui.member_editor.member_editor import MemberEditor
|
from ui.member_editor.member_editor import MemberEditor
|
||||||
|
|
||||||
@ -40,7 +40,7 @@ class LibraryWindow(QtWidgets.QMainWindow):
|
|||||||
|
|
||||||
|
|
||||||
self.dashboard = LibraryDashboard()
|
self.dashboard = LibraryDashboard()
|
||||||
self.member_list = MemberDashboard()
|
self.member_list = MemberList()
|
||||||
central_widget.addTab(self.dashboard, "Dashboard")
|
central_widget.addTab(self.dashboard, "Dashboard")
|
||||||
central_widget.addTab(self.member_list, "Members")
|
central_widget.addTab(self.member_list, "Members")
|
||||||
|
|
||||||
|
6
src/utils/errors/database/duplicate_entry_error.py
Normal file
6
src/utils/errors/database/duplicate_entry_error.py
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
from .database_error import DatabaseError
|
||||||
|
|
||||||
|
class DuplicateEntryError(DatabaseError):
|
||||||
|
def __init__(self, message: str):
|
||||||
|
super().__init__(message)
|
||||||
|
self.message = message
|
Loading…
x
Reference in New Issue
Block a user