Use ORM for applicants

This commit is contained in:
Yuri Becker
2025-04-21 23:45:33 +02:00
parent 0ae153617b
commit 45f19d80d0
45 changed files with 388 additions and 354 deletions

View File

@@ -1,19 +1,18 @@
# This Python file uses the following encoding: utf-8
import toml
from platformdirs import user_config_dir
from pathlib import Path
from PySide6.QtCore import QObject, Slot, Signal
from .Vermasseln import Vermasseln
import shutil
from urllib.parse import urlparse
from .DB.DbManager import DbManager
import os
from Crypto.Random import get_random_bytes
from base64 import b64encode
from pathlib import Path
from urllib.parse import urlparse
import toml
from Crypto.Random import get_random_bytes
from PySide6.QtCore import QObject, Slot, Signal
from platformdirs import user_config_dir
from .DB.UserManager import UserManager
from .PyqcrmFlags import PyqcrmFlags
from .Vermasseln import Vermasseln
from .domain.BaseModel import database
class ConfigLoader(QObject):
@@ -21,7 +20,6 @@ class ConfigLoader(QObject):
__version = "0.1-alpha"
__check_enc_key = True
dbConnectionError = Signal(str, bool)
adminUserError = Signal(str, bool)
adminNotAsvailable = Signal()
@@ -31,7 +29,6 @@ class ConfigLoader(QObject):
def __init__(self):
super().__init__()
# print(f"In {__file__} file, __init__()")
self.config_dir = user_config_dir() + '/pyqcrm'
config_dir = Path(self.config_dir)
if config_dir.exists():
@@ -41,15 +38,13 @@ class ConfigLoader(QObject):
else:
config_dir.mkdir(0o750, True, True)
@Slot(dict, result = bool)
@Slot(dict, result=bool)
def setConfig(self, app_config):
# print(f"In {__file__} file, setConfig()")
if not self.__config:
base_conf = self.__initializeConfig()
conf = self.__checkDbConnection(app_config)
app_config = toml.dumps(app_config)
if conf:
if conf:
app_config = base_conf + app_config
self.__config = toml.loads(app_config)
self.__saveConfig()
@@ -65,45 +60,41 @@ class ConfigLoader(QObject):
conf = conf + f"ENCRYPTION_KEY = \"{self.__encrypt_key}\"\n\n"
return conf
def __checkDbConnection(self, db_config):
# print(f"In {__file__} file, __checkDbConnection()")
con = DbManager(db_config['database']).getConnection()
if con:
def __checkDbConnection(self):
if database.is_closed():
self.dbConnectionError.emit("Connection OK", True)
return True
else:
self.dbConnectionError.emit("Connection fehlgeschlagen", False)
return False
def __saveConfig(self):
# print(f"In {__file__} file, saveConfig()")
try:
with open (self.config_dir + '/pyqcrm.toml', 'w') as f:
with open(self.config_dir + '/pyqcrm.toml', 'w') as f:
# print(self.__config)
config = Vermasseln().oscarVermasseln(toml.dumps(self.__config))
f.write(config)
except FileNotFoundError:
print("Konnte die Konfiguration nicht speichern.")
def __checkAdminUser(self):
# print(f"In {__file__} file, __checkAdminUser()")
result = UserManager().checkAdmin()
if not result:
#if not result[0][0] == 1:
# if not result[0][0] == 1:
self.adminUserError.emit("Kein Admin vorhanden", False)
return False
else:
self.adminUserError.emit("Admin vorhanden", True)
return True
@Slot(dict, result= bool)
@Slot(dict, result=bool)
def addAdminUser(self, user_config):
# print(f"In {__file__} file, addAdminUser()")
admin = UserManager(user_config["user"], PyqcrmFlags.ADMIN).createUser()
if not admin:
#self.adminNotAvailable.emit()
# self.adminNotAvailable.emit()
self.adminUserError.emit("Benutzername nich verfügbar", False)
else:
self.__config['pyqcrm']['ENCRYPTION_KEY_VALID'] = 'Yes'
@@ -111,7 +102,6 @@ class ConfigLoader(QObject):
self.backupEncryptionKey.emit()
return admin
@Slot(str, str)
def __saveData(self, recovery_file, recovery_password, data):
# print(f"In {__file__} file, __saveData()")
@@ -121,7 +111,7 @@ class ConfigLoader(QObject):
rf = Vermasseln().oscarVermasseln(rf, local)
rec_file = urlparse(recovery_file)
if os.name == "nt":
rec_file = rec_file [1:]
rec_file = rec_file[1:]
else:
rec_file = rec_file.path + ".pyqrec"
try:
@@ -136,7 +126,7 @@ class ConfigLoader(QObject):
rec_file = urlparse(recovery_file)
rec_file = rec_file.path
if os.name == "nt":
rec_file = rec_file [1:]
rec_file = rec_file[1:]
try:
ek = self.__parseImport(rec_file, recovery_password)
@@ -165,7 +155,6 @@ class ConfigLoader(QObject):
else:
return None
def __invalidateEncryptionKey(self):
# print(f"In {__file__} file, __invalidateEncryptionKey()")
self.__config['pyqcrm']['ENCRYPTION_KEY_VALID'] = 'No'
@@ -182,7 +171,7 @@ class ConfigLoader(QObject):
rp = self.__setRecoveryPassword(recovery_password, salt)
return rp[1] == password
@Slot(str, str) # todo: non local encryption
@Slot(str, str) # todo: non local encryption
def importConfig(self, confile, password):
confile = urlparse(confile)
confile = confile.path
@@ -200,15 +189,10 @@ class ConfigLoader(QObject):
except Exception as e:
print(str(e))
def __configLoad(self):
# print(f"In {__file__} file, __configLoad()")
try:
with open (self.config_dir + '/pyqcrm.toml', 'r') as f:
with open(self.config_dir + '/pyqcrm.toml', 'r') as f:
config = f.read()
self.__config = toml.loads(Vermasseln().entschluesseln(config))
self.configurationReady.emit()
@@ -219,13 +203,12 @@ class ConfigLoader(QObject):
except Exception as e:
print(str(e))
def getConfig(self):
# print(f"In {__file__} file, getConfig()")
# print(self.__config)
return self.__config
def __setRecoveryPassword(self, key, salt = None):
def __setRecoveryPassword(self, key, salt=None):
# print(f"In {__file__} file, __setRecoveryPassword()")
key = Vermasseln.userPasswordHash(key, salt)
return key.split("$")
@@ -242,13 +225,12 @@ class ConfigLoader(QObject):
conf_file = toml.dumps(self.getConfig())
self.__saveData(filename, password, conf_file)
@Slot(dict)
def saveDbConf(self, db = None):
def saveDbConf(self, db=None):
self.__config.update(db)
self.__saveConfig()
@Slot(result = dict)
@Slot(result=dict)
def getDbConf(self):
try:
return self.__config['database']
@@ -257,11 +239,11 @@ class ConfigLoader(QObject):
return None
@Slot(dict)
def saveCompanyInfo(self, company = None):
def saveCompanyInfo(self, company=None):
self.__config.update(company)
self.__saveConfig()
@Slot(result = dict)
@Slot(result=dict)
def getCompanyInfo(self):
try:
return self.__config['company']
@@ -270,11 +252,11 @@ class ConfigLoader(QObject):
return None
@Slot(dict)
def saveMiscConf(self, misc_conf = None):
def saveMiscConf(self, misc_conf=None):
self.__config.update(misc_conf)
self.__saveConfig()
@Slot(result = bool)
@Slot(result=bool)
def systray(self):
try:
return self.__config['misc']['SYSTRAY']
@@ -282,9 +264,7 @@ class ConfigLoader(QObject):
print(f"Missing configuration: {ex}")
return False
@Slot(str, str)
def backupEncryptkey(self, filename, password):
encrypt_key = self.__config['pyqcrm']['ENCRYPTION_KEY']
self.__saveData(filename, password, encrypt_key)