Implement search
This commit is contained in:
@@ -2,21 +2,24 @@
|
||||
import os
|
||||
from base64 import b64encode
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
from urllib.parse import urlparse
|
||||
|
||||
import toml
|
||||
from Crypto.Random import get_random_bytes
|
||||
from PySide6.QtCore import QObject, Slot, Signal
|
||||
from peewee import OperationalError
|
||||
from platformdirs import user_config_dir
|
||||
|
||||
from .Config import Config, DatabaseConfig
|
||||
from .DB.UserManager import UserManager
|
||||
from .PyqcrmFlags import PyqcrmFlags
|
||||
from .Vermasseln import Vermasseln
|
||||
from .domain.BaseModel import database
|
||||
from .domain.BaseModel import init_database_from_config
|
||||
|
||||
|
||||
class ConfigLoader(QObject):
|
||||
__config = None
|
||||
__config: Optional[Config] = None
|
||||
__version = "0.1-alpha"
|
||||
__check_enc_key = True
|
||||
|
||||
@@ -39,34 +42,34 @@ class ConfigLoader(QObject):
|
||||
config_dir.mkdir(0o750, True, True)
|
||||
|
||||
@Slot(dict, result=bool)
|
||||
def setConfig(self, app_config):
|
||||
def setConfig(self, app_config: Config):
|
||||
if not self.__config:
|
||||
base_conf = self.__initializeConfig()
|
||||
conf = self.__checkDbConnection(app_config)
|
||||
app_config = toml.dumps(app_config)
|
||||
conf = self._is_db_connectable(app_config['database'])
|
||||
app_config = toml.dumps(app_config)
|
||||
if conf:
|
||||
app_config = base_conf + app_config
|
||||
self.__config = toml.loads(app_config)
|
||||
self.__saveConfig()
|
||||
conf = self.__checkAdminUser()
|
||||
if conf:
|
||||
app_config = base_conf + app_config
|
||||
self.__config = toml.loads(app_config)
|
||||
self.__saveConfig()
|
||||
conf = self.__checkAdminUser()
|
||||
if conf:
|
||||
self.configurationReady.emit()
|
||||
self.configurationReady.emit()
|
||||
|
||||
def __initializeConfig(self):
|
||||
# print(f"In {__file__} file, __initializeConfig()")
|
||||
self.__encrypt_key = b64encode(get_random_bytes(32)).decode("utf-8")
|
||||
conf = f"[pyqcrm]\nVERSION = \"{self.__version}\"\n"
|
||||
conf = conf + f"ENCRYPTION_KEY_VALID = \"No\"\n"
|
||||
conf = conf + f"ENCRYPTION_KEY = \"{self.__encrypt_key}\"\n\n"
|
||||
return conf
|
||||
|
||||
def __checkDbConnection(self):
|
||||
if database.is_closed():
|
||||
def _is_db_connectable(self, config: DatabaseConfig):
|
||||
try:
|
||||
init_database_from_config(config)
|
||||
self.dbConnectionError.emit("Connection OK", True)
|
||||
return True
|
||||
else:
|
||||
except OperationalError as e:
|
||||
self.dbConnectionError.emit("Connection fehlgeschlagen", False)
|
||||
return False
|
||||
return False
|
||||
|
||||
def __saveConfig(self):
|
||||
# print(f"In {__file__} file, saveConfig()")
|
||||
@@ -190,7 +193,6 @@ class ConfigLoader(QObject):
|
||||
print(str(e))
|
||||
|
||||
def __configLoad(self):
|
||||
# print(f"In {__file__} file, __configLoad()")
|
||||
try:
|
||||
with open(self.config_dir + '/pyqcrm.toml', 'r') as f:
|
||||
config = f.read()
|
||||
@@ -200,16 +202,11 @@ class ConfigLoader(QObject):
|
||||
print("Konnte die Konfiguration nicht laden.")
|
||||
except TypeError:
|
||||
print(f"Invalid Configuration: {__file__}")
|
||||
except Exception as e:
|
||||
print(str(e))
|
||||
|
||||
def getConfig(self):
|
||||
# print(f"In {__file__} file, getConfig()")
|
||||
# print(self.__config)
|
||||
def get_config(self) -> Optional[Config]:
|
||||
return self.__config
|
||||
|
||||
def __setRecoveryPassword(self, key, salt=None):
|
||||
# print(f"In {__file__} file, __setRecoveryPassword()")
|
||||
key = Vermasseln.userPasswordHash(key, salt)
|
||||
return key.split("$")
|
||||
|
||||
@@ -222,7 +219,7 @@ class ConfigLoader(QObject):
|
||||
|
||||
@Slot(str, str)
|
||||
def backupConfig(self, filename, password):
|
||||
conf_file = toml.dumps(self.getConfig())
|
||||
conf_file = toml.dumps(self.get_config())
|
||||
self.__saveData(filename, password, conf_file)
|
||||
|
||||
@Slot(dict)
|
||||
@@ -233,7 +230,7 @@ class ConfigLoader(QObject):
|
||||
@Slot(result=dict)
|
||||
def getDbConf(self):
|
||||
try:
|
||||
return self.__config['database']
|
||||
return self.__config['database'] if self.__config is not None else None
|
||||
except KeyError as ex:
|
||||
print(f"Missing database configuration: {ex}")
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user