Added start blocke on no database connection available

This commit is contained in:
2024-12-14 20:28:04 +01:00
parent 559ad1b882
commit 903a2b8dc1
13 changed files with 161 additions and 41 deletions

74
Gui/NoDbConnection.qml Normal file
View File

@@ -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()
}
}
}

View File

@@ -23,7 +23,7 @@ ApplicationWindow
leftMargin: 9 leftMargin: 9
} }
visible: bad_config? false: true visible: bad_config || !db_con ? false: true
} }
Item Item
@@ -54,7 +54,11 @@ ApplicationWindow
{ {
importDialog.open() importDialog.open()
} }
else appLoader.source= "LoginScreen.qml" else
{
if (db_con) appLoader.source= "LoginScreen.qml"
else appLoader.source= "NoDbConnection.qml"
}
} }
Dialog Dialog

View File

@@ -4,9 +4,12 @@ import json
class AddressDAO: class AddressDAO:
__cur = None
def __init__(self): def __init__(self):
#print(f"*** File: {__file__}, init()")
self.__con = DbManager().getConnection() self.__con = DbManager().getConnection()
self.__cur = self.__con.cursor() if self.__con:
self.__cur = self.__con.cursor()
def __importPlz(self): def __importPlz(self):
@@ -29,6 +32,9 @@ class AddressDAO:
print("FINISHED")# print("FINISHED")#
def getAddressData(self, all = True, zipcode = None): def getAddressData(self, all = True, zipcode = None):
self.__cur.callproc("getAddress", (all, zipcode,)) if self.__cur:
self.__data = self.__cur.fetchall() self.__cur.callproc("getAddress", (all, zipcode,))
return self.__data self.__data = self.__cur.fetchall()
return self.__data
else:
return None

View File

@@ -2,11 +2,17 @@ from .DbManager import DbManager
class BTypeDAO: class BTypeDAO:
__cur = None
def __init__(self): def __init__(self):
#print(f"*** File: {__file__}, init()")
self.__con = DbManager().getConnection() self.__con = DbManager().getConnection()
self.__cur = self.__con.cursor() if self.__con:
self.__cur = self.__con.cursor()
def getBType(self): def getBType(self):
self.__cur.callproc("getBtype", (None, None, )) if self.__cur:
data = self.__cur.fetchall() self.__cur.callproc("getBtype", (None, None, ))
return(data) data = self.__cur.fetchall()
return(data)
else:
return None

View File

@@ -6,21 +6,30 @@ from PySide6.QtCore import QObject, Signal
class BusinessDAO(QObject): class BusinessDAO(QObject):
newBusinessAdded = Signal() newBusinessAdded = Signal()
__cur = None
def __init__(self): def __init__(self):
#print(f"*** File: {__file__}, init()")
super().__init__() super().__init__()
self.__con = DbManager().getConnection() self.__con = DbManager().getConnection()
self.__cur = self.__con.cursor() if self.__con:
self.__cur = self.__con.cursor()
def getBusiness(self, enc_key, criterion = "Alle"): def getBusiness(self, enc_key, criterion = "Alle"):
self.__cur.callproc("getCustomerView", (enc_key, criterion,)) if self.__cur:
self.__all_cols = [desc[0] for desc in self.__cur.description] self.__cur.callproc("getCustomerView", (enc_key, criterion,))
return self.__cur.fetchall(), self.__all_cols 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): def addBusiness(self, data, contact_id):
try: try:
self.__cur.callproc("addBusiness", (json.dumps(data), contact_id)) if self.__cur:
self.__con.commit() self.__cur.callproc("addBusiness", (json.dumps(data), contact_id))
self.newBusinessAdded.emit() self.__con.commit()
self.newBusinessAdded.emit()
except mariadb.Error as e: except mariadb.Error as e:
print(str(e)) print(str(e))

View File

@@ -113,7 +113,7 @@ class BusinessModel(QAbstractTableModel):
@Slot(int) @Slot(int)
def onRowClicked(self, row): def onRowClicked(self, row):
print(row) print(f"Selected table row: {row}, corresponding DB ID: {self.__data[row][0]}")
@Slot(str) @Slot(str)
def viewCriterion(self, criterion): def viewCriterion(self, criterion):

View File

@@ -7,18 +7,22 @@ class ContactDAO:
def __init__(self): def __init__(self):
#print(f"*** File: {__file__}, __init__()") #print(f"*** File: {__file__}, __init__()")
self.__con = DbManager().getConnection() self.__con = DbManager().getConnection()
self.__cur = self.__con.cursor() if self.__con:
self.__cur = self.__con.cursor()
def getContacts(self): def getContacts(self):
print(f"*** File: {__file__}, getContacts()") print(f"*** File: {__file__}, getContacts()")
def addContact(self, contact, enc_key): def addContact(self, contact, enc_key):
try: try:
self.__cur.callproc("addContactPerson", (enc_key, json.dumps(contact),)) if self.__cur:
self.__con.commit() self.__cur.callproc("addContactPerson", (enc_key, json.dumps(contact),))
self.__cur.callproc("getLastInsertId") self.__con.commit()
contact_id = self.__cur.fetchone() self.__cur.callproc("getLastInsertId")
return contact_id[0] contact_id = self.__cur.fetchone()
return contact_id[0]
else:
return None
except mariadb.Error as e: except mariadb.Error as e:
print("MDB: " + str(e)) print("MDB: " + str(e))
except Exception as e: except Exception as e:

View File

