refactor email functionality into seperate functions in economy.email module

This commit is contained in:
Thomas Steen Rasmussen 2018-08-30 17:54:31 +02:00
parent 8d32170422
commit 698beaaffd
2 changed files with 57 additions and 24 deletions

45
src/economy/email.py Normal file
View file

@ -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],
)

View file

@ -5,8 +5,8 @@ from django.conf import settings
from django.db import models from django.db import models
from django.contrib import messages from django.contrib import messages
from utils.email import add_outgoing_email
from utils.models import CampRelatedModel, UUIDModel from utils.models import CampRelatedModel, UUIDModel
from .email import *
class Expense(CampRelatedModel, UUIDModel): 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") messages.error(request, "You cannot approve your own expenses, aka. the anti-stein-bagger defence")
return return
# mark as approved and save
self.approved = True self.approved = True
self.save() self.save()
# Add email for this expense which will be sent to the accounting software # send email to economic for this expense
add_outgoing_email( send_accountingsystem_email(expense=self)
"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),
)
# Add email which will be sent to the user who entered the expense # send email to the user
add_outgoing_email( send_expense_approved_email(expense=self)
"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],
)
# message to the browser
messages.success(request, "Expense %s approved" % self.pk) messages.success(request, "Expense %s approved" % self.pk)
def reject(self, request): def reject(self, request):
@ -121,19 +111,17 @@ class Expense(CampRelatedModel, UUIDModel):
This method marks an expense as not approved. 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. 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.approved = False
self.save() self.save()
# Add email which will be sent to the user who entered the expense # send email to the user
add_outgoing_email( send_expense_rejected_email(expense=self)
"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],
)
# message to the browser
messages.success(request, "Expense %s rejected" % self.pk) messages.success(request, "Expense %s rejected" % self.pk)
class Reimbursement(CampRelatedModel, UUIDModel): class Reimbursement(CampRelatedModel, UUIDModel):
""" """
A reimbursement covers one or more expenses. A reimbursement covers one or more expenses.