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,15 +36,17 @@ 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):
try: date = '{}.{}.{}'.format(jobtransact['date'].day, jobtransact['date'].month, jobtransact['date'].year)
if user.mail == 'None' or not user.mail: from_user = '{} {}'.format(jobtransact['from_user'].firstname, jobtransact['from_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}})) subject = 'Bardienstanfrage am {}'.format(date)
raise Exception("no valid Email") text = MIMEText(
msg = MIMEMultipart() "Hallo {} {},\n"
msg['From'] = self.email "{} 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')
msg['To'] = user.mail return (subject, text)
msg['Subject'] = Header('Gerücht, bezahle deine Schulden!', 'utf-8')
def credit(self, user):
subject = Header('Gerücht, bezahle deine Schulden!', 'utf-8')
sum = user.getGeruecht(datetime.now().year).getSchulden() sum = user.getGeruecht(datetime.now().year).getSchulden()
if sum < 0: if sum < 0:
type = 'Schulden' type = 'Schulden'
@ -52,8 +54,30 @@ class EmailController():
else: else:
type = 'Guthaben' type = 'Guthaben'
add = '' 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') 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}}))
raise Exception("no valid Email")
msg = MIMEMultipart()
msg['From'] = self.email
msg['To'] = user.mail
if type == 'credit':
subject, text = self.credit(user)
elif type == 'jobtransact':
subject, text = self.jobTransact(user, jobtransact)
else:
raise Exception("Fail to send Email. No type is set. user={}, type={} , jobtransact={}".format(user, type, jobtransact))
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())