From 698beaaffd6bdca42db381f847fdf5acbff32b8c Mon Sep 17 00:00:00 2001 From: Thomas Steen Rasmussen Date: Thu, 30 Aug 2018 17:54:31 +0200 Subject: [PATCH] refactor email functionality into seperate functions in economy.email module --- src/economy/email.py | 45 +++++++++++++++++++++++++++++++++++++++++++ src/economy/models.py | 36 ++++++++++++---------------------- 2 files changed, 57 insertions(+), 24 deletions(-) create mode 100644 src/economy/email.py diff --git a/src/economy/email.py b/src/economy/email.py new file mode 100644 index 00000000..7fabd8bf --- /dev/null +++ b/src/economy/email.py @@ -0,0 +1,45 @@ +import os + +from django.conf import settings + +from utils.email import add_outgoing_email + + +def send_accountingsystem_email(expense): + """ + Sends an email to the accountingsystem with the invoice as an attachment, + and with the expense uuid and description in email subject + """ + add_outgoing_email( + "emails/accountingsystem_email.txt", + formatdict=dict(expense=expense), + subject="Expense %s for %s" % (expense.pk, expense.camp.title), + to_recipients=[settings.ACCOUNTINGSYSTEM_EMAIL], + attachment=expense.invoice.read(), + attachment_filename=os.path.basename(expense.invoice.file.name), + ) + + +def send_expense_approved_email(expense): + """ + Sends an expense-approved email to the user who created the expense + """ + add_outgoing_email( + "emails/expense_approved_email.txt", + formatdict=dict(expense=expense), + subject="Your expense for %s has been approved." % expense.camp.title, + to_recipients=[expense.user.emailaddress_set.get(primary=True).email], + ) + + +def send_expense_rejected_email(expense): + """ + Sends an expense-rejected email to the user who created the expense + """ + add_outgoing_email( + "emails/expense_rejected_email.txt", + formatdict=dict(expense=expense), + subject="Your expense for %s has been rejected." % expense.camp.title, + to_recipients=[expense.user.emailaddress_set.get(primary=True).email], + ) + diff --git a/src/economy/models.py b/src/economy/models.py index 731bc1d7..cbe66323 100644 --- a/src/economy/models.py +++ b/src/economy/models.py @@ -5,8 +5,8 @@ from django.conf import settings from django.db import models from django.contrib import messages -from utils.email import add_outgoing_email from utils.models import CampRelatedModel, UUIDModel +from .email import * class Expense(CampRelatedModel, UUIDModel): @@ -93,27 +93,17 @@ class Expense(CampRelatedModel, UUIDModel): messages.error(request, "You cannot approve your own expenses, aka. the anti-stein-bagger defence") return + # mark as approved and save self.approved = True self.save() - # Add email for this expense which will be sent to the accounting software - add_outgoing_email( - "emails/accountingsystem_email.txt", - formatdict=dict(expense=self), - subject="Expense %s for %s" % (self.pk, self.camp.title), - to_recipients=[settings.ACCOUNTINGSYSTEM_EMAIL], - attachment=self.invoice.read(), - attachment_filename=os.path.basename(self.invoice.file.name), - ) + # send email to economic for this expense + send_accountingsystem_email(expense=self) - # Add email which will be sent to the user who entered the expense - add_outgoing_email( - "emails/expense_approved_email.txt", - formatdict=dict(expense=self), - subject="Your expense for %s has been approved." % self.camp.title, - to_recipients=[self.user.emailaddress_set.get(primary=True).email], - ) + # send email to the user + send_expense_approved_email(expense=self) + # message to the browser messages.success(request, "Expense %s approved" % self.pk) def reject(self, request): @@ -121,19 +111,17 @@ class Expense(CampRelatedModel, UUIDModel): This method marks an expense as not approved. Not approving an expense triggers an email to the user who submitted the expense in the first place. """ + # mark as not approved and save self.approved = False self.save() - # Add email which will be sent to the user who entered the expense - add_outgoing_email( - "emails/expense_rejected_email.txt", - formatdict=dict(expense=self), - subject="Your expense for %s has been rejected." % self.camp.title, - to_recipients=[self.user.emailaddress_set.get(primary=True).email], - ) + # send email to the user + send_expense_rejected_email(expense=self) + # message to the browser messages.success(request, "Expense %s rejected" % self.pk) + class Reimbursement(CampRelatedModel, UUIDModel): """ A reimbursement covers one or more expenses.