closes #3, fix bug with user creation

This commit is contained in:
WorldTeacher
2024-09-16 13:51:44 +02:00
parent f5bb5d3adc
commit e5816b40d2
2 changed files with 52 additions and 29 deletions

View File

@@ -25,12 +25,12 @@ class Database:
try:
os.makedirs(config.database.path)
except FileNotFoundError:
print(self.db_path)
dbg(self.db_path)
if not os.path.exists(config.database.backupLocation):
os.makedirs(config.database.backupLocation)
#if main path does not exist, try to create it. if that fails, use the backuplocation
print(self.db_path)
dbg(self.db_path)
self.checkDatabaseStatus()
def handle_folder_reachability(self, original_path, backup_path):
@@ -59,7 +59,7 @@ class Database:
return backup_path +"/" + FILE
else:
print("Original Path Exists, ")
dbg("Original Path Exists, using this path")
# Original folder is reachable, check for backup
if os.path.exists(backup_file):
# Restore backup
@@ -198,15 +198,24 @@ class Database:
self.close_connection(conn)
return True
def getUserBy(self, key, value) -> User:
conn = self.connect()
cursor = conn.cursor()
cursor.execute(f"SELECT * FROM users WHERE {key} = '{value}'")
result = cursor.fetchone()
self.close_connection(conn)
user = User(userid=result[1], username=result[2], email=result[3], id=result[0])
return user
def getUser(self, user_id) -> User:
conn = self.connect()
cursor = conn.cursor()
cursor.execute(f"SELECT * FROM users")
result = cursor.fetchall()
self.close_connection(conn)
print(result)
for res in result:
if res[0] == user_id:
if res[1] == user_id:
user = User(userid=res[1], username=res[2], email=res[3], id=res[0])
dbg(f"Returning User {user}")
log.info(f"Returning User {user}")
@@ -452,7 +461,7 @@ class Database:
cursor = conn.cursor()
cursor.execute(query)
result = cursor.fetchone()
print(result)
dbg("Result", response=result)
self.close_connection(conn)
if result is not None:
return result[0]

View File

@@ -106,14 +106,16 @@ class MainUI(QtWidgets.QMainWindow, Ui_MainWindow):
log.info("Showing Settings")
settings = Settings()
settings.exec()
if settings.settingschanged:
# reload settings
print(config)
#print(config)
def changeMode(self):
log.info("Changing Mode")
dbg(f"Current mode: {self.activeState}")
self.input_username.clear()
stayReturn = False
if self.userdata.toPlainText() != "":
stayReturn = True
self.userdata.clear()
self.input_userno.clear()
self.btn_show_lentmedia.setText("")
@@ -123,7 +125,10 @@ class MainUI(QtWidgets.QMainWindow, Ui_MainWindow):
self.mediaOverview.setRowCount(0)
self.activeUser = None #! remove if last user should be kept
if self.activeState == "Rückgabe":
self.activateLoanMode()
if stayReturn:
self.activateReturnMode()
else: self.activateLoanMode()
else:
self.activateReturnMode()
@@ -190,9 +195,10 @@ class MainUI(QtWidgets.QMainWindow, Ui_MainWindow):
user = CreateUser(fieldname="id", data="")
user.exec()
userid = user.userid
print(userid)
if userid:
log.info(f"User created {userid}")
data = self.db.getUser(userid)
data = self.db.getUserBy("user_id", userid)
self.activeUser = data
# set user to active user
self.setUserData()
@@ -233,7 +239,7 @@ class MainUI(QtWidgets.QMainWindow, Ui_MainWindow):
self.setUserData()
self.input_file_ident.setFocus()
self.mode.setText("Ausleihe")
print(self.activeUser.__dict__)
#print(self.activeUser.__dict__)
loans = self.db.getActiveLoans(self.activeUser.id)
dbg(loans=loans)
self.btn_show_lentmedia.setText(loans)
@@ -298,7 +304,7 @@ class MainUI(QtWidgets.QMainWindow, Ui_MainWindow):
elif book_id:
if isinstance(book_id, list) and len(book_id) > 1:
print("Multiple Books found")
#print("Multiple Books found")
# TODO: implement book selection dialog
return
else:
@@ -350,7 +356,7 @@ class MainUI(QtWidgets.QMainWindow, Ui_MainWindow):
self.duedate.date().toString("yyyy-MM-dd"),
)
media = self.db.getMedia(book_id[0])
print(media)
#print(media)
self.mediaOverview.insertRow(0)
self.mediaOverview.setItem(0, 0, QtWidgets.QTableWidgetItem(media.signature))
self.mediaOverview.setItem(0, 1, QtWidgets.QTableWidgetItem(media.title))
@@ -401,7 +407,8 @@ class MainUI(QtWidgets.QMainWindow, Ui_MainWindow):
self.setStatusTipMessage("Buch nicht entliehen")
self.input_file_ident.clear()
else:
print("Book not found")
dbg("Book not found")
#print("Book not found")
#self.input_file_ident.setPlaceholderText(f"Buch {identifier} nicht gefunden")
def setStatusTipMessage(self, message):
@@ -414,7 +421,7 @@ class MainUI(QtWidgets.QMainWindow, Ui_MainWindow):
def exit_handler():
dbg("Exiting, creating backup")
app = QtWidgets.QApplication(sys.argv)
print(backup.backup)
#print(backup.backup)
# generate report if monday
if datetime.datetime.now().weekday() == config.report.report_day:
generate_report()
@@ -446,8 +453,15 @@ def exit_handler():
dialog.setWindowTitle("Backup nicht möglich")
dialog.setText("Backup konnte nicht erstellt werden\nGrund: {}".format(reason))
dialog.exec()
def launch(options = None):
app = QtWidgets.QApplication(sys.argv if options is None else [options])
def launch(*argv):
options = sys.argv
if argv:
options += [arg for arg in argv]
options = [arg for arg in options if arg.startswith("--")]
#print("Launching Main UI")
#print(options)
app = QtWidgets.QApplication([])
main_ui = MainUI()
atexit.register(exit_handler)
sys.exit(app.exec())