Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

WIP: Adjust hiding of overlapping public holidays and absences #200

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions timed/tracking/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,24 @@ class Meta:

class AbsenceManager(models.Manager):
def get_queryset(self):
from timed.employment.models import PublicHoliday
"""Return only absences which do not overlap with public holidays."""
from timed.employment.models import Employment, PublicHoliday

# location of employment at the date of absence
location_qs = Employment.objects.filter(
(
models.Q(end_date__gte=models.OuterRef("date"))
| models.Q(end_date__isnull=True)
),
start_date__lte=models.OuterRef("date"),
user_id=models.OuterRef(models.OuterRef("user")),
).values("location")[:1]

queryset = super().get_queryset()
queryset = queryset.exclude(
date__in=models.Subquery(
PublicHoliday.objects.filter(
location__employments__user=models.OuterRef("user")
location=models.Subquery(location_qs)
).values("date")
)
)
Expand Down
15 changes: 13 additions & 2 deletions timed/tracking/tests/test_absence.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,25 @@


def test_absence_list_authenticated(auth_client):
absence = AbsenceFactory.create(user=auth_client.user)
# public holidays of previous location may not be hidden
absence = AbsenceFactory.create(
user=auth_client.user, date=datetime.date(2018, 1, 2)
)
previous_employment = EmploymentFactory.create(
user=absence.user,
start_date=datetime.date(2017, 1, 1),
end_date=datetime.date(2017, 12, 31),
)
PublicHolidayFactory.create(
date=absence.date, location=previous_employment.location
)

# overlapping absence with public holidays need to be hidden
overlap_absence = AbsenceFactory.create(
user=auth_client.user, date=datetime.date(2018, 1, 1)
)
employment = EmploymentFactory.create(
user=overlap_absence.user, start_date=datetime.date(2017, 12, 31)
user=overlap_absence.user, start_date=datetime.date(2018, 1, 1)
)
PublicHolidayFactory.create(date=overlap_absence.date, location=employment.location)
url = reverse("absence-list")
Expand Down