Skip to content

Commit

Permalink
Merge pull request #25 from moshthepitt/issue-24
Browse files Browse the repository at this point in the history
version 0.1.5
  • Loading branch information
moshthepitt authored Feb 12, 2019
2 parents 4be093d + 512050a commit da41dd7
Show file tree
Hide file tree
Showing 16 changed files with 469 additions and 195 deletions.
4 changes: 2 additions & 2 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ name = "pypi"
python_version = "3.6"

[packages]
"e1839a8" = {editable = true, path = "."}
e1839a8 = {editable = true,path = "."}

[dev-packages]
"flake8" = "*"
Expand All @@ -18,6 +18,6 @@ yapf = "*"
ipdb = "*"
model-mommy = "*"
tblib = "*"
pylint-django = {git = "https://github.com/PyCQA/pylint-django.git", ref = "4316c3d90f4ac6cbbeddfc8d431f5d4e031d5cf1"}
pylint-django = "*"
isort = "*"
"pep8" = "*"
343 changes: 175 additions & 168 deletions Pipfile.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
url='https://github.com/moshthepitt/small-small-hr',
packages=find_packages(exclude=['docs', 'tests']),
install_requires=[
'Django >= 2.0.8',
'Django >= 2.0.11',
'voluptuous',
'psycopg2-binary',
'sorl-thumbnail',
Expand Down
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, 4)
VERSION = (0, 1, 5)
__version__ = '.'.join(str(v) for v in VERSION)
# pylint: disable=invalid-name
default_app_config = 'small_small_hr.apps.SmallSmallHrConfig' # noqa
2 changes: 1 addition & 1 deletion small_small_hr/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class SmallSmallHrConfig(AppConfig):
app_label = 'small_small_hr'

def ready(self):
# pylint: disable=unused-variable
# pylint: disable=unused-import
import small_small_hr.signals # noqa

# set up app settings
Expand Down
24 changes: 17 additions & 7 deletions small_small_hr/emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,19 @@
from django.utils.translation import ugettext as _


def send_email( # pylint: disable=too-many-arguments
def send_email( # pylint: disable=too-many-arguments, too-many-locals
name: str, email: str, subject: str, message: str, obj: object = None,
cc_list: list = None):
cc_list: list = None, template: str = 'generic'):
"""
Sends a generic email
:param name: name of person
:param email: email address to send to
:param subject: the email's subject
:param message: the email's body text
:param obj: the object in question
:param cc_list: the list of email address to "CC"
:param template: the template to use
"""
context = {
'name': name,
Expand All @@ -22,12 +30,12 @@ def send_email( # pylint: disable=too-many-arguments
'SITE': Site.objects.get_current()
}
email_subject = render_to_string(
'small_small_hr/email/generic_email_subject.txt',
f'small_small_hr/email/{template}_email_subject.txt',
context).replace('\n', '')
email_txt_body = render_to_string(
'small_small_hr/email/generic_email_body.txt', context)
f'small_small_hr/email/{template}_email_body.txt', context)
email_html_body = render_to_string(
'small_small_hr/email/generic_email_body.html', context
f'small_small_hr/email/{template}_email_body.html', context
).replace('\n', '')

subject = email_subject
Expand Down Expand Up @@ -64,7 +72,8 @@ def leave_application_email(leave_obj: object):
email=admin_email,
subject=subj,
message=msg,
obj=leave_obj
obj=leave_obj,
template='leave_application'
)


Expand Down Expand Up @@ -113,7 +122,8 @@ def overtime_application_email(overtime_obj: object):
email=admin_email,
subject=subj,
message=msg,
obj=overtime_obj
obj=overtime_obj,
template='overtime_application'
)


Expand Down
10 changes: 6 additions & 4 deletions small_small_hr/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,13 @@ def clean(self):
start = cleaned_data.get('start')
date = cleaned_data.get('date')
staff = cleaned_data.get('staff')
status = cleaned_data.get('status')

# end must be later than start
if end <= start:
self.add_error('end', _("end must be greater than start"))

