Skip to content

Commit

Permalink
Resolves #850
Browse files Browse the repository at this point in the history
  • Loading branch information
k1o0 committed May 7, 2024
1 parent aadfa12 commit 1ce7672
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
36 changes: 31 additions & 5 deletions alyx/actions/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,20 @@ def check_weighed(subject, date=None):
# Reinit the water_control instance to make sure the just-added
# weighing is taken into account
wc = subject.reinit_water_control()
if not wc:
if not wc or not wc.is_water_restricted(date):
return

assert hasattr(date, 'date')
ref_weight = wc.reference_weight(date)
is_restriction_day = wc.water_restriction_at(date).date() == date.date()
# Don't notifiy if a reference weight was entered and subject
# was put on water restriction on the same day
if is_restriction_day and ref_weight:
return

lwb = wc.last_weighing_before(date=date)
if hasattr(date, 'date'):
date = date.date()
date = date.date()

datetime = lwb[0] if lwb else None
if not datetime or datetime.date() != date:
header = 'ATTENTION'
Expand All @@ -50,13 +59,30 @@ def check_weighed(subject, date=None):


def check_water_administration(subject, date=None):
"""
Check the subject was administered water in the last 24 hours.
Creates a notification if the subject was not given required water
today.
Parameters
----------
subject : subject.models.Subject
A subject instance.
date : datetime.datetime
The datetime to check, deafults to now.
"""
date = date or timezone.now()
wc = subject.reinit_water_control()
if not wc or not wc.is_water_restricted(date):
return
remaining = wc.remaining_water(date=date)
wa = wc.last_water_administration_at(date=date)
if not wa:
# If the subject is not on water restriction, or the restriction
# was created on the same day, water administration is not required
if wc.water_restriction_at(date).date() == date.date():
return
delay = date - wa[0]
delay = date - (wa[0] if wa else wc.current_water_restriction())
# Notification if water needs to be given more than 23h after the last
# water administration.
if remaining > 0 and delay.total_seconds() >= 23 * 3600 - 10:
Expand Down
10 changes: 9 additions & 1 deletion alyx/actions/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def test_notif_water_1(self):
date = timezone.datetime(2018, 6, 3, 16, 0, 0)
check_water_administration(self.subject, date=date)
notif = Notification.objects.last()
self.assertTrue(notif is None)
self.assertIsNone(notif)

def test_notif_water_2(self):
# If the last water admin was on June 3 at 12pm, the notification
Expand All @@ -182,6 +182,14 @@ def test_notif_water_2(self):
notif = Notification.objects.last()
self.assertTrue((notif is not None) is r)

def test_notif_water_3(self):
# If the subject was place on water restriction on the same day
# there should be no notification
date = timezone.datetime(2018, 6, 2, 22, 30, 0)
check_water_administration(self.subject, date=date)
notif = Notification.objects.last()
self.assertIsNone(notif)

def test_notif_user_change_1(self):
self.subject.responsible_user = self.user2
self.subject.save()
Expand Down

0 comments on commit 1ce7672

Please sign in to comment.