From 559ad1b882e842d81d3aa545f5f1dfd1e1581c453c98271385e6547b91123407 Mon Sep 17 00:00:00 2001 From: linuxero Date: Sat, 14 Dec 2024 15:32:10 +0100 Subject: [PATCH] Fixing qml connection to save business with a new contact - some database modifications --- Gui/AddContact.qml | 9 ++++++++- Gui/AddCustomer.qml | 19 ++++++++++++------- Gui/CustomerTable.qml | 8 ++++++-- lib/DB/BusinessDAO.py | 10 +++++++--- lib/DB/BusinessModel.py | 13 +++++++++++-- lib/DB/ContactDAO.py | 6 +++--- lib/DB/ContactModel.py | 11 +++++++---- 7 files changed, 54 insertions(+), 22 deletions(-) diff --git a/Gui/AddContact.qml b/Gui/AddContact.qml index 18303ec..c925bcd 100644 --- a/Gui/AddContact.qml +++ b/Gui/AddContact.qml @@ -125,7 +125,14 @@ GridLayout placeholderText: qsTr("TT.MM.JJJJ") validator: RegularExpressionValidator { - regularExpression: /((^|)([0-2]{1}[0-9]{1}|3[0-1]))\.((^|)([0-1]{1,2}|12))\.([0-9]{4})/ + regularExpression: /((^|)([0-2]{1}[0-9]{1}|3[0-1]))\.((^|)(0[1-9]{1}|1[0-2]{1}))\.((^|)(196[0-9]{1}|19[7-9]{1}[0-9]{1}|20[0-9]{2}))/ + } + + onTextChanged: + { + var len = birthday.length + var bd = birthday.text + if (len == 2 || len == 5) birthday.text = bd + "." } } diff --git a/Gui/AddCustomer.qml b/Gui/AddCustomer.qml index bced2c1..5c5ea86 100644 --- a/Gui/AddCustomer.qml +++ b/Gui/AddCustomer.qml @@ -85,7 +85,18 @@ ColumnLayout Layout.fillHeight: true } - Component.onCompleted: contact_model.contactIdReady.connect(onContactId) + //Component.onCompleted: contact_model.contactIdReady.connect(onContactId) + + Connections + { + target: contact_model + onContactIdReady: + { + var con_id = arguments[0] + business_model.addBusiness(new_business, con_id) + appLoader.source = "CustomerTable.qml" + } + } function checkFields() { @@ -101,10 +112,4 @@ ColumnLayout else saveBtn.enabled = true } - - function onContactId(con_id) - { - business_model.addBusiness(new_business, con_id)// bm - appLoader.source = "CustomerTable.qml" - } } diff --git a/Gui/CustomerTable.qml b/Gui/CustomerTable.qml index 4c366cb..292d5ce 100644 --- a/Gui/CustomerTable.qml +++ b/Gui/CustomerTable.qml @@ -107,6 +107,10 @@ Item { id: customerTable Layout.fillHeight: true Layout.fillWidth: true + ScrollBar.vertical: ScrollBar + { + policy: customerTable.contentHeight > customerTable.height ? ScrollBar.AlwaysOn : ScrollBar.AlwaysOff + } columnSpacing: 1 rowSpacing: 2 model: business_model @@ -162,7 +166,7 @@ Item { Text { - text: model.display + text: model.display == null? "": model.display elide: Text.ElideRight width: parent.width height: parent.height @@ -197,7 +201,7 @@ Item { Item { - Layout.fillHeight: true + //Layout.fillHeight: true Layout.fillWidth: true } } diff --git a/lib/DB/BusinessDAO.py b/lib/DB/BusinessDAO.py index 2680fdb..0a373a2 100644 --- a/lib/DB/BusinessDAO.py +++ b/lib/DB/BusinessDAO.py @@ -1,15 +1,18 @@ from .DbManager import DbManager import json import mariadb +from PySide6.QtCore import QObject, Signal -class BusinessDAO: +class BusinessDAO(QObject): + newBusinessAdded = Signal() def __init__(self): + super().__init__() self.__con = DbManager().getConnection() self.__cur = self.__con.cursor() - def getBusiness(self, criterion = "Alle"): - self.__cur.callproc("getCustomerView", (criterion,)) + 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 @@ -17,6 +20,7 @@ class BusinessDAO: try: 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 701b176..73dc789 100644 --- a/lib/DB/BusinessModel.py +++ b/lib/DB/BusinessModel.py @@ -2,6 +2,7 @@ from PySide6.QtCore import QAbstractTableModel, QModelIndex, Qt, Slot, Signal from .BusinessDAO import BusinessDAO from ..PyqcrmFlags import PyqcrmFlags +from ..ConfigLoader import ConfigLoader # USERS TABLE # CUSTOMER_COLUMN_NAMES = \ @@ -63,14 +64,19 @@ from ..PyqcrmFlags import PyqcrmFlags class BusinessModel(QAbstractTableModel): __visible_index = {} __col_name = "" + __business_dao = None def __init__(self): super().__init__() + self.__business_dao = BusinessDAO() + self.__business_dao.newBusinessAdded.connect(self.__refreshView) + self.__conf = ConfigLoader().getConfig() + self.__key = self.__conf['pyqcrm']['ENCRYPTION_KEY'] self.__getData() def __getData(self, criterion = "Alle"): self.beginResetModel() - rows, self.__visible_columns = BusinessDAO().getBusiness(criterion) + rows, self.__visible_columns = self.__business_dao.getBusiness(self.__key, criterion) self.__data = rows self.endResetModel() @@ -116,7 +122,10 @@ class BusinessModel(QAbstractTableModel): @Slot(dict, int) def addBusiness(self, business, contact_id): - BusinessDAO().addBusiness(business, contact_id) + self.__business_dao.addBusiness(business, contact_id) + + @Slot() + def __refreshView(self): self.__getData() @Slot(dict) diff --git a/lib/DB/ContactDAO.py b/lib/DB/ContactDAO.py index 6d055f2..918fa7e 100644 --- a/lib/DB/ContactDAO.py +++ b/lib/DB/ContactDAO.py @@ -5,16 +5,16 @@ import mariadb class ContactDAO: def __init__(self): - print(f"*** File: {__file__}, __init__()") + #print(f"*** File: {__file__}, __init__()") self.__con = DbManager().getConnection() self.__cur = self.__con.cursor() def getContacts(self): print(f"*** File: {__file__}, getContacts()") - def addContact(self, contact): + def addContact(self, contact, enc_key): try: - self.__cur.callproc("addContactPerson", (json.dumps(contact),)) + self.__cur.callproc("addContactPerson", (enc_key, json.dumps(contact),)) self.__con.commit() self.__cur.callproc("getLastInsertId") contact_id = self.__cur.fetchone() diff --git a/lib/DB/ContactModel.py b/lib/DB/ContactModel.py index e3ffb59..02951da 100644 --- a/lib/DB/ContactModel.py +++ b/lib/DB/ContactModel.py @@ -1,27 +1,30 @@ from PySide6.QtCore import QAbstractTableModel, QModelIndex, Qt, Slot, QObject, Signal from .ContactDAO import ContactDAO +from ..ConfigLoader import ConfigLoader import logging class ContactModel(QObject): contactIdReady = Signal(int) def __init__(self): super().__init__() + self.__conf = ConfigLoader().getConfig() + self.__key = self.__conf['pyqcrm']['ENCRYPTION_KEY'] #self.logger = logging.getLogger() - print(f"*** File: {__file__}, __init__()") + # print(f"*** File: {__file__}, __init__()") self.__data = self.__getData() def getContacts(self): - print(f"*** File: {__file__}, getContacts()") + # print(f"*** File: {__file__}, getContacts()") logging.debug("No debug message") return self.__data def __getData(self): - print(f"*** File: {__file__}, __getData()") + # print(f"*** File: {__file__}, __getData()") ContactDAO().getContacts() @Slot(dict) def addContact(self, contact): - i = ContactDAO().addContact(contact) + i = ContactDAO().addContact(contact, self.__key) self.contactIdReady.emit(i)