code change

add refresh_elsa_table function, bugfixes
implement background processing to threaded functions
This commit is contained in:
WorldTeacher
2024-06-18 14:16:37 +02:00
parent f799a01e14
commit 0170a8292c

View File

@@ -13,6 +13,7 @@ from omegaconf import OmegaConf
from PyQt6 import QtCore, QtGui, QtWidgets from PyQt6 import QtCore, QtGui, QtWidgets
from PyQt6.QtCore import QDate, QThread from PyQt6.QtCore import QDate, QThread
from PyQt6.QtGui import QRegularExpressionValidator from PyQt6.QtGui import QRegularExpressionValidator
import yaml
from src import Icon from src import Icon
from src.backend import ( from src.backend import (
@@ -32,7 +33,6 @@ from src.logic import (
BookGrabber, BookGrabber,
MyLogger, MyLogger,
csv_to_list, csv_to_list,
custom_sort,
elsa_word_to_csv, elsa_word_to_csv,
word_docx_to_csv, word_docx_to_csv,
) )
@@ -325,6 +325,13 @@ class Ui(Ui_Semesterapparat):
self.db.createElsaApparat(date, prof, semester) self.db.createElsaApparat(date, prof, semester)
self.cancel_elsa_creation() self.cancel_elsa_creation()
self.refresh_elsa_table()
def refresh_elsa_table(self):
self.elsa_table.setRowCount(0)
elsa_apparats = self.db.getElsaApparats()
for apparat in elsa_apparats:
self.insert_elsa_into_table(apparat)
def insert_elsa_into_table(self, apparat): def insert_elsa_into_table(self, apparat):
self.elsa_table.insertRow(0) self.elsa_table.insertRow(0)
@@ -859,9 +866,8 @@ class Ui(Ui_Semesterapparat):
self.chkbx_show_del_media.show() self.chkbx_show_del_media.show()
self.cancel_active_selection.setEnabled(False) self.cancel_active_selection.setEnabled(False)
def confirm_popup(self, message: str): def confirm_popup(self, message: str, title: str):
popup = popus_confirm() popup = popus_confirm(title=title)
popup.setupUi()
popup.textEdit.setReadOnly(True) popup.textEdit.setReadOnly(True)
popup.textEdit.setText(message) popup.textEdit.setText(message)
@@ -1104,7 +1110,7 @@ class Ui(Ui_Semesterapparat):
def btn_add_medium(self): def btn_add_medium(self):
if not self.app_group_box.isEnabled(): if not self.app_group_box.isEnabled():
self.confirm_popup("Bitte erst einen Apparat auswählen!") self.confirm_popup("Bitte erst einen Apparat auswählen!", title="Fehler")
return return
def __new_ui(): def __new_ui():
@@ -1130,10 +1136,11 @@ class Ui(Ui_Semesterapparat):
# check if app_id is in database # check if app_id is in database
if not self.db.checkApparatExists(app_id): if not self.db.checkApparatExists(app_id):
# create apparat # create apparat
self.btn_save_apparat() self.btn_save_apparat(False)
# create a thread that updates the progress label after each medium # create a thread that updates the progress label after each medium
self.bookGrabber = BookGrabber( self.bookGrabber = BookGrabber(
mode=mode, app_id=app_id, prof_id=prof_id, data=data mode=mode, prof_id=prof_id, app_id=app_id, data=data
) )
self.bookGrabber.finished.connect(self.bookGrabber.deleteLater) self.bookGrabber.finished.connect(self.bookGrabber.deleteLater)
self.bookGrabber.finished.connect(self.hide_progress_label) self.bookGrabber.finished.connect(self.hide_progress_label)
@@ -1141,6 +1148,10 @@ class Ui(Ui_Semesterapparat):
self.bookGrabber.updateSignal.connect(self.update_progress_label) self.bookGrabber.updateSignal.connect(self.update_progress_label)
self.bookGrabber.start() self.bookGrabber.start()
while self.bookGrabber.isRunning():
print("waiting for thread to finish")
QtWidgets.QApplication.processEvents()
self.__clear_fields()
else: else:
return return
@@ -1324,7 +1335,6 @@ class Ui(Ui_Semesterapparat):
print("Add document") print("Add document")
picker = FilePicker() picker = FilePicker()
files = picker.pick_files() files = picker.pick_files()
for file in files: for file in files:
print(file) print(file)
filename = file.split("/")[-1] filename = file.split("/")[-1]
@@ -1353,7 +1363,7 @@ class Ui(Ui_Semesterapparat):
self.dokument_list.currentRow(), 1 self.dokument_list.currentRow(), 1
).text() ).text()
except AttributeError: except AttributeError:
self.confirm_popup("Bitte erst ein Dokument auswählen!") self.confirm_popup("Bitte erst ein Dokument auswählen!", title="Fehler")
return return
if not _selected_doc_location == "Database": if not _selected_doc_location == "Database":
path = Path(_selected_doc_location) path = Path(_selected_doc_location)
@@ -1426,7 +1436,9 @@ class Ui(Ui_Semesterapparat):
print(file) print(file)
if file_type == "pdf": if file_type == "pdf":
# Todo: implement parser here # Todo: implement parser here
self.confirm_popup("PDF Dateien werden nochnicht unterstützt!") self.confirm_popup(
"PDF Dateien werden nochnicht unterstützt!", title="Fehler"
)
return return
if file_type == "csv": if file_type == "csv":
signatures = csv_to_list(file) signatures = csv_to_list(file)
@@ -1477,10 +1489,12 @@ class Ui(Ui_Semesterapparat):
# check if apparat in database # check if apparat in database
# if app_id not in database, create apparat # if app_id not in database, create apparat
created = False
if not self.db.checkApparatExistsById(app_id): if not self.db.checkApparatExistsById(app_id):
# create apparat # create apparat
print("Creating apparat") print("Creating apparat")
self.btn_save_apparat() self.btn_save_apparat(False)
created = True
if self.dokument_list.rowCount() == 0: if self.dokument_list.rowCount() == 0:
print("No file selected") print("No file selected")
self.tableWidget_apparate.setEnabled(True) self.tableWidget_apparate.setEnabled(True)
@@ -1503,10 +1517,13 @@ class Ui(Ui_Semesterapparat):
if file_location == "Database": if file_location == "Database":
file = recreateFile(file_name, app_id, file_type, open=False) file = recreateFile(file_name, app_id, file_type, open=False)
else: else:
self.add_files() if not created:
self.add_files()
if file_type == "pdf": if file_type == "pdf":
# Todo: implement parser here # Todo: implement parser here
self.confirm_popup("PDF Dateien werden noch nicht unterstützt!") self.confirm_popup(
"PDF Dateien werden noch nicht unterstützt!", title="Fehler"
)
return return
if file_type == "csv": if file_type == "csv":
signatures = csv_to_list(file) signatures = csv_to_list(file)
@@ -1525,59 +1542,57 @@ class Ui(Ui_Semesterapparat):
self.autoGrabber = BookGrabber( self.autoGrabber = BookGrabber(
mode="ARRAY", app_id=app_id, prof_id=prof_id, data=signatures mode="ARRAY", app_id=app_id, prof_id=prof_id, data=signatures
) )
# grabber.mode = "ARRAY"
# grabber.data = signatures
# grabber.app_id = app_id
# grabber.prof_id = prof_id
# grabber.moveToThread(thread)
self.label_info.show() self.label_info.show()
self.progress_label.show() self.progress_label.show()
self.line_2.show() self.line_2.show()
# grabber.finished.connect(thread.quit) # grabber.finished.connect(thread.quit)
self.autoGrabber.finished.connect(self.autoGrabber.deleteLater) self.autoGrabber.finished.connect(self.autoGrabber.deleteLater)
self.autoGrabber.finished.connect(self.hide_progress_label) self.autoGrabber.finished.connect(self.hide_progress_label)
self.autoGrabber.finished.connect(self.update_app_media_list)
self.autoGrabber.finished.connect(self.unlock_apparate) self.autoGrabber.finished.connect(self.unlock_apparate)
self.autoGrabber.updateSignal.connect(self.update_progress_label) self.autoGrabber.updateSignal.connect(self.update_progress_label)
# worker.finished.connect(worker.deleteLater) # worker.finished.connect(worker.deleteLater)
self.autoGrabber.start() self.autoGrabber.start()
# self.thread = thread while self.autoGrabber.isRunning():
# kill grabber after completion QtWidgets.QApplication.processEvents()
# self.grabbers.append(grabber) # end of thread
# self.autoGrabber.exit()
self.update_app_media_list()
self.__clear_fields()
self.btn_cancel_active_selection()
def unlock_apparate(self): def unlock_apparate(self):
self.tableWidget_apparate.setEnabled(True) self.tableWidget_apparate.setEnabled(True)
self.tableWidget_apparate.setToolTip("") self.tableWidget_apparate.setToolTip("")
def btn_save_apparat(self): def __clear_fields(self):
def __clear_fields(): self.drpdwn_app_nr.setCurrentText("")
self.drpdwn_app_nr.clear() self.prof_title.clear()
self.prof_title.clear() self.drpdwn_prof_name.clearMask()
self.drpdwn_prof_name.clearMask() self.drpdwn_prof_name.setCurrentText("")
self.app_name.clear() self.app_name.clear()
self.prof_mail.clear() self.prof_mail.clear()
self.prof_tel_nr.clear() self.prof_tel_nr.clear()
self.app_fach.setCurrentText("") self.app_fach.setCurrentText("")
self.app_name.clear() self.app_name.clear()
self.sem_year.clear() self.sem_year.clear()
self.dokument_list.setRowCount(0) self.dokument_list.setRowCount(0)
self.sem_winter.setChecked(False) self.sem_winter.setChecked(False)
self.sem_sommer.setChecked(False) self.sem_sommer.setChecked(False)
self.check_eternal_app.setChecked(False) self.check_eternal_app.setChecked(False)
self.prof_id_adis.clear() self.prof_id_adis.clear()
self.prof_id_adis.clear() self.prof_id_adis.clear()
self.apparat_id_adis.clear() self.apparat_id_adis.clear()
self.drpdwn_prof_name.clear() self.drpdwn_prof_name.clear()
self.tableWidget_apparat_media.setRowCount(0) self.tableWidget_apparat_media.setRowCount(0)
self.app_group_box.setEnabled(False) self.app_group_box.setEnabled(False)
self.groupBox_2.hide() self.groupBox_2.hide()
self.groupBox.hide() self.groupBox.hide()
self.check_send_mail.setChecked(False) self.check_send_mail.setChecked(False)
self.cancel_active_selection.setEnabled(False) self.cancel_active_selection.setEnabled(False)
def btn_save_apparat(self, clear_fields=True):
if not self.validate_fields(): if not self.validate_fields():
self.confirm_popup("Bitte alle Pflichtfelder ausfüllen!") self.confirm_popup("Bitte alle Pflichtfelder ausfüllen!", title="Fehler")
return return
appd = ApparatData() appd = ApparatData()
appd.appnr = self.active_apparat appd.appnr = self.active_apparat
@@ -1601,7 +1616,7 @@ class Ui(Ui_Semesterapparat):
pass pass
self.db.createApparat(appd) self.db.createApparat(appd)
# if self.dokument_list.rowCount() > 0: # if self.dokument_list.rowCount() > 0:
self.add_files() # self.add_files()
appdata = self.db.getAllAparats() appdata = self.db.getAllAparats()
# merge self.appdata and appdata, remove duplicates # merge self.appdata and appdata, remove duplicates
self.apparats = list(set(self.apparats + appdata)) self.apparats = list(set(self.apparats + appdata))
@@ -1614,7 +1629,8 @@ class Ui(Ui_Semesterapparat):
self.contact_prof( self.contact_prof(
apparat=appd.appnr, mail="Information zum Semesterapparat" apparat=appd.appnr, mail="Information zum Semesterapparat"
) )
__clear_fields() if clear_fields:
self.__clear_fields()
def send_mail_preview(self): def send_mail_preview(self):
pass pass
@@ -1837,7 +1853,7 @@ class Ui(Ui_Semesterapparat):
signature=signature, signature=signature,
) )
message = f'Soll das Medium "{self.tableWidget_apparat_media.item(self.tableWidget_apparat_media.currentRow(),0).text()}" wirklich gelöscht werden?' message = f'Soll das Medium "{self.tableWidget_apparat_media.item(self.tableWidget_apparat_media.currentRow(),0).text()}" wirklich gelöscht werden?'
state = self.confirm_popup(message) state = self.confirm_popup(message, title="Löschen?")
print(state) print(state)
if state == 1: if state == 1:
self.db.deleteBook(book_id) self.db.deleteBook(book_id)
@@ -1853,7 +1869,7 @@ class Ui(Ui_Semesterapparat):
rows.append(row) rows.append(row)
print(rows) print(rows)
message = f"Sollen die {len(rows)} Medien wirklich gelöscht werden?" message = f"Sollen die {len(rows)} Medien wirklich gelöscht werden?"
state = self.confirm_popup(message) state = self.confirm_popup(message, title="Löschen?")
if state == 1: if state == 1:
for _ in rows: for _ in rows:
signature = self.tableWidget_apparat_media.item(_, 1).text() signature = self.tableWidget_apparat_media.item(_, 1).text()
@@ -1891,7 +1907,9 @@ class Ui(Ui_Semesterapparat):
print(apparat) print(apparat)
if self.active_apparat == "": if self.active_apparat == "":
if apparat is False: if apparat is False:
self.confirm_popup("Bitte erst einen Apparat auswählen!") self.confirm_popup(
"Bitte erst einen Apparat auswählen!", title="Apparat auswählen"
)
return return
else: else:
# TODO: stuff # TODO: stuff