finished ##171

This commit is contained in:
Tim Gröger 2020-02-27 15:01:41 +01:00
parent ff2df817a3
commit f3e2ef2515
2 changed files with 37 additions and 11 deletions

View File

@ -19,7 +19,7 @@ class EmailController():
self.email = email self.email = email
else: else:
self.email = user self.email = user
LOGGER.debug('Init EmailController with smtpServer={}, port={}, user={}, crypt={}, email={}'.format(smtpServer, user, port, crypt, self.email)) LOGGER.debug('Init EmailController with smtpServer={}, port={}, user={}, crypt={}, email={}'.format(smtpServer, port, user, crypt, self.email))
def __connect__(self): def __connect__(self):
LOGGER.info('Connect to E-Mail-Server') LOGGER.info('Connect to E-Mail-Server')
@ -36,7 +36,30 @@ class EmailController():
log = self.smtp.login(self.user, self.passwd) log = self.smtp.login(self.user, self.passwd)
LOGGER.debug(log) LOGGER.debug(log)
def sendMail(self, user): def jobTransact(self, user, jobtransact):
date = '{}.{}.{}'.format(jobtransact['date'].day, jobtransact['date'].month, jobtransact['date'].year)
from_user = '{} {}'.format(jobtransact['from_user'].firstname, jobtransact['from_user'].lastname)
subject = 'Bardienstanfrage am {}'.format(date)
text = MIMEText(
"Hallo {} {},\n"
"{} fragt, ob du am {} zum Bardienst teilnehmen willst. Beantworte die Anfrage im Userportal von Flaschengeist.".format(user.firstname, user.lastname, from_user, date), 'utf-8')
return (subject, text)
def credit(self, user):
subject = Header('Gerücht, bezahle deine Schulden!', 'utf-8')
sum = user.getGeruecht(datetime.now().year).getSchulden()
if sum < 0:
type = 'Schulden'
add = 'Bezahle diese umgehend an den Finanzer.'
else:
type = 'Guthaben'
add = ''
text = MIMEText(
"Hallo {} {},\nDu hast {} im Wert von {:.2f} €. {}\n\nDiese Nachricht wurde automatisch erstellt.".format(
user.firstname, user.lastname, type, abs(sum) / 100, add), 'plain', 'utf-8')
return (subject, text)
def sendMail(self, user, type='credit', jobtransact=None):
try: try:
if user.mail == 'None' or not user.mail: if user.mail == 'None' or not user.mail:
LOGGER.debug("cant send email to {}. Has no email-address. {}".format(user.uid, {'error': True, 'user': {'userId': user.uid, 'firstname': user.firstname, 'lastname': user.lastname}})) LOGGER.debug("cant send email to {}. Has no email-address. {}".format(user.uid, {'error': True, 'user': {'userId': user.uid, 'firstname': user.firstname, 'lastname': user.lastname}}))
@ -44,16 +67,17 @@ class EmailController():
msg = MIMEMultipart() msg = MIMEMultipart()
msg['From'] = self.email msg['From'] = self.email
msg['To'] = user.mail msg['To'] = user.mail
msg['Subject'] = Header('Gerücht, bezahle deine Schulden!', 'utf-8')
sum = user.getGeruecht(datetime.now().year).getSchulden() if type == 'credit':
if sum < 0: subject, text = self.credit(user)
type = 'Schulden' elif type == 'jobtransact':
add = 'Bezahle diese umgehend an den Finanzer.' subject, text = self.jobTransact(user, jobtransact)
else: else:
type = 'Guthaben' raise Exception("Fail to send Email. No type is set. user={}, type={} , jobtransact={}".format(user, type, jobtransact))
add = ''
text = MIMEText("Hallo {} {},\nDu hast {} im Wert von {:.2f} €. {}\n\nDiese Nachricht wurde automatisch erstellt.".format(user.firstname, user.lastname, type, abs(sum)/100, add), 'plain', 'utf-8') msg['Subject'] = subject
msg.attach(text) msg.attach(text)
LOGGER.debug("Send email to {}: '{}'".format(user.uid, msg.as_string())) LOGGER.debug("Send email to {}: '{}'".format(user.uid, msg.as_string()))
self.__connect__() self.__connect__()
self.smtp.sendmail(self.email, user.mail, msg.as_string()) self.smtp.sendmail(self.email, user.mail, msg.as_string())

View File

@ -18,7 +18,9 @@ class UserController(metaclass=Singleton):
pass pass
def setTransactJob(self, from_user, to_user, date): def setTransactJob(self, from_user, to_user, date):
return db.setTransactJob(from_user, to_user, date.date()) jobtransact = db.setTransactJob(from_user, to_user, date.date())
emailController.sendMail(jobtransact['to_user'], 'jobtransact', jobtransact)
return jobtransact
def getTransactJobFromUser(self, user, date): def getTransactJobFromUser(self, user, date):
return db.getTransactJobFromUser(user, date.date()) return db.getTransactJobFromUser(user, date.date())