# must not overlap within the same date
# must not overlap within the same date unless being rejected
# pylint: disable=no-member
overlap_qs = OverTime.objects.filter(
date=date, staff=staff, status=OverTime.APPROVED).filter(
Expand All @@ -196,7 +197,7 @@ def clean(self):
if self.instance is not None:
overlap_qs = overlap_qs.exclude(id=self.instance.id)

if overlap_qs.exists():
if overlap_qs.exists() and status != OverTime.REJECTED:
msg = _('you cannot have overlapping overtime hours on the '
'same day')
self.add_error('start', msg)
Expand Down Expand Up @@ -337,6 +338,7 @@ def clean(self):
staff = cleaned_data.get('staff')
end = cleaned_data.get('end')
start = cleaned_data.get('start')
status = cleaned_data.get('status')

if all([staff, leave_type, start, end]):
# end year and start year must be the same
Expand Down Expand Up @@ -369,7 +371,7 @@ def clean(self):
self.add_error('start', msg)
self.add_error('end', msg)

# must not overlap
# must not overlap unless it is being rejected
# pylint: disable=no-member
overlap_qs = Leave.objects.filter(
staff=staff,
Expand All @@ -380,7 +382,7 @@ def clean(self):
if self.instance is not None:
overlap_qs = overlap_qs.exclude(id=self.instance.id)

if overlap_qs.exists():
if overlap_qs.exists() and status != Leave.REJECTED:
msg = _('you cannot have overlapping leave days')
self.add_error('start', msg)
self.add_error('end', msg)
Expand Down
16 changes: 8 additions & 8 deletions small_small_hr/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
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
# SSHR_LEAVE_PROCESSED_EMAIL_TXT - text of processed leave email
# SSHR_LEAVE_PROCESSED_EMAIL_SUBJ - subject of processed leave email
# SSHR_LEAVE_APPLICATION_EMAIL_TXT - text of leave application email
# SSHR_LEAVE_APPLICATION_EMAIL_SUBJ - subject of leave application email
# SSHR_OVERTIME_PROCESSED_EMAIL_TXT - text of processed overtime email
# SSHR_OVERTIME_PROCESSED_EMAIL_SUBJ - subject of processed overtime email
# SSHR_OVERTIME_APPLICATION_EMAIL_TXT - text of overtime application email
# SSHR_OVERTIME_APPLICATION_EMAIL_SUBJ - subject of overtime application email
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Hello {{name}},<br/><br/>
{{message|linebreaks}}
<br/><br/>
Thank you,<br/>
{{SITE.name}}<br/>
------<br/>
http://{{SITE.domain}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Hello {{name}},

{{message}}

Thank you,

{{SITE.name}}
------
http://{{SITE.domain}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{subject}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Hello {{name}},<br/><br/>
{{message|linebreaks}}
<br/><br/>
Thank you,<br/>
{{SITE.name}}<br/>
------<br/>
http://{{SITE.domain}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Hello {{name}},

{{message}}

Thank you,

{{SITE.name}}
------
http://{{SITE.domain}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{subject}}
126 changes: 123 additions & 3 deletions tests/test_emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Module to test small_small_hr Emails
"""
from datetime import datetime
from unittest.mock import patch
from unittest.mock import call, patch

from django.conf import settings
from django.core import mail
Expand Down Expand Up @@ -59,7 +59,8 @@ def test_leave_application_email(self, mock):
email="[email protected]",
subject="New Leave Application",
message="There has been a new leave application. Please log in to process it.", # noqa
obj=leave
obj=leave,
template="leave_application",
)

@patch('small_small_hr.emails.send_email')
Expand Down Expand Up @@ -107,7 +108,8 @@ def test_overtime_application_email(self, mock):
email="[email protected]",
subject="New Overtime Application",
message="There has been a new overtime application. Please log in to process it.", # noqa
obj=overtime
obj=overtime,
template="overtime_application",
)

@patch('small_small_hr.emails.send_email')
Expand Down Expand Up @@ -163,3 +165,121 @@ def test_send_email(self):
mail.outbox[0].alternatives[0][0],
'Hello Bob Munro,<br/><br/><p>The quick brown fox.</p><br/><br/>'
'Thank you,<br/>example.com<br/>------<br/>http://example.com')

@patch('small_small_hr.emails.Site.objects.get_current')
@patch('small_small_hr.emails.render_to_string')
def test_send_email_templates(self, mock, site_mock):
"""
Test the templates used with send_email
"""
mock.return_value = "Some random text"
site_mock.return_value = 42 # ensure that this is predictable

# test generic
data = {
'name': 'Bob Munro',
'email': '[email protected]',
'subject': "I love oov",
'message': "Its dangerous",
}

send_email(**data)

context = data.copy()
context.pop("email")
context["object"] = None
context["SITE"] = 42

expected_calls = [
call(
"small_small_hr/email/generic_email_subject.txt",
context
),
call(
"small_small_hr/email/generic_email_body.txt",
context
),
call(
"small_small_hr/email/generic_email_body.html",
context
)
]

mock.assert_has_calls(expected_calls)

mock.reset_mock()

# test leave
start = datetime(
2017, 6, 5, 0, 0, 0, tzinfo=pytz.timezone(settings.TIME_ZONE))
end = datetime(
2017, 6, 10, 0, 0, 0, tzinfo=pytz.timezone(settings.TIME_ZONE))
leave = mommy.make(
'small_small_hr.Leave', staff=self.staffprofile, start=start,
end=end, leave_type=Leave.SICK,
status=Leave.PENDING)

leave_application_email(leave)

context = dict(
name="Bob Ndoe",
subject="New Leave Application",
message="There has been a new leave application. Please log in to process it.", # noqa
object=leave,
SITE=42
)

expected_calls = [
call(
"small_small_hr/email/leave_application_email_subject.txt",
context
),
call(
"small_small_hr/email/leave_application_email_body.txt",
context
),
call(
"small_small_hr/email/leave_application_email_body.html",
context
)
]

mock.assert_has_calls(expected_calls)

mock.reset_mock()

# test overtime
start = datetime(
2017, 6, 5, 0, 0, 0, tzinfo=pytz.timezone(settings.TIME_ZONE))
end = datetime(
2017, 6, 10, 0, 0, 0, tzinfo=pytz.timezone(settings.TIME_ZONE))
overtime = mommy.make(
'small_small_hr.OverTime', staff=self.staffprofile, start=start,
end=end, status=OverTime.PENDING)

overtime_application_email(overtime)

context = dict(
name="Bob Ndoe",
subject="New Overtime Application",
message="There has been a new overtime application. Please log in to process it.", # noqa
object=overtime,
SITE=42
)

expected_calls = [
call(
"small_small_hr/email/overtime_application_email_subject.txt",
context
),
call(
"small_small_hr/email/overtime_application_email_body.txt",
context
),
call(
"small_small_hr/email/overtime_application_email_body.html",
context
)
]

mock.assert_has_calls(expected_calls)
Loading

0 comments on commit da41dd7

Please sign in to comment.