@@ -7,10 +7,10 @@ class ContactModel(QObject):
contactIdReady = Signal(int) contactIdReady = Signal(int)
def __init__(self): def __init__(self):
super().__init__() super().__init__()
# print(f"*** File: {__file__}, __init__()")
#self.logger = logging.getLogger()
self.__conf = ConfigLoader().getConfig() self.__conf = ConfigLoader().getConfig()
self.__key = self.__conf['pyqcrm']['ENCRYPTION_KEY'] self.__key = self.__conf['pyqcrm']['ENCRYPTION_KEY']
#self.logger = logging.getLogger()
# print(f"*** File: {__file__}, __init__()")
self.__data = self.__getData() self.__data = self.__getData()
def getContacts(self): def getContacts(self):

View File

@@ -16,14 +16,17 @@ class DbManager(object):
def getConnection(cls): def getConnection(cls):
#print(f"DB Manager: {cls.__dbmanager}")
#print(f"DB Connection: {cls.__connection}")
try: try:
if not cls.__connection or not cls.__connection.ping(): if not cls.__connection or not cls.__connection.ping():
cls.__failure_notified = False
cls.__connection = mariadb.connect(**cls.__con_param) cls.__connection = mariadb.connect(**cls.__con_param)
except mariadb.InterfaceError as e: except mariadb.InterfaceError as e:
cls.__connection = mariadb.connect(**cls.__con_param) cls.__connection = mariadb.connect(**cls.__con_param)
print(f"DbManager Connection (INTERFACE ERROR): {e}..reconnecting...") print(f"DbManager Connection (INTERFACE ERROR): {e}..reconnecting...")
except mariadb.Error as e: except mariadb.Error as e:
print(f"Connection parameters are wrong: {e}") print(f"File: {__file__}\n Database connection error: {e}")
cls.__connection = None cls.__connection = None
return cls.__connection return cls.__connection

View File

@@ -4,15 +4,20 @@ from ..PyqcrmFlags import PyqcrmFlags
import mariadb import mariadb
class UserDAO: class UserDAO:
__cursor = None
def __init__(self): def __init__(self):
#print(f"*** File: {__file__}, init()")
self.__con = DbManager().getConnection() 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): def createUser(self, username, password, info, role= PyqcrmFlags.USER):
user_created = True user_created = False
try: try:
self.__cur.callproc("createUser", (username, password, info, role)) if self.__cur:
self.__con.commit() self.__cur.callproc("createUser", (username, password, info, role))
self.__con.commit()
user_created = True
except mariadb.Error as e: except mariadb.Error as e:
print(f"Error: {e}") print(f"Error: {e}")
print(e.errno) print(e.errno)
@@ -21,8 +26,11 @@ class UserDAO:
return user_created return user_created
def getUser(self, username): def getUser(self, username):
self.__cur.callproc("getUser", (username,)) if self.__cur:
return self.__cur.fetchone() self.__cur.callproc("getUser", (username,))
return self.__cur.fetchone()
else:
return None

View File

@@ -13,13 +13,14 @@ class UserManager(QObject):
def __init__(self, user_config = None, role = None): def __init__(self, user_config = None, role = None):
super().__init__() super().__init__()
self.__con = DbManager().getConnection() self.__con = DbManager().getConnection()
self.__cur = self.__con.cursor() if self.__con:
if user_config and role: self.__cur = self.__con.cursor()
if user_config and role:
self.__username = user_config["PYQCRM_USER"] self.__username = user_config["PYQCRM_USER"]
self.__password = user_config["PYQCRM_USER_PASS"] self.__password = user_config["PYQCRM_USER_PASS"]
self.__info = user_config["PYQCRM_USER_INFO"] self.__info = user_config["PYQCRM_USER_INFO"]
self.__role = role if role == PyqcrmFlags.ADMIN else 0 self.__role = role if role == PyqcrmFlags.ADMIN else 0
def createUser(self): def createUser(self):

View File

@@ -28,6 +28,7 @@ from lib.DB.ContactModel import ContactModel
# type="" # type=""
bad_config = False bad_config = False
db_con = False
address_model = None address_model = None
business_model = None business_model = None
business_type = None business_type = None
@@ -35,14 +36,14 @@ contact_model = None
user = None user = None
def initializeProgram(): 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 global address_model, bad_config, business_model, user, business_type, contact_model
if not bad_config: if not bad_config:
dbconf = config.getConfig()['database'] dbconf = config.getConfig()['database']
DbManager(dbconf) DbManager(dbconf)
bad_config = False bad_config = False
business_model = BusinessModel()
user = UserManager() user = UserManager()
business_model = BusinessModel()
address_model = AddressModel() address_model = AddressModel()
business_type = BTypeModel() business_type = BTypeModel()
contact_model = ContactModel() contact_model = ContactModel()
@@ -78,9 +79,12 @@ if __name__ == "__main__":
config.configurationReady.connect(initializeProgram) config.configurationReady.connect(initializeProgram)
else: else:
initializeProgram() initializeProgram()
if DbManager().getConnection():
db_con = True
engine.rootContext().setContextProperty("bad_config", bad_config) # print(f"Fehler: {i}") engine.rootContext().setContextProperty("bad_config", bad_config) # print(f"Fehler: {i}")
engine.rootContext().setContextProperty("db_con", db_con)
engine.rootContext().setContextProperty("config", config) engine.rootContext().setContextProperty("config", config)
engine.load(qml_file) engine.load(qml_file)

View File

@@ -15,5 +15,6 @@
<file>Gui/SearchBar.qml</file> <file>Gui/SearchBar.qml</file>
<file>js/qmldict.js</file> <file>js/qmldict.js</file>
<file>Gui/CustomerView.qml</file> <file>Gui/CustomerView.qml</file>
<file>Gui/NoDbConnection.qml</file>
</qresource> </qresource>
</RCC> </RCC>