From 42d013a52d17f0a7d3a6ad92b8ebd5d9fc973cd9 Mon Sep 17 00:00:00 2001 From: Oliver Sauder Date: Wed, 3 Jan 2018 14:02:46 +0100 Subject: [PATCH] Adjust hiding of overlapping public holidays and absences Determine by location of employment at the date of absence whether a public holiday overlaps it. --- timed/tracking/models.py | 15 +++++++++++++-- timed/tracking/tests/test_absence.py | 15 +++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/timed/tracking/models.py b/timed/tracking/models.py index 2ef078167..3ca497568 100644 --- a/timed/tracking/models.py +++ b/timed/tracking/models.py @@ -151,12 +151,23 @@ 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') )) return queryset diff --git a/timed/tracking/tests/test_absence.py b/timed/tracking/tests/test_absence.py index 817935284..1f00434ae 100644 --- a/timed/tracking/tests/test_absence.py +++ b/timed/tracking/tests/test_absence.py @@ -9,14 +9,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) + start_date=datetime.date(2018, 1, 1) ) PublicHolidayFactory.create( date=overlap_absence.date, location=employment.location