From 903a2b8dc1fe1a165e716a5a9f7299b3aaf0394f91afc9ae6c3dc515fab1ac22 Mon Sep 17 00:00:00 2001 From: linuxero Date: Sat, 14 Dec 2024 20:28:04 +0100 Subject: [PATCH] Added start blocke on no database connection available --- Gui/NoDbConnection.qml | 74 +++++++++++++++++++++++++++++++++++++++++ Gui/main.qml | 8 +++-- lib/DB/AddressDAO.py | 14 +++++--- lib/DB/BTypeDAO.py | 14 +++++--- lib/DB/BusinessDAO.py | 23 +++++++++---- lib/DB/BusinessModel.py | 2 +- lib/DB/ContactDAO.py | 16 +++++---- lib/DB/ContactModel.py | 4 +-- lib/DB/DbManager.py | 5 ++- lib/DB/UserDAO.py | 20 +++++++---- lib/DB/UserManager.py | 13 ++++---- main.py | 8 +++-- qml.qrc | 1 + 13 files changed, 161 insertions(+), 41 deletions(-) create mode 100644 Gui/NoDbConnection.qml diff --git a/Gui/NoDbConnection.qml b/Gui/NoDbConnection.qml new file mode 100644 index 0000000..1e8740f --- /dev/null +++ b/Gui/NoDbConnection.qml @@ -0,0 +1,74 @@ +import QtQuick +import QtQuick.Controls + +Rectangle +{ + anchors.fill: parent + color: "slateblue" + + Rectangle + { + id: info + anchors.horizontalCenter: parent.horizontalCenter + color: "slateblue" + implicitHeight: 55 + implicitWidth: parent.width / 4 + y: parent.height / 4 + Text + { + anchors.centerIn: parent + text: qsTr("Keine Verbindung zur Datenbank!") + color: "moccasin" + font.bold: true + font.pixelSize: 45 + } + } + + Rectangle + { + id: nostart + anchors.top: info.bottom + color: "slateblue" + anchors.horizontalCenter: parent.horizontalCenter + implicitHeight: 55 + implicitWidth: parent.width / 4 + Text + { + text: qsTr("Programm kann nicht starten..") + color: "moccasin" + anchors.centerIn: parent + font.bold: true + font.pixelSize: 45 + } + } + + Rectangle + { + anchors.top: nostart.bottom + anchors.topMargin: 25 + anchors.horizontalCenter: parent.horizontalCenter + color: "slateblue" + implicitHeight: 55 + implicitWidth: parent.width / 4 + Button + { + width: parent.width + height: 75 + Text + { + text: qsTr("Beenden") + color: "moccasin" + anchors.centerIn: parent + font.bold: true + font.pixelSize: 45 + } + anchors.centerIn: parent + background: Rectangle + { + color: "dodgerblue" + radius: 50 + } + onClicked: Qt.quit() + } + } +} diff --git a/Gui/main.qml b/Gui/main.qml index 342251c..7209fb1 100644 --- a/Gui/main.qml +++ b/Gui/main.qml @@ -23,7 +23,7 @@ ApplicationWindow leftMargin: 9 } - visible: bad_config? false: true + visible: bad_config || !db_con ? false: true } Item @@ -54,7 +54,11 @@ ApplicationWindow { importDialog.open() } - else appLoader.source= "LoginScreen.qml" + else + { + if (db_con) appLoader.source= "LoginScreen.qml" + else appLoader.source= "NoDbConnection.qml" + } } Dialog diff --git a/lib/DB/AddressDAO.py b/lib/DB/AddressDAO.py index 14e841e..0945072 100644 --- a/lib/DB/AddressDAO.py +++ b/lib/DB/AddressDAO.py @@ -4,9 +4,12 @@ import json class AddressDAO: + __cur = None def __init__(self): + #print(f"*** File: {__file__}, init()") self.__con = DbManager().getConnection() - self.__cur = self.__con.cursor() + if self.__con: + self.__cur = self.__con.cursor() def __importPlz(self): @@ -29,6 +32,9 @@ class AddressDAO: print("FINISHED")# def getAddressData(self, all = True, zipcode = None): - self.__cur.callproc("getAddress", (all, zipcode,)) - self.__data = self.__cur.fetchall() - return self.__data + if self.__cur: + self.__cur.callproc("getAddress", (all, zipcode,)) + self.__data = self.__cur.fetchall() + return self.__data + else: + return None diff --git a/lib/DB/BTypeDAO.py b/lib/DB/BTypeDAO.py index af7eda5..49dca34 100644 --- a/lib/DB/BTypeDAO.py +++ b/lib/DB/BTypeDAO.py @@ -2,11 +2,17 @@ from .DbManager import DbManager class BTypeDAO: + __cur = None def __init__(self): + #print(f"*** File: {__file__}, init()") self.__con = DbManager().getConnection() - self.__cur = self.__con.cursor() + if self.__con: + self.__cur = self.__con.cursor() def getBType(self): - self.__cur.callproc("getBtype", (None, None, )) - data = self.__cur.fetchall() - return(data) + if self.__cur: + self.__cur.callproc("getBtype", (None, None, )) + data = self.__cur.fetchall() + return(data) + else: + return None diff --git a/lib/DB/BusinessDAO.py b/lib/DB/BusinessDAO.py index 0a373a2..bf7882d 100644 --- a/lib/DB/BusinessDAO.py +++ b/lib/DB/BusinessDAO.py @@ -6,21 +6,30 @@ from PySide6.QtCore import QObject, Signal class BusinessDAO(QObject): newBusinessAdded = Signal() + + __cur = None + def __init__(self): + #print(f"*** File: {__file__}, init()") super().__init__() self.__con = DbManager().getConnection() - self.__cur = self.__con.cursor() + if self.__con: + self.__cur = self.__con.cursor() def getBusiness(self, enc_key, criterion = "Alle"): - self.__cur.callproc("getCustomerView", (enc_key, criterion,)) - self.__all_cols = [desc[0] for desc in self.__cur.description] - return self.__cur.fetchall(), self.__all_cols + if self.__cur: + self.__cur.callproc("getCustomerView", (enc_key, criterion,)) + self.__all_cols = [desc[0] for desc in self.__cur.description] + return self.__cur.fetchall(), self.__all_cols + else: + return None, None def addBusiness(self, data, contact_id): try: - self.__cur.callproc("addBusiness", (json.dumps(data), contact_id)) - self.__con.commit() - self.newBusinessAdded.emit() + if self.__cur: + self.__cur.callproc("addBusiness", (json.dumps(data), contact_id)) + self.__con.commit() + self.newBusinessAdded.emit() except mariadb.Error as e: print(str(e)) diff --git a/lib/DB/BusinessModel.py b/lib/DB/BusinessModel.py index 73dc789..120fd70 100644 --- a/lib/DB/BusinessModel.py +++ b/lib/DB/BusinessModel.py @@ -113,7 +113,7 @@ class BusinessModel(QAbstractTableModel): @Slot(int) def onRowClicked(self, row): - print(row) + print(f"Selected table row: {row}, corresponding DB ID: {self.__data[row][0]}") @Slot(str) def viewCriterion(self, criterion): diff --git a/lib/DB/ContactDAO.py b/lib/DB/ContactDAO.py index 918fa7e..7098ba7 100644 --- a/lib/DB/ContactDAO.py +++ b/lib/DB/ContactDAO.py @@ -7,18 +7,22 @@ class ContactDAO: def __init__(self): #print(f"*** File: {__file__}, __init__()") self.__con = DbManager().getConnection() - self.__cur = self.__con.cursor() + if self.__con: + self.__cur = self.__con.cursor() def getContacts(self): print(f"*** File: {__file__}, getContacts()") def addContact(self, contact, enc_key): try: - self.__cur.callproc("addContactPerson", (enc_key, json.dumps(contact),)) - self.__con.commit() - self.__cur.callproc("getLastInsertId") - contact_id = self.__cur.fetchone() - return contact_id[0] + if self.__cur: + self.__cur.callproc("addContactPerson", (enc_key, json.dumps(contact),)) + self.__con.commit() + self.__cur.callproc("getLastInsertId") + contact_id = self.__cur.fetchone() + return contact_id[0] + else: + return None except mariadb.Error as e: print("MDB: " + str(e)) except Exception as e: diff --git a/lib/DB/ContactModel.py b/lib/DB/ContactModel.py index 02951da..0cc833c 100644 --- a/lib/DB/ContactModel.py +++ b/lib/DB/ContactModel.py @@ -7,10 +7,10 @@ class ContactModel(QObject): contactIdReady = Signal(int) def __init__(self): super().__init__() + # print(f"*** File: {__file__}, __init__()") + #self.logger = logging.getLogger() self.__conf = ConfigLoader().getConfig() self.__key = self.__conf['pyqcrm']['ENCRYPTION_KEY'] - #self.logger = logging.getLogger() - # print(f"*** File: {__file__}, __init__()") self.__data = self.__getData() def getContacts(self): diff --git a/lib/DB/DbManager.py b/lib/DB/DbManager.py index 8ebd99f..ca67c4d 100644 --- a/lib/DB/DbManager.py +++ b/lib/DB/DbManager.py @@ -16,14 +16,17 @@ class DbManager(object): def getConnection(cls): + #print(f"DB Manager: {cls.__dbmanager}") + #print(f"DB Connection: {cls.__connection}") try: if not cls.__connection or not cls.__connection.ping(): + cls.__failure_notified = False cls.__connection = mariadb.connect(**cls.__con_param) except mariadb.InterfaceError as e: cls.__connection = mariadb.connect(**cls.__con_param) print(f"DbManager Connection (INTERFACE ERROR): {e}..reconnecting...") except mariadb.Error as e: - print(f"Connection parameters are wrong: {e}") + print(f"File: {__file__}\n Database connection error: {e}") cls.__connection = None return cls.__connection diff --git a/lib/DB/UserDAO.py b/lib/DB/UserDAO.py index 758de6f..0ac6eb0 100644 --- a/lib/DB/UserDAO.py +++ b/lib/DB/UserDAO.py @@ -4,15 +4,20 @@ from ..PyqcrmFlags import PyqcrmFlags import mariadb class UserDAO: + __cursor = None def __init__(self): + #print(f"*** File: {__file__}, init()") self.__con = DbManager().getConnection() - self.__cur = self.__con.cursor() + if self.__con: + self.__cur = self.__con.cursor() def createUser(self, username, password, info, role= PyqcrmFlags.USER): - user_created = True + user_created = False try: - self.__cur.callproc("createUser", (username, password, info, role)) - self.__con.commit() + if self.__cur: + self.__cur.callproc("createUser", (username, password, info, role)) + self.__con.commit() + user_created = True except mariadb.Error as e: print(f"Error: {e}") print(e.errno) @@ -21,8 +26,11 @@ class UserDAO: return user_created def getUser(self, username): - self.__cur.callproc("getUser", (username,)) - return self.__cur.fetchone() + if self.__cur: + self.__cur.callproc("getUser", (username,)) + return self.__cur.fetchone() + else: + return None diff --git a/lib/DB/UserManager.py b/lib/DB/UserManager.py index 285f914..b7fa8f5 100644 --- a/lib/DB/UserManager.py +++ b/lib/DB/UserManager.py @@ -13,13 +13,14 @@ class UserManager(QObject): def __init__(self, user_config = None, role = None): super().__init__() self.__con = DbManager().getConnection() - self.__cur = self.__con.cursor() - if user_config and role: + if self.__con: + self.__cur = self.__con.cursor() + if user_config and role: - self.__username = user_config["PYQCRM_USER"] - self.__password = user_config["PYQCRM_USER_PASS"] - self.__info = user_config["PYQCRM_USER_INFO"] - self.__role = role if role == PyqcrmFlags.ADMIN else 0 + self.__username = user_config["PYQCRM_USER"] + self.__password = user_config["PYQCRM_USER_PASS"] + self.__info = user_config["PYQCRM_USER_INFO"] + self.__role = role if role == PyqcrmFlags.ADMIN else 0 def createUser(self): diff --git a/main.py b/main.py index a93d54b..6dbb351 100644 --- a/main.py +++ b/main.py @@ -28,6 +28,7 @@ from lib.DB.ContactModel import ContactModel # type="" bad_config = False +db_con = False address_model = None business_model = None business_type = None @@ -35,14 +36,14 @@ contact_model = None user = None def initializeProgram(): - # print(f"In {__file__} file, initializeProgram()") + #print(f"In {__file__} file, initializeProgram()") global address_model, bad_config, business_model, user, business_type, contact_model if not bad_config: dbconf = config.getConfig()['database'] DbManager(dbconf) bad_config = False - business_model = BusinessModel() user = UserManager() + business_model = BusinessModel() address_model = AddressModel() business_type = BTypeModel() contact_model = ContactModel() @@ -78,9 +79,12 @@ if __name__ == "__main__": config.configurationReady.connect(initializeProgram) else: initializeProgram() + if DbManager().getConnection(): + db_con = True engine.rootContext().setContextProperty("bad_config", bad_config) # print(f"Fehler: {i}") + engine.rootContext().setContextProperty("db_con", db_con) engine.rootContext().setContextProperty("config", config) engine.load(qml_file) diff --git a/qml.qrc b/qml.qrc index 9448bdf..2cbbe58 100644 --- a/qml.qrc +++ b/qml.qrc @@ -15,5 +15,6 @@ Gui/SearchBar.qml js/qmldict.js Gui/CustomerView.qml + Gui/NoDbConnection.qml