Skip to content

Commit

Permalink
Merge pull request #23 from moshthepitt/fix-emails
Browse files Browse the repository at this point in the history
Cleanup emails
  • Loading branch information
moshthepitt authored Dec 9, 2018
2 parents 24f0e0b + 4085721 commit 4be093d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 22 deletions.
2 changes: 1 addition & 1 deletion small_small_hr/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Main init file for small_small_hr
"""
VERSION = (0, 1, 3)
VERSION = (0, 1, 4)
__version__ = '.'.join(str(v) for v in VERSION)
# pylint: disable=invalid-name
default_app_config = 'small_small_hr.apps.SmallSmallHrConfig' # noqa
35 changes: 19 additions & 16 deletions small_small_hr/emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
from django.utils.translation import ugettext as _


def send_email(
name: str, email: str, subject: str, message: str, obj: object = None):
def send_email( # pylint: disable=too-many-arguments
name: str, email: str, subject: str, message: str, obj: object = None,
cc_list: list = None):
"""
Sends a generic email
"""
Expand All @@ -35,6 +36,8 @@ def send_email(
text_content = email_txt_body
html_content = email_html_body
msg = EmailMultiAlternatives(subject, text_content, from_email, [to_email])
if cc_list:
msg.cc = cc_list
msg.attach_alternative(html_content, "text/html")

return msg.send(fail_silently=True)
Expand All @@ -46,15 +49,14 @@ def leave_application_email(leave_obj: object):
"""
msg = getattr(
settings,
'HR_LEAVE_APPLICATION_EMAIL_TXT',
'SSHR_LEAVE_APPLICATION_EMAIL_TXT',
_("There has been a new leave application. Please log in to process "
"it."))
subj = getattr(
settings,
'HR_LEAVE_APPLICATION_EMAIL_SUBJ',
'SSHR_LEAVE_APPLICATION_EMAIL_SUBJ',
_("New Leave Application"))
admin_emails = getattr(
settings, 'HR_ADMIN_EMAILS', [settings.DEFAULT_FROM_EMAIL])
admin_emails = settings.SSHR_ADMIN_LEAVE_EMAILS

