From f3e2ef2515aa3528342cca44ab73acd65835677a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Thu, 27 Feb 2020 15:01:41 +0100 Subject: [PATCH] finished ##171 --- geruecht/controller/emailController.py | 44 ++++++++++++++++++++------ geruecht/controller/userController.py | 4 ++- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/geruecht/controller/emailController.py b/geruecht/controller/emailController.py index ebad0da..314342c 100644 --- a/geruecht/controller/emailController.py +++ b/geruecht/controller/emailController.py @@ -19,7 +19,7 @@ class EmailController(): self.email = email else: 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): LOGGER.info('Connect to E-Mail-Server') @@ -36,7 +36,30 @@ class EmailController(): log = self.smtp.login(self.user, self.passwd) 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: 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}})) @@ -44,16 +67,17 @@ class EmailController(): msg = MIMEMultipart() msg['From'] = self.email msg['To'] = user.mail - msg['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.' + + if type == 'credit': + subject, text = self.credit(user) + elif type == 'jobtransact': + subject, text = self.jobTransact(user, jobtransact) 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') + raise Exception("Fail to send Email. No type is set. user={}, type={} , jobtransact={}".format(user, type, jobtransact)) + + msg['Subject'] = subject msg.attach(text) + LOGGER.debug("Send email to {}: '{}'".format(user.uid, msg.as_string())) self.__connect__() self.smtp.sendmail(self.email, user.mail, msg.as_string()) diff --git a/geruecht/controller/userController.py b/geruecht/controller/userController.py index a7a542c..852662f 100644 --- a/geruecht/controller/userController.py +++ b/geruecht/controller/userController.py @@ -18,7 +18,9 @@ class UserController(metaclass=Singleton): pass 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): return db.getTransactJobFromUser(user, date.date())