Skip to content

Commit

Permalink
Merge pull request #16 from moshthepitt/qol-fixes
Browse files Browse the repository at this point in the history
Add emails for leave & overtime application and processing
  • Loading branch information
moshthepitt authored Oct 17, 2018
2 parents 705731d + b13fea3 commit 197ee3e
Show file tree
Hide file tree
Showing 14 changed files with 479 additions and 155 deletions.
6 changes: 2 additions & 4 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ python_version = "3.6"

[dev-packages]
"flake8" = "*"
"pep8" = "*"
pylint = "*"
pycodestyle = "*"
coverage = "*"
tox = "*"
yapf = "*"
isort = "*"
ipdb = "*"
model-mommy = "*"
"autopep8" = "*"
tblib = "*"
pylint-django = {git = "https://github.com/PyCQA/pylint-django.git", ref = "4316c3d90f4ac6cbbeddfc8d431f5d4e031d5cf1"}
"pep8" = "*"
isort = "*"
250 changes: 109 additions & 141 deletions Pipfile.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
default_section = THIRDPARTY
known_first_party = small_small_hr
known_django = django
sections = FUTURE,STDLIB,DJANGO,THIRDPARTY,FIRSTPARTY,LOCALFOLDER
sections = FUTURE,STDLIB,DJANGO,THIRDPARTY,FIRST_PARTY,LOCALFOLDER
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 >= 1.11',
'Django >= 2.0.8',
'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,5 +1,5 @@
"""
Main init file for small_small_hr
"""
VERSION = (0, 1, 0)
VERSION = (0, 1, 1)
__version__ = '.'.join(str(v) for v in VERSION)
139 changes: 139 additions & 0 deletions small_small_hr/emails.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
"""
Emails module for scam app
"""
from django.conf import settings
from django.contrib.sites.models import Site
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
from django.utils.translation import ugettext as _


def send_email(
name: str, email: str, subject: str, message: str, obj: object = None):
"""
Sends a generic email
"""
context = {
'name': name,
'subject': subject,
'message': message,
'object': obj,
'SITE': Site.objects.get_current()
}
email_subject = render_to_string(
'small_small_hr/email/generic_email_subject.txt',
context).replace('\n', '')
email_txt_body = render_to_string(
'small_small_hr/email/generic_email_body.txt', context)
email_html_body = render_to_string(
'small_small_hr/email/generic_email_body.html', context
).replace('\n', '')

subject = email_subject
from_email = settings.DEFAULT_FROM_EMAIL
to_email = f'{name} <{email}>'
text_content = email_txt_body
html_content = email_html_body
msg = EmailMultiAlternatives(subject, text_content, from_email, [to_email])
msg.attach_alternative(html_content, "text/html")

return msg.send(fail_silently=True)


def leave_application_email(leave_obj: object):
"""
Sends an email to admins when a leave application is made
"""
msg = getattr(
settings,
'HR_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',
_("New Leave Application"))
admin_emails = getattr(
settings, 'HR_ADMIN_EMAILS', [settings.DEFAULT_FROM_EMAIL])

for admin_email in admin_emails:
send_email(
name=leave_obj.staff.get_name(),
email=admin_email,
subject=subj,
message=msg,
obj=leave_obj
)


def leave_processed_email(leave_obj: object):
"""
Sends an email to admins when a leave application is processed
"""
if leave_obj.staff.user.email:
msg = getattr(
settings, 'HR_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',
_("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
)


def overtime_application_email(overtime_obj: object):
"""
Sends an email to admins when an overtime application is made
"""
msg = getattr(
settings,
'HR_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',
_("New Overtime Application"))
admin_emails = getattr(
settings, 'HR_ADMIN_EMAILS', [settings.DEFAULT_FROM_EMAIL])

for admin_email in admin_emails:
send_email(
name=overtime_obj.staff.get_name(),
email=admin_email,
subject=subj,
message=msg,
obj=overtime_obj
)


def overtime_processed_email(overtime_obj: object):
"""
Sends an email to admins when an overtime application is processed
"""
if overtime_obj.staff.user.email:

msg = getattr(
settings, 'HR_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',
_("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
)
18 changes: 18 additions & 0 deletions small_small_hr/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from phonenumber_field.phonenumber import PhoneNumber
from phonenumber_field.formfields import PhoneNumberField

from small_small_hr.emails import (leave_application_email,
overtime_application_email)
from small_small_hr.models import (TWOPLACES, Leave, OverTime, Role,
StaffDocument, StaffProfile, AnnualLeave)

Expand Down Expand Up @@ -219,6 +221,14 @@ def __init__(self, *args, **kwargs):
)
)

def save(self, commit=True):
"""
Custom save method
"""
overtime = super().save()
overtime_application_email(overtime_obj=overtime)
return overtime


class LeaveForm(forms.ModelForm):
"""
Expand Down Expand Up @@ -391,6 +401,14 @@ def __init__(self, *args, **kwargs):
)
)

def save(self, commit=True):
"""
Custom save method
"""
leave = super().save()
leave_application_email(leave_obj=leave)
return leave


class StaffDocumentForm(forms.ModelForm):
"""
Expand Down
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}}
23 changes: 22 additions & 1 deletion tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"""
Settings for tests
"""
from __future__ import unicode_literals
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

INSTALLED_APPS = [
# core django apps
Expand All @@ -12,6 +14,7 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
# third party
'sorl.thumbnail',
'private_storage',
Expand All @@ -32,6 +35,22 @@
}
}

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

TIME_ZONE = 'Africa/Nairobi'
USE_I18N = True
USE_L10N = True
Expand All @@ -43,6 +62,8 @@
MEDIA_ROOT = '/tmp/'
PRIVATE_STORAGE_AUTH_FUNCTION = 'private_storage.permissions.allow_staff'

SITE_ID = 1

# try and load local_settings if present
try:
# pylint: disable=wildcard-import
Expand Down
Loading

0 comments on commit 197ee3e

Please sign in to comment.