fix issue with database path falling back to faulty path
update logic in welcome wizard, add checks for qapplication instance
This commit is contained in:
@@ -1,14 +1,25 @@
|
||||
from typing import Any
|
||||
from .widget_sources.welcome_wizard_ui import Ui_Wizard
|
||||
from PySide6 import QtWidgets, QtCore, QtGui
|
||||
from src import settings
|
||||
from src.backend import AdminCommands
|
||||
from src import settings, LOG_DIR
|
||||
from src.backend import Database
|
||||
import sys
|
||||
from appdirs import AppDirs
|
||||
from pathlib import Path
|
||||
import loguru
|
||||
|
||||
appdirs = AppDirs("SemesterApparatsManager", "SAM")
|
||||
|
||||
log = loguru.logger
|
||||
log.remove()
|
||||
log.add(sys.stdout, level="INFO")
|
||||
log.add(f"{LOG_DIR}/application.log", rotation="1 MB", retention="10 days")
|
||||
|
||||
|
||||
class WelcomeWizard(QtWidgets.QWizard, Ui_Wizard):
|
||||
def __init__(self, parent=None):
|
||||
super().__init__(parent)
|
||||
log.info("Initializing WelcomeWizard")
|
||||
self.setupUi(self)
|
||||
self.btn_database.clicked.connect(self.open_database_settings)
|
||||
self.btn_temp.clicked.connect(self.open_temp_settings)
|
||||
@@ -16,49 +27,102 @@ class WelcomeWizard(QtWidgets.QWizard, Ui_Wizard):
|
||||
self.btn_create.clicked.connect(self.create_sam_user)
|
||||
# mail password field
|
||||
self.settings_mail_password.setEchoMode(QtWidgets.QLineEdit.EchoMode.Password)
|
||||
self.settings_zotero_api_key.setEchoMode(QtWidgets.QLineEdit.EchoMode.Password)
|
||||
self.settings_openai_api_key.setEchoMode(QtWidgets.QLineEdit.EchoMode.Password)
|
||||
self.sam_password.setEchoMode(QtWidgets.QLineEdit.EchoMode.Password)
|
||||
|
||||
# allow user to toggle password visibility
|
||||
self.settings_mail_password.setContextMenuPolicy(
|
||||
QtCore.Qt.ContextMenuPolicy.CustomContextMenu
|
||||
)
|
||||
self.settings_zotero_api_key.setContextMenuPolicy(
|
||||
QtCore.Qt.ContextMenuPolicy.CustomContextMenu
|
||||
)
|
||||
self.settings_openai_api_key.setContextMenuPolicy(
|
||||
QtCore.Qt.ContextMenuPolicy.CustomContextMenu
|
||||
)
|
||||
self.sam_password.setContextMenuPolicy(
|
||||
QtCore.Qt.ContextMenuPolicy.CustomContextMenu
|
||||
)
|
||||
self.settings_mail_password.customContextMenuRequested.connect(
|
||||
self.toggle_password_visibility
|
||||
lambda pos: self.toggle_password_visibility(
|
||||
pos, self.settings_mail_password
|
||||
)
|
||||
)
|
||||
self.settings_zotero_api_key.customContextMenuRequested.connect(
|
||||
lambda pos: self.toggle_password_visibility(
|
||||
pos, self.settings_zotero_api_key
|
||||
)
|
||||
)
|
||||
self.settings_openai_api_key.customContextMenuRequested.connect(
|
||||
lambda pos: self.toggle_password_visibility(
|
||||
pos, self.settings_openai_api_key
|
||||
)
|
||||
)
|
||||
self.sam_password.customContextMenuRequested.connect(
|
||||
lambda pos: self.toggle_password_visibility(pos, self.sam_password)
|
||||
)
|
||||
# if button for next page is clicked, run function to store settings
|
||||
self.button(QtWidgets.QWizard.WizardButton.FinishButton).clicked.connect(
|
||||
self.button(QtWidgets.QWizard.WizardButton.NextButton).clicked.connect(
|
||||
self.store_settings
|
||||
)
|
||||
self.settings_mail_use_user_name.toggled.connect(self.set_check_text)
|
||||
# set initial values for checkbox, database
|
||||
self.set_check_text(self.settings_mail_use_user_name.isChecked())
|
||||
self.settings_database.setText(
|
||||
str(settings.database.path) or str(appdirs.user_data_dir)
|
||||
str(settings.database.path)
|
||||
if settings.database.path is not None
|
||||
else str(appdirs.user_data_dir)
|
||||
)
|
||||
|
||||
self.settings_temp.setText(
|
||||
str(settings.database.temp) or str(appdirs.user_cache_dir)
|
||||
str(settings.database.temp)
|
||||
if settings.database.temp is not None
|
||||
else str(appdirs.user_cache_dir)
|
||||
)
|
||||
self.settings_database_name.setText("semesterapparate.db")
|
||||
|
||||
def test_login_data(self):
|
||||
sam_users = AdminCommands().list_users()
|
||||
from src.backend import AdminCommands
|
||||
|
||||
log.info("Testing login data for SAM user")
|
||||
db_path = (
|
||||
self.settings_database.text() + "/" + self.settings_database_name.text()
|
||||
)
|
||||
db_path = Path(db_path).expanduser()
|
||||
db = Database(db_path=db_path)
|
||||
db.overwritePath(db_path)
|
||||
db.initializeDatabase()
|
||||
sam_users = AdminCommands(db_path=db_path).list_users()
|
||||
sam_users = [user[2] for user in sam_users]
|
||||
if not sam_users:
|
||||
QtWidgets.QMessageBox.critical(
|
||||
self,
|
||||
"Error",
|
||||
"No SAM users found. Please create a SAM user first.",
|
||||
)
|
||||
return
|
||||
|
||||
username = self.sam_username.text()
|
||||
if username in sam_users:
|
||||
QtWidgets.QMessageBox.information(
|
||||
self,
|
||||
"Error",
|
||||
"Information",
|
||||
f"User '{username}' exists in SAM.",
|
||||
)
|
||||
else:
|
||||
QtWidgets.QMessageBox.information(
|
||||
self,
|
||||
"Information",
|
||||
f"User '{username}' does not exist in SAM.",
|
||||
)
|
||||
|
||||
def create_sam_user(self):
|
||||
"""Create a SAM user in the database."""
|
||||
admin = AdminCommands()
|
||||
from src.backend import AdminCommands
|
||||
|
||||
db_path = (
|
||||
self.settings_database.text() + "/" + self.settings_database_name.text()
|
||||
)
|
||||
db_path = Path(db_path).expanduser()
|
||||
db = Database(db_path=db_path)
|
||||
db.overwritePath(db_path)
|
||||
db.initializeDatabase()
|
||||
admin = AdminCommands(db_path=db_path)
|
||||
admin.create_admin()
|
||||
if not admin.create_user(self.sam_username.text(), self.sam_password.text()):
|
||||
QtWidgets.QMessageBox.critical(
|
||||
self,
|
||||
@@ -79,17 +143,12 @@ class WelcomeWizard(QtWidgets.QWizard, Ui_Wizard):
|
||||
else:
|
||||
self.settings_mail_use_user_name.setText("Nutzername wird nicht verwendet")
|
||||
|
||||
def toggle_password_visibility(self, pos):
|
||||
def toggle_password_visibility(self, pos: Any, field):
|
||||
"""Toggle the visibility of the password field."""
|
||||
if (
|
||||
self.settings_mail_password.echoMode()
|
||||
== QtWidgets.QLineEdit.EchoMode.Password
|
||||
):
|
||||
self.settings_mail_password.setEchoMode(QtWidgets.QLineEdit.EchoMode.Normal)
|
||||
if field.echoMode() == QtWidgets.QLineEdit.EchoMode.Password:
|
||||
field.setEchoMode(QtWidgets.QLineEdit.EchoMode.Normal)
|
||||
else:
|
||||
self.settings_mail_password.setEchoMode(
|
||||
QtWidgets.QLineEdit.EchoMode.Password
|
||||
)
|
||||
field.setEchoMode(QtWidgets.QLineEdit.EchoMode.Password)
|
||||
|
||||
def store_settings(self):
|
||||
# database page
|
||||
@@ -135,7 +194,6 @@ class WelcomeWizard(QtWidgets.QWizard, Ui_Wizard):
|
||||
settings.set_openai_attr("model", openai_model)
|
||||
# save settings to file
|
||||
print("Saving settings...")
|
||||
print(settings)
|
||||
settings.save()
|
||||
|
||||
def open_database_settings(self):
|
||||
|
||||
Reference in New Issue
Block a user