Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exclude weekend days from the checkin search offset #406

Merged
merged 1 commit into from
Apr 22, 2024
Merged
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
12 changes: 6 additions & 6 deletions compass/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from compass.dao.person import get_appuser_by_uwnetid, PersonNotFoundException
from compass.dao.group import is_group_member
from compass.dao import current_datetime
from datetime import datetime, timedelta, timezone
from compass.utils import weekdays_before


class AppUserManager(models.Manager):
Expand Down Expand Up @@ -227,13 +227,13 @@ def has_student_role(self, request):


class ContactManager(models.Manager):
def by_adviser(self, adviser_uwnetid, offset_hours=72):
def by_adviser(self, adviser_uwnetid, from_days=3):
# Only return contacts from the checkin system, not manual contacts
kwargs = {'app_user__uwnetid': adviser_uwnetid, 'source': 'Checkin'}

if offset_hours is not None and offset_hours > 0:
cutoffdt = current_datetime() - timedelta(hours=offset_hours)
kwargs['checkin_date__gte'] = cutoffdt.replace(tzinfo=timezone.utc)
if from_days is not None and from_days > 0:
start_date = weekdays_before(current_datetime().date(), from_days)
kwargs['checkin_date__gte'] = start_date

return super().get_queryset().filter(**kwargs).values(
'student__system_key',
Expand All @@ -242,7 +242,7 @@ def by_adviser(self, adviser_uwnetid, offset_hours=72):
'source',
'trans_id',
checkin_date_str=Cast('checkin_date', models.TextField())
).order_by('-checkin_date')
).order_by('checkin_date')


class Contact(models.Model):
Expand Down
37 changes: 37 additions & 0 deletions compass/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright 2024 UW-IT, University of Washington
# SPDX-License-Identifier: Apache-2.0

from django.test import TestCase
from compass.utils import weekdays_before
from datetime import date


class TestUtils(TestCase):
def test_weekdays_before(self):
val = weekdays_before(date(2024, 4, 19), offset_days=3)
self.assertEqual(str(val), '2024-04-16', 'Friday, offset=3')

val = weekdays_before(date(2024, 4, 20), offset_days=3)
self.assertEqual(str(val), '2024-04-17', 'Saturday, offset=3')

val = weekdays_before(date(2024, 4, 21), offset_days=3)
self.assertEqual(str(val), '2024-04-17', 'Sunday, offset=3')

val = weekdays_before(date(2024, 4, 22), offset_days=1)
self.assertEqual(str(val), '2024-04-19', 'Monday, offset=1')

val = weekdays_before(date(2024, 4, 22), offset_days=2)
self.assertEqual(str(val), '2024-04-18', 'Monday, offset=2')

val = weekdays_before(date(2024, 4, 22), offset_days=3)
self.assertEqual(str(val), '2024-04-17', 'Monday, offset=3')

# Bad offsets
val = weekdays_before(date(2024, 4, 22), offset_days=None)
self.assertEqual(str(val), '2024-04-22', 'Monday, offset=None')

val = weekdays_before(date(2024, 4, 22), offset_days=-5)
self.assertEqual(str(val), '2024-04-22', 'Monday, offset=-5')

val = weekdays_before(date(2024, 4, 22), offset_days=0)
self.assertEqual(str(val), '2024-04-22', 'Monday, offset=0')
2 changes: 1 addition & 1 deletion compass/tests/views/api/test_adviser.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def setUp(self):
def test_get_contacts(self):
d1 = datetime(2023, 5, 10, 2, 30, 30, 300, tzinfo=timezone.utc)
d2 = datetime(2023, 5, 7, 12, 1, 30, 300, tzinfo=timezone.utc)
d3 = datetime(2023, 5, 7, 12, 0, 00, 100, tzinfo=timezone.utc)
d3 = datetime(2023, 5, 4, 9, 0, 00, 100, tzinfo=timezone.utc)

for date in [d1, d2, d3]:
contact = Contact(app_user=self.app_user,
Expand Down
23 changes: 23 additions & 0 deletions compass/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2024 UW-IT, University of Washington
# SPDX-License-Identifier: Apache-2.0

from datetime import date, timedelta


def weekdays_before(end_date, offset_days=3):
"""
Adjust a start_date to exclude weekend days from the passed offset_days.
"""
start_date = end_date
if offset_days is not None and offset_days > 0:
start_date = end_date - timedelta(days=offset_days)

day = start_date
actual_offset = 1 if (day.weekday() == 6) else 0
while day < end_date:
actual_offset += 1
if day.weekday() >= 5:
actual_offset += 1
day = day + timedelta(days=1)

return end_date - timedelta(days=actual_offset)
Loading