Files
SemesterapparatsManager/test/rds_test.py
2024-05-17 08:35:37 +02:00

104 lines
3.3 KiB
Python

def contact_prof(self):
dialog = QtWidgets.QDialog()
mail_prev = Mail_Dialog()
mail_prev.setupUi(dialog)
mail_prevs = os.listdir("mail_vorlagen")
if self.app_name.text() == "":
mail_prevs.remove("Information zum Semesterapparat.eml")
mail_prev.comboBox.addItems(mail_prevs)
active_apparat_id = self.tableWidget_apparate.item(
self.tableWidget_apparate.currentRow(), 0
).text()
general_data = {
"Appname": self.app_name.text(),
"AppSubject": self.app_fach.currentText(),
"appnr": self.active_apparat,
}
print(active_apparat_id)
mail_prev.appid = active_apparat_id
base_data = self.db.getProfData(id=active_apparat_id)
profname = self.db.getProfNameById(base_data["id"])
profname = profname.split(" ")
profname = f"{profname[1]} {profname[0]}"
pass_data = {
"prof_name": profname,
"mail_name": base_data["prof_mail"],
"general": general_data,
}
mail_prev.set_data(pass_data)
mail_prev.set_mail()
dialog.exec()
import subprocess
import tempfile
from PyQt6 import QtCore, QtGui, QtWidgets
from src.ui.dialogs.mail_preview import Ui_Dialog
class Mail_Dialog(QtWidgets.QDialog, Ui_Dialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
self.appid = ""
self.mail_data = ""
self.data = None
self.buttonBox.accepted.connect(self.save_mail)
def set_data(self, data: dict):
print(data)
self.prof_name.setText(data["prof_name"])
self.mail_name.setText(data["mail_name"])
# assign data["general"] to self.data
self.data = data["general"]
def set_mail(self):
email_template = self.comboBox.currentText()
with open(f"mail_vorlagen/{email_template}", "r", encoding="utf-8") as f:
mail_template = f.read()
email_header = email_template.split(".eml")[0]
if "{AppNr}" in email_template:
email_header = email_template.split(".eml")[0].format(AppNr=self.appid)
self.mail_header.setText(email_header)
self.mail_data = mail_template.split("<html>")[0]
mail_html = mail_template.split("<html>")[1]
mail_html = "<html>" + mail_html
print(self.data)
Appname = self.data["Appname"]
mail_html = mail_html.format(
Profname=self.prof_name.text().split(" ")[-1], Appname=Appname
)
self.mail_body.setHtml(mail_html)
def save_mail(self):
# create a temporary file
mail_header = self.mail_header.text()
mail_body = self.mail_body.toHtml()
mail = self.mail_data + mail_body
mail = mail.replace("Subject:", f"Subject: {mail_header}")
with tempfile.NamedTemporaryFile(
mode="w", delete=False, suffix=".eml", encoding="utf-8", dir="mails"
) as f:
f.write(mail)
self.mail_path = f.name
print(self.mail_path)
# open the file using thunderbird
subprocess.Popen(["thunderbird", f"{self.mail_path}"])
# delete the file
# os.remove(self.mail_path)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Mail_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec())