Skip to content

Commit

Permalink
Merge pull request #41 from moshthepitt/day-count
Browse files Browse the repository at this point in the history
Implement better leave day count
  • Loading branch information
moshthepitt authored Oct 15, 2020
2 parents 21b410b + c9a4efc commit cecfe4f
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 13 deletions.
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, 3, 0)
VERSION = (0, 4, 0)
__version__ = ".".join(str(v) for v in VERSION)
# pylint: disable=invalid-name
default_app_config = "small_small_hr.apps.SmallSmallHrConfig" # noqa
27 changes: 27 additions & 0 deletions small_small_hr/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,15 @@ def duration(self):
"""Get duration as a property."""
return self.get_duration()

@cached_property
def day_count(self):
"""
Get the number of leave days as a property.
This takes into account holidays and weekend policy.
"""
return get_real_leave_duration(leave_obj=self)


class OverTime(BaseStaffRequest):
"""Overtime model class."""
Expand Down Expand Up @@ -507,6 +516,24 @@ def get_days(start: object, end: object):
yield local_start.date() + timedelta(days=i)


def get_real_leave_duration(leave_obj: Leave) -> Decimal:
"""
Get the real leave duration.
Takes into account public holidays, weekends and weekend policy
"""
count = Decimal(0)
free_days = FreeDay.objects.filter(
date__gte=leave_obj.start.date(), date__lte=leave_obj.end.date()
).values_list("date", flat=True)
days = get_days(start=leave_obj.start, end=leave_obj.end)
for day in days:
if day not in free_days:
day_value = settings.SSHR_DAY_LEAVE_VALUES[day.isoweekday()]
count = count + Decimal(day_value)
return count


def get_taken_leave_days(
staffprofile: object, status: str, leave_type: str, start_year: int, end_year: int
):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{ object.content_object.staff.get_name }} requested time off:<br /><br />
{{ object.content_object.duration.days }} days of {{ object.content_object.get_leave_type_display}}<br />
{{ object.content_object.day_count }} days of {{ object.content_object.get_leave_type_display}}<br />
{{ object.content_object.start|date:"D, d M Y" }} - {{ object.content_object.end|date:"D, d M Y" }}<br />
Available Balance: {{ object.content_object.staff.get_available_leave_days|floatformat:2 }} days<br /><br />
Please log in to process the above: http://{{SITE.domain}}/reviews/{{ object.pk }}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{ object.content_object.staff.get_name }} requested time off:

{{ object.content_object.duration.days }} days of {{ object.content_object.get_leave_type_display}}
{{ object.content_object.day_count }} days of {{ object.content_object.get_leave_type_display}}
{{ object.content_object.start|date:"D, d M Y" }} - {{ object.content_object.end|date:"D, d M Y" }}
Available Balance: {{ object.content_object.staff.get_available_leave_days|floatformat:2 }} days

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{{ object.content_object.staff.get_name }},<br /><br />
Your time off request for {{ object.content_object.duration.days }} days of {{ object.content_object.get_leave_type_display }} from {{ object.content_object.start|date:"d M" }} - {{ object.content_object.end|date:"d M" }} has been {{ object.content_object.get_review_status_display|lower }}.<br /><br />
Your time off request for {{ object.content_object.day_count }} days of {{ object.content_object.get_leave_type_display }} from {{ object.content_object.start|date:"d M" }} - {{ object.content_object.end|date:"d M" }} has been {{ object.content_object.get_review_status_display|lower }}.<br /><br />
{{ object.content_object.start|date:"D, d M Y" }} - {{ object.content_object.end|date:"D, d M Y" }}<br />
{{ object.content_object.get_leave_type_display}}<br />
{{ object.content_object.duration.days }} days<br />
{{ object.content_object.day_count }} days<br />
Status: {{ object.content_object.get_review_status_display }}
<br/><br/>
Thank you,<br/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{{ object.content_object.staff.get_name }},

Your time off request for {{ object.content_object.duration.days }} days of {{ object.content_object.get_leave_type_display }} from {{ object.content_object.start|date:"d M" }} - {{ object.content_object.end|date:"d M" }} has been {{ object.content_object.get_review_status_display|lower }}.
Your time off request for {{ object.content_object.day_count }} days of {{ object.content_object.get_leave_type_display }} from {{ object.content_object.start|date:"d M" }} - {{ object.content_object.end|date:"d M" }} has been {{ object.content_object.get_review_status_display|lower }}.

{{ object.content_object.start|date:"D, d M Y" }} - {{ object.content_object.end|date:"D, d M Y" }}
{{ object.content_object.get_leave_type_display}}
{{ object.content_object.duration.days }} days
{{ object.content_object.day_count }} days
Status: {{ object.content_object.get_review_status_display }}

Thank you,
Expand Down
1 change: 1 addition & 0 deletions tests/test_emails.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Module to test small_small_hr Emails."""
# pylint: disable=hard-coded-auth-user
from datetime import datetime

from django.conf import settings
Expand Down
22 changes: 16 additions & 6 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Module to test small_small_hr models."""
# pylint: disable=too-many-lines
# pylint: disable=too-many-lines,hard-coded-auth-user
import os
from datetime import date, datetime, timedelta

Expand All @@ -11,6 +11,7 @@

import pytz
from model_mommy import mommy
from model_mommy.recipe import Recipe
from model_reviews.models import ModelReview

from small_small_hr.forms import (
Expand Down Expand Up @@ -39,6 +40,17 @@ class TestForms(TestCase): # pylint: disable=too-many-public-methods
def setUp(self):
"""Set up test class."""
self.factory = RequestFactory()
StaffProfile.objects.rebuild()
self.manager = mommy.make(
"auth.User", first_name="Jane", last_name="Ndoe", email="[email protected]"
)
self.user = mommy.make(
"auth.User", first_name="Bob", last_name="Ndoe", email="[email protected]"
)
manager_mommy = Recipe(StaffProfile, lft=None, rght=None, user=self.manager)
staff_mommy = Recipe(StaffProfile, lft=None, rght=None, user=self.user)
self.manager_profile = manager_mommy.make()
self.staffprofile = staff_mommy.make()

def test_annual_leave_form(self):
"""Test AnnualLeaveForm."""
Expand Down Expand Up @@ -1266,11 +1278,9 @@ def test_staff_profile_admin_create_form(self):

def test_staff_profile_admin_form(self):
"""Test StaffProfileAdminForm."""
manager = mommy.make("auth.User", username="manager")
managerprofile = mommy.make("small_small_hr.StaffProfile", user=manager)

user = mommy.make("auth.User")
staffprofile = mommy.make("small_small_hr.StaffProfile", user=user)
managerprofile = self.manager_profile
user = self.user
staffprofile = self.staffprofile

request = self.factory.get("/")
request.session = {}
Expand Down
43 changes: 43 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from small_small_hr.models import FreeDay, Leave, get_taken_leave_days
from small_small_hr.utils import create_free_days

# pylint: disable=hard-coded-auth-user


class TestModels(TestCase):
"""Test class for models."""
Expand Down Expand Up @@ -41,6 +43,47 @@ def test_freedays_str(self):
).__str__(),
)

@override_settings(
SSHR_DAY_LEAVE_VALUES={
1: 1, # Monday
2: 1, # Tuesday
3: 1, # Wednesday
4: 1, # Thursday
5: 1, # Friday
6: 0.5, # Saturday
7: 0, # Sunday
}
)
def test_leave_day_count(self):
"""Test leave object day_count."""
user = mommy.make("auth.User", first_name="Mosh", last_name="Pitt")
staff = mommy.make("small_small_hr.StaffProfile", user=user)
mommy.make(
"small_small_hr.AnnualLeave",
staff=staff,
year=2017,
leave_type=Leave.REGULAR,
allowed_days=21,
)
mommy.make(
"small_small_hr.FreeDay",
name="RANDOM HOLIDAY",
date=date(day=15, month=6, year=2017),
)
# 9.5 days of leave ==> Sun not counted, Sat = 0.5 and 15/6/2017 is holiday
start = datetime(2017, 6, 5, 0, 0, 0, tzinfo=pytz.timezone(settings.TIME_ZONE))
end = datetime(2017, 6, 16, 0, 0, 0, tzinfo=pytz.timezone(settings.TIME_ZONE))
leave_obj = mommy.make(
"small_small_hr.Leave",
staff=staff,
start=start,
end=end,
leave_type=Leave.REGULAR,
review_status=Leave.APPROVED,
)

self.assertEqual(9.5, leave_obj.day_count)

@override_settings(
SSHR_DAY_LEAVE_VALUES={
1: 1, # Monday
Expand Down

0 comments on commit cecfe4f

Please sign in to comment.