for admin_email in admin_emails:
send_email(
Expand All @@ -72,20 +74,21 @@ def leave_processed_email(leave_obj: object):
"""
if leave_obj.staff.user.email:
msg = getattr(
settings, 'HR_LEAVE_PROCESSED_EMAIL_TXT',
settings, 'SSHR_LEAVE_PROCESSED_EMAIL_TXT',
_(f"You leave application status is "
f"{leave_obj.get_status_display()}. Log in for more info.")
)
subj = getattr(
settings, 'HR_LEAVE_PROCESSED_EMAIL_SUBJ',
settings, 'SSHR_LEAVE_PROCESSED_EMAIL_SUBJ',
_("Your leave application has been processed"))

send_email(
name=leave_obj.staff.get_name(),
email=leave_obj.staff.user.email,
subject=subj,
message=msg,
obj=leave_obj
obj=leave_obj,
cc_list=settings.SSHR_ADMIN_LEAVE_EMAILS
)


Expand All @@ -95,15 +98,14 @@ def overtime_application_email(overtime_obj: object):
"""
msg = getattr(
settings,
'HR_OVERTIME_APPLICATION_EMAIL_TXT',
'SSHR_OVERTIME_APPLICATION_EMAIL_TXT',
_("There has been a new overtime application. Please log in to "
"process it."))
subj = getattr(
settings,
'HR_OVERTIME_APPLICATION_EMAIL_SUBJ',
'SSHR_OVERTIME_APPLICATION_EMAIL_SUBJ',
_("New Overtime Application"))
admin_emails = getattr(
settings, 'HR_ADMIN_EMAILS', [settings.DEFAULT_FROM_EMAIL])
admin_emails = settings.SSHR_ADMIN_OVERTIME_EMAILS

for admin_email in admin_emails:
send_email(
Expand All @@ -122,18 +124,19 @@ def overtime_processed_email(overtime_obj: object):
if overtime_obj.staff.user.email:

msg = getattr(
settings, 'HR_OVERTIME_PROCESSED_EMAIL_TXT',
settings, 'SSHR_OVERTIME_PROCESSED_EMAIL_TXT',
_(f"You overtime application status is "
f"{overtime_obj.get_status_display()}. Log in for more info.")
)
subj = getattr(
settings, 'HR_OVERTIME_PROCESSED_EMAIL_SUBJ',
settings, 'SSHR_OVERTIME_PROCESSED_EMAIL_SUBJ',
_("Your overtime application has been processed"))

send_email(
name=overtime_obj.staff.get_name(),
email=overtime_obj.staff.user.email,
subject=subj,
message=msg,
obj=overtime_obj
obj=overtime_obj,
cc_list=settings.SSHR_ADMIN_OVERTIME_EMAILS
)
14 changes: 14 additions & 0 deletions small_small_hr/settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""
Configurable options
"""
from django.conf import settings

SSHR_MAX_CARRY_OVER = 10
SSHR_DAY_LEAVE_VALUES = {
1: 1, # Monday
Expand All @@ -22,3 +24,15 @@
{'day': 25, 'month': 12}, # Christmas
{'day': 26, 'month': 12}, # Boxing day
] # these are days that are not counted when getting taken leave days
# emails
SSHR_ADMIN_EMAILS = [settings.DEFAULT_FROM_EMAIL]
SSHR_ADMIN_LEAVE_EMAILS = SSHR_ADMIN_EMAILS
SSHR_ADMIN_OVERTIME_EMAILS = SSHR_ADMIN_EMAILS
# SSHR_LEAVE_PROCESSED_EMAIL_TXT
# SSHR_LEAVE_PROCESSED_EMAIL_SUBJ
# SSHR_LEAVE_APPLICATION_EMAIL_TXT
# SSHR_LEAVE_APPLICATION_EMAIL_SUBJ
# SSHR_OVERTIME_PROCESSED_EMAIL_TXT
# SSHR_OVERTIME_PROCESSED_EMAIL_SUBJ
# SSHR_OVERTIME_APPLICATION_EMAIL_TXT
# SSHR_OVERTIME_APPLICATION_EMAIL_SUBJ
16 changes: 11 additions & 5 deletions tests/test_emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@


@override_settings(
HR_ADMIN_EMAILS=["[email protected]"]
SSHR_ADMIN_EMAILS=["[email protected]"],
SSHR_ADMIN_LEAVE_EMAILS=["[email protected]"],
SSHR_ADMIN_OVERTIME_EMAILS=["[email protected]"]
)
class TestEmails(TestCase):
"""
Expand Down Expand Up @@ -81,7 +83,8 @@ def test_leave_processed_email(self, mock):
email="[email protected]",
subject="Your leave application has been processed",
message="You leave application status is Approved. Log in for more info.", # noqa
obj=leave
obj=leave,
cc_list=['[email protected]']
)

@patch('small_small_hr.emails.send_email')
Expand All @@ -101,7 +104,7 @@ def test_overtime_application_email(self, mock):

mock.assert_called_with(
name="Bob Ndoe",
email="hr@example.com",
email="ot@example.com",
subject="New Overtime Application",
message="There has been a new overtime application. Please log in to process it.", # noqa
obj=overtime
Expand All @@ -127,7 +130,8 @@ def test_overtime_processed_email(self, mock):
email="[email protected]",
subject="Your overtime application has been processed",
message="You overtime application status is Rejected. Log in for more info.", # noqa
obj=overtime
obj=overtime,
cc_list=['[email protected]']
)

def test_send_email(self):
Expand All @@ -141,14 +145,16 @@ def test_send_email(self):
'name': 'Bob Munro',
'email': '[email protected]',
'subject': "I love oov",
'message': message
'message': message,
'cc_list': settings.SSHR_ADMIN_EMAILS
}

send_email(**data)

self.assertEqual(len(mail.outbox), 1)
self.assertEqual(mail.outbox[0].subject, 'I love oov')
self.assertEqual(mail.outbox[0].to, ['Bob Munro <[email protected]>'])
self.assertEqual(mail.outbox[0].cc, ['[email protected]'])
self.assertEqual(
mail.outbox[0].body,
'Hello Bob Munro,\n\nThe quick brown fox.\n\nThank you,\n\n'
Expand Down

0 comments on commit 4be093d

Please sign in to comment.