diff --git a/small_small_hr/__init__.py b/small_small_hr/__init__.py index 8b9b9f6..d062cbe 100644 --- a/small_small_hr/__init__.py +++ b/small_small_hr/__init__.py @@ -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 diff --git a/small_small_hr/emails.py b/small_small_hr/emails.py index 0284bdd..eb79966 100644 --- a/small_small_hr/emails.py +++ b/small_small_hr/emails.py @@ -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 """ @@ -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) @@ -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( @@ -72,12 +74,12 @@ 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( @@ -85,7 +87,8 @@ def leave_processed_email(leave_obj: object): email=leave_obj.staff.user.email, subject=subj, message=msg, - obj=leave_obj + obj=leave_obj, + cc_list=settings.SSHR_ADMIN_LEAVE_EMAILS ) @@ -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( @@ -122,12 +124,12 @@ 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( @@ -135,5 +137,6 @@ def overtime_processed_email(overtime_obj: object): email=overtime_obj.staff.user.email, subject=subj, message=msg, - obj=overtime_obj + obj=overtime_obj, + cc_list=settings.SSHR_ADMIN_OVERTIME_EMAILS ) diff --git a/small_small_hr/settings.py b/small_small_hr/settings.py index eda5287..3c5ed82 100644 --- a/small_small_hr/settings.py +++ b/small_small_hr/settings.py @@ -1,6 +1,8 @@ """ Configurable options """ +from django.conf import settings + SSHR_MAX_CARRY_OVER = 10 SSHR_DAY_LEAVE_VALUES = { 1: 1, # Monday @@ -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 diff --git a/tests/test_emails.py b/tests/test_emails.py index f772377..e77e075 100644 --- a/tests/test_emails.py +++ b/tests/test_emails.py @@ -19,7 +19,9 @@ @override_settings( - HR_ADMIN_EMAILS=["hr@example.com"] + SSHR_ADMIN_EMAILS=["admin@example.com"], + SSHR_ADMIN_LEAVE_EMAILS=["hr@example.com"], + SSHR_ADMIN_OVERTIME_EMAILS=["ot@example.com"] ) class TestEmails(TestCase): """ @@ -81,7 +83,8 @@ def test_leave_processed_email(self, mock): email="bob@example.com", 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=['hr@example.com'] ) @patch('small_small_hr.emails.send_email') @@ -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 @@ -127,7 +130,8 @@ def test_overtime_processed_email(self, mock): email="bob@example.com", 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=['ot@example.com'] ) def test_send_email(self): @@ -141,7 +145,8 @@ def test_send_email(self): 'name': 'Bob Munro', 'email': 'bob@example.com', 'subject': "I love oov", - 'message': message + 'message': message, + 'cc_list': settings.SSHR_ADMIN_EMAILS } send_email(**data) @@ -149,6 +154,7 @@ def test_send_email(self): self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].subject, 'I love oov') self.assertEqual(mail.outbox[0].to, ['Bob Munro ']) + self.assertEqual(mail.outbox[0].cc, ['admin@example.com']) self.assertEqual( mail.outbox[0].body, 'Hello Bob Munro,\n\nThe quick brown fox.\n\nThank you,\n\n'