fix mail not showning, gitignore

This commit is contained in:
WorldTeacher
2024-05-07 14:19:00 +02:00
parent 9aae45c2a2
commit 2eb1e74b97
3 changed files with 310 additions and 51 deletions

1
.gitignore vendored
View File

@@ -223,3 +223,4 @@ output/output/LOGtoJSON.exe
.pytest_cache .pytest_cache
output output
docs/

View File

@@ -23,14 +23,14 @@ from src.backend.create_file import recreateFile
from src.backend.database import Database from src.backend.database import Database
from src.backend.delete_temp_contents import delete_temp_contents from src.backend.delete_temp_contents import delete_temp_contents
from src.backend.semester import generateSemesterByDate from src.backend.semester import generateSemesterByDate
from src.logic import c_sort from src.logic import AvailChecker, BookGrabber, c_sort
from src.logic.constants import APP_NRS, PROF_TITLES from src.logic.constants import APP_NRS, PROF_TITLES
from src.logic.csvparser import csv_to_list from src.logic.csvparser import csv_to_list
from src.logic.dataclass import ApparatData, BookData, MailData, Subjects from src.logic.dataclass import ApparatData, BookData, MailData, Subjects
from src.logic.log import MyLogger from src.logic.log import MyLogger
from src.logic.threads import AvailChecker, BookGrabber from src.logic.threads import MailThread
from src.logic.wordparser import word_docx_to_csv from src.logic.wordparser import word_docx_to_csv
from src.ui import ( from src.ui import ( # Mail_Dialog,
App_Ext_Dialog, App_Ext_Dialog,
FilePicker, FilePicker,
GraphWidget, GraphWidget,
@@ -45,10 +45,143 @@ from src.ui import (
reminder_ui, reminder_ui,
settings_ui, settings_ui,
) )
from src.ui.dialogs.Ui_mail_preview import Ui_eMailPreview as Ui_Dialog
config = OmegaConf.load("config.yaml") config = OmegaConf.load("config.yaml")
# class Mail_Dialog(QtWidgets.QDialog, Ui_Dialog):
# def __init__(
# self,
# app_id,
# app_name,
# app_subject,
# prof_name,
# prof_mail,
# parent=None,
# default_mail="Information zum Semesterapparat {AppNr} - {AppName}.eml",
# ):
# super().__init__(parent)
# self.setupUi(
# self,
# # app_id,
# # app_name,
# # app_subject,
# # prof_name,
# )
# self.appid = app_id
# self.appname = app_name
# self.subject = app_subject
# self.profname = prof_name
# self.mail_data = ""
# self.prof_mail = prof_mail
# self.comboBox.currentIndexChanged.connect(self.set_mail)
# self.prof_name.setText(prof_name)
# self.mail_name.setText(self.prof_mail)
# # self.load_mail_templates()
# # if default_mail is not None:
# # # get the nearest match to the default mail
# # for i in range(self.comboBox.count()):
# # if default_mail in self.comboBox.itemText(i):
# # default_mail = self.comboBox.itemText(i)
# # break
# # self.comboBox.setCurrentText(default_mail)
# self.gender_female.clicked.connect(self.set_mail)
# self.gender_male.clicked.connect(self.set_mail)
# self.gender_non.clicked.connect(self.set_mail)
# self.buttonBox.accepted.connect(self.createAndSendMail)
# print(self.appid, self.appname, self.subject, self.profname, self.prof_mail)
# # self.send_button.clicked.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 load_mail_templates(self):
# print("loading mail templates")
# mail_templates = os.listdir("mail_vorlagen")
# for template in mail_templates:
# self.comboBox.addItem(template)
# def add_data(self, app_id, app_name, subject, prof_name, prof_mail):
# self.appid = app_id
# self.appname = app_name
# self.subject = subject
# self.profname = prof_name
# self.prof_mail = prof_mail
# self.prof_name.setText(prof_name)
# self.mail_name.setText(prof_mail)
# self.load_mail_templates()
# def get_greeting(self):
# if self.gender_male.isChecked():
# return "Sehr geehrter Herr"
# elif self.gender_female.isChecked():
# return "Sehr geehrte Frau"
# elif self.gender_non.isChecked():
# return "Guten Tag"
# def set_mail(self):
# email_template = self.comboBox.currentText()
# if email_template == "":
# return
# 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]
# email_header = email_header.format(AppNr=self.appid, AppName=self.appname)
# 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
# Appname = self.appname
# mail_html = mail_html.format(
# Profname=self.prof_name.text().split(" ")[-1],
# Appname=Appname,
# AppNr=self.appid,
# AppSubject=self.subject,
# greeting=self.get_greeting(),
# )
# self.mail_body.setHtml(mail_html)
# def createAndSendMail(self):
# import smtplib
# import ssl
# from email.mime.multipart import MIMEMultipart
# from email.mime.text import MIMEText
# smtp_server = config["mail"]["smtp_server"]
# port: int = config["mail"]["port"]
# sender_email = config["mail"]["sender"]
# password = config["mail"]["password"]
# message = MIMEMultipart()
# message["From"] = sender_email
# message["To"] = self.prof_mail
# message["Subject"] = self.mail_header.text()
# mail_body = self.mail_body.toHtml()
# message.attach(MIMEText(mail_body, "html"))
# mail = message.as_string()
# server = smtplib.SMTP_SSL(smtp_server, port)
# # server.starttls()
# # server.auth(mechanism="PLAIN")
# if config["mail"]["use_user_name"] == 1:
# print(config["mail"]["user_name"])
# server.login(config["mail"]["user_name"], password)
# else:
# server.login(sender_email, password)
# server.sendmail(sender_email, self.prof_mail, mail)
# print("Mail sent")
# # end active process
# server.quit()
class Medien(medienadder_ui): class Medien(medienadder_ui):
def __init__(self) -> None: def __init__(self) -> None:
self.logger = MyLogger("Medien") self.logger = MyLogger("Medien")
@@ -311,6 +444,7 @@ class Ui(Ui_Semesterapparat):
self.bookGrabber = None self.bookGrabber = None
self.availChecker = None self.availChecker = None
self.mail_thread = None
self.autoGrabber = None self.autoGrabber = None
def populate_appfach_dropdown(self): def populate_appfach_dropdown(self):
@@ -369,7 +503,7 @@ class Ui(Ui_Semesterapparat):
data = self.db.getProfByName(fullname) data = self.db.getProfByName(fullname)
# set the data # set the data
print(data) print(data)
if data == None: if data is None:
self.edit_faculty_member_title.setText("") self.edit_faculty_member_title.setText("")
self.faculty_member_old_telnr.setText("") self.faculty_member_old_telnr.setText("")
self.faculty_member_oldmail.setText("") self.faculty_member_oldmail.setText("")
@@ -380,7 +514,7 @@ class Ui(Ui_Semesterapparat):
self.faculty_member_oldmail.setText(data[5]) self.faculty_member_oldmail.setText(data[5])
( (
self.edit_faculty_member_title.setText(data[1]) self.edit_faculty_member_title.setText(data[1])
if data[1] != None if data[1] is not None
else self.edit_faculty_member_title.setText("") else self.edit_faculty_member_title.setText("")
) )
@@ -1692,7 +1826,9 @@ class Ui(Ui_Semesterapparat):
# self.btn_load_apparat() # self.btn_load_apparat()
if self.check_send_mail.isChecked(): if self.check_send_mail.isChecked():
self.contact_prof(apparat=appd.appnr) self.contact_prof(
apparat=appd.appnr, mail="Information zum Semesterapparat"
)
__clear_fields() __clear_fields()
def send_mail_preview(self): def send_mail_preview(self):
@@ -1830,6 +1966,7 @@ class Ui(Ui_Semesterapparat):
self.spin_select_message.setMaximum(len(messages)) self.spin_select_message.setMaximum(len(messages))
self.spin_select_message.setValue(1) self.spin_select_message.setValue(1)
self.label_total_day_messages.setText("/ " + str(len(messages))) self.label_total_day_messages.setText("/ " + str(len(messages)))
#
selected_date = self.calendarWidget.selectedDate().toString("yyyy-MM-dd") selected_date = self.calendarWidget.selectedDate().toString("yyyy-MM-dd")
print(selected_date) print(selected_date)
@@ -1998,8 +2135,6 @@ class Ui(Ui_Semesterapparat):
if apparat is None: if apparat is None:
self.confirm_popup("Bitte erst einen Apparat auswählen!") self.confirm_popup("Bitte erst einen Apparat auswählen!")
return return
dialog = QtWidgets.QDialog()
active_apparat_id = ( active_apparat_id = (
self.tableWidget_apparate.item( self.tableWidget_apparate.item(
self.tableWidget_apparate.currentRow(), 0 self.tableWidget_apparate.currentRow(), 0
@@ -2007,28 +2142,19 @@ class Ui(Ui_Semesterapparat):
if apparat is None if apparat is None
else apparat else apparat
) )
print(active_apparat_id)
profname = self.drpdwn_prof_name.currentText().replace(",", "").split(" ") profname = self.drpdwn_prof_name.currentText().replace(",", "").split(" ")
profname = f"{profname[1]} {profname[0]}" profname = f"{profname[1]} {profname[0]}"
prof_id = self.db.getProfId(self.drpdwn_prof_name.currentText()) prof_id = self.db.getProfId(self.drpdwn_prof_name.currentText())
mail = self.db.getSpecificProfData(prof_id, ["mail"]) pmail = self.db.getSpecificProfData(prof_id, ["mail"])
pass_data = { # create a new thread to show the mail interface and send the mail
"prof_name": profname, self.mail_thread = Mail_Dialog(
"mail_name": mail, app_id=active_apparat_id,
} prof_name=profname,
print( prof_mail=pmail,
profname,
mail,
)
mail_prev = Mail_Dialog(
app_id=self.active_apparat,
data=pass_data,
app_name=self.app_name.text(), app_name=self.app_name.text(),
app_subject=self.app_fach.currentText(), app_subject=self.app_fach.currentText(),
) )
mail_prev.setupUi(dialog) self.mail_thread.show()
mail_prev.set_mail()
dialog.exec()
def delete_apparat(self): def delete_apparat(self):
selected_apparat_id = self.tableWidget_apparate.item( selected_apparat_id = self.tableWidget_apparate.item(
@@ -2106,3 +2232,42 @@ if __name__ == "__main__":
# app.exec() # app.exec()
# open login screen # open login screen
launch_gui() launch_gui()

View File

@@ -1,24 +1,56 @@
import subprocess
import tempfile
import os import os
import sys
from omegaconf import OmegaConf
from PySide6 import QtCore, QtGui, QtWidgets from PySide6 import QtCore, QtGui, QtWidgets
from src.ui.dialogs.Ui_mail_preview import Ui_eMailPreview as Ui_Dialog from src.ui.dialogs.Ui_mail_preview import Ui_eMailPreview as Ui_Dialog
from omegaconf import OmegaConf
config = OmegaConf.load("config.yaml") config = OmegaConf.load("config.yaml")
class Mail_Dialog(QtWidgets.QDialog, Ui_Dialog): class Mail_Dialog(QtWidgets.QDialog, Ui_Dialog):
def __init__(self, parent=None, app_id="", app_name="", app_subject="", prof_name="", data = None): def __init__(
self,
app_id,
app_name,
app_subject,
prof_name,
prof_mail,
parent=None,
default_mail="Information zum Semesterapparat {AppNr} - {AppName}.eml",
):
super().__init__(parent) super().__init__(parent)
self.setupUi(self) self.setupUi(
self,
# app_id,
# app_name,
# app_subject,
# prof_name,
)
self.appid = app_id self.appid = app_id
self.appname = app_name self.appname = app_name
self.subject = app_subject self.subject = app_subject
self.profname = prof_name self.profname = prof_name
self.mail_data = "" self.mail_data = ""
self.data = data self.prof_mail = prof_mail
self.buttonBox.accepted.connect(self.save_mail) self.comboBox.currentIndexChanged.connect(self.set_mail)
self.comboBox.selec self.prof_name.setText(prof_name)
self.mail_name.setText(self.prof_mail)
self.load_mail_templates()
# if default_mail is not None:
# # get the nearest match to the default mail
# for i in range(self.comboBox.count()):
# if default_mail in self.comboBox.itemText(i):
# default_mail = self.comboBox.itemText(i)
# break
# self.comboBox.setCurrentText(default_mail)
self.gender_female.clicked.connect(self.set_mail)
self.gender_male.clicked.connect(self.set_mail)
self.gender_non.clicked.connect(self.set_mail)
self.buttonBox.accepted.connect(self.createAndSendMail)
# self.send_button.clicked.connect(self.save_mail)
# def set_data(self, data: dict): # def set_data(self, data: dict):
# print(data) # print(data)
@@ -27,6 +59,12 @@ class Mail_Dialog(QtWidgets.QDialog, Ui_Dialog):
# # assign data["general"] to self.data # # assign data["general"] to self.data
# self.data = data["general"] # self.data = data["general"]
def load_mail_templates(self):
print("loading mail templates")
mail_templates = os.listdir("mail_vorlagen")
for template in mail_templates:
self.comboBox.addItem(template)
def get_greeting(self): def get_greeting(self):
if self.gender_male.isChecked(): if self.gender_male.isChecked():
return "Sehr geehrter Herr" return "Sehr geehrter Herr"
@@ -34,44 +72,95 @@ class Mail_Dialog(QtWidgets.QDialog, Ui_Dialog):
return "Sehr geehrte Frau" return "Sehr geehrte Frau"
elif self.gender_non.isChecked(): elif self.gender_non.isChecked():
return "Guten Tag" return "Guten Tag"
def set_mail(self): def set_mail(self):
email_template = self.comboBox.currentText() email_template = self.comboBox.currentText()
if email_template == "":
return
with open(f"mail_vorlagen/{email_template}", "r", encoding="utf-8") as f: with open(f"mail_vorlagen/{email_template}", "r", encoding="utf-8") as f:
mail_template = f.read() mail_template = f.read()
email_header = email_template.split(".eml")[0] email_header = email_template.split(".eml")[0]
if "{AppNr}" in email_template: if "{AppNr}" in email_template:
email_header = email_template.split(".eml")[0] email_header = email_template.split(".eml")[0]
email_header = email_header.format(AppNr=self.appid, AppName=self.appname)
self.mail_header.setText(email_header) self.mail_header.setText(email_header)
self.mail_data = mail_template.split("<html>")[0] self.mail_data = mail_template.split("<html>")[0]
mail_html = mail_template.split("<html>")[1] mail_html = mail_template.split("<html>")[1]
mail_html = "<html>" + mail_html mail_html = "<html>" + mail_html
Appname = self.appname Appname = self.appname
mail_html = mail_html.format( mail_html = mail_html.format(
Profname=self.prof_name.text().split(" ")[-1], Appname=Appname, AppNr=self.appid, AppSubject = self.subject,greeting = self.get_greeting() Profname=self.prof_name.text().split(" ")[-1],
Appname=Appname,
AppNr=self.appid,
AppSubject=self.subject,
greeting=self.get_greeting(),
) )
self.mail_body.setHtml(mail_html) self.mail_body.setHtml(mail_html)
def save_mail(self): def createAndSendMail(self):
# create a temporary file import smtplib
mail_header = self.mail_header.text() import ssl
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
smtp_server = config["mail"]["smtp_server"]
port: int = config["mail"]["port"]
sender_email = config["mail"]["sender"]
password = config["mail"]["password"]
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = self.prof_mail
message["Subject"] = self.mail_header.text()
mail_body = self.mail_body.toHtml() mail_body = self.mail_body.toHtml()
mail = self.mail_data + mail_body message.attach(MIMEText(mail_body, "html"))
mail = mail.replace("Subject:", f"Subject: {mail_header}") mail = message.as_string()
directory = config["database"]["tempdir"]
directory = directory.replace("~", str(os.path.expanduser("~"))) server = smtplib.SMTP_SSL(smtp_server, port)
with tempfile.NamedTemporaryFile( # server.starttls()
mode="w", delete=False, suffix=".eml", encoding="utf-8", dir=directory # server.auth(mechanism="PLAIN")
) as f: if config["mail"]["use_user_name"] == 1:
f.write(mail) print(config["mail"]["user_name"])
self.mail_path = f.name server.login(config["mail"]["user_name"], password)
print(self.mail_path) else:
# open the file using thunderbird server.login(sender_email, password)
subprocess.Popen([f"{self.mail_path}"]) server.sendmail(sender_email, self.prof_mail, mail)
# delete the file print("Mail sent")
# os.remove(self.mail_path) # end active process
server.quit()
# self.accept()
# # 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}")
# directory = config["database"]["tempdir"]
# directory = directory.replace("~", str(os.path.expanduser("~")))
# with tempfile.NamedTemporaryFile(
# mode="w", delete=False, suffix=".eml", encoding="utf-8", dir=directory
# ) as f:
# f.write(mail)
# self.mail_path = f.name
# print(self.mail_path)
# # open the file using thunderbird
# subprocess.Popen([f"{self.mail_path}"])
# # delete the file
# # os.remove(self.mail_path)
def launch_gui(app_id="", app_name="", app_subject="", prof_name="", prof_mail=""):
app = QtWidgets.QApplication(sys.argv)
dialog = Mail_Dialog(
app_id=app_id,
app_name=app_name,
app_subject=app_subject,
prof_name=prof_name,
prof_mail=prof_mail,
default_mail="Information bezüglich der Auflösung des Semesterapparates",
)
dialog.show()
sys.exit(app.exec())
if __name__ == "__main__": if __name__ == "__main__":
@@ -83,3 +172,7 @@ if __name__ == "__main__":
ui.setupUi(Dialog) ui.setupUi(Dialog)
Dialog.show() Dialog.show()
sys.exit(app.exec()) sys.exit(app.exec())