Enforce min and max nap start parameters

Fixes #644
This commit is contained in:
Christopher C. Wells 2023-06-04 17:39:08 -07:00
parent ac3a6fd973
commit 8abb6d749e
4 changed files with 53 additions and 27 deletions

View File

@ -5,11 +5,20 @@ from django.utils.translation import gettext_lazy as _
import dbsettings
from core.fields import NapStartMaxTimeField, NapStartMinTimeField
from .widgets import TimeInput
class NapStartMaxTimeValue(dbsettings.TimeValue):
field = NapStartMaxTimeField
class NapStartMinTimeValue(dbsettings.TimeValue):
field = NapStartMinTimeField
class NapSettings(dbsettings.Group):
nap_start_min = dbsettings.TimeValue(
nap_start_min = NapStartMinTimeValue(
default=time(6),
description=_("Default minimum nap start time"),
help_text=_(
@ -18,7 +27,7 @@ class NapSettings(dbsettings.Group):
),
widget=TimeInput,
)
nap_start_max = dbsettings.TimeValue(
nap_start_max = NapStartMaxTimeValue(
default=time(18),
description=_("Default maximum nap start time"),
help_text=_(

31
core/fields.py Normal file
View File

@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
from django import forms
from django.utils.translation import gettext as _
class NapStartMaxTimeField(forms.TimeField):
def validate(self, value):
from core.models import Sleep
if value < Sleep.settings.nap_start_min:
raise forms.ValidationError(
_(
"Nap start max. value %(max)s must be greater than nap start min. value %(min)s."
),
code="invalid_nap_start_max",
params={"max": value, "min": Sleep.settings.nap_start_min},
)
class NapStartMinTimeField(forms.TimeField):
def validate(self, value):
from core.models import Sleep
if value > Sleep.settings.nap_start_max:
raise forms.ValidationError(
_(
"Nap start min. value %(min)s must be less than nap start min. value %(max)s."
),
code="invalid_nap_start_min",
params={"min": value, "max": Sleep.settings.nap_start_max},
)

View File

@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
import datetime
from django.contrib.auth import get_user_model
from django.core.management import call_command
from django.test import TestCase
@ -546,22 +548,14 @@ class SleepFormsTestCase(FormsTestCaseBase):
self.assertContains(page, "Sleep entry deleted")
def test_nap_default(self):
models.Sleep.settings.nap_start_min = (
timezone.now() - timezone.timedelta(hours=1)
).time()
models.Sleep.settings.nap_start_max = (
timezone.now() + timezone.timedelta(hours=1)
).time()
models.Sleep.settings.nap_start_min = datetime.time(0, 0, 0)
models.Sleep.settings.nap_start_max = datetime.time(23, 59, 59)
response = self.c.get("/sleep/add/")
self.assertTrue(response.context["form"].initial["nap"])
def test_not_nap_default(self):
models.Sleep.settings.nap_start_min = (
timezone.now() + timezone.timedelta(hours=1)
).time()
models.Sleep.settings.nap_start_max = (
timezone.now() + timezone.timedelta(hours=2)
).time()
models.Sleep.settings.nap_start_min = datetime.time(0, 0, 0)
models.Sleep.settings.nap_start_max = datetime.time(0, 0, 0)
response = self.c.get("/sleep/add/")
self.assertFalse(response.context["form"].initial["nap"])

View File

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from datetime import datetime
import datetime
from django.contrib.auth import get_user_model
from django.core.management import call_command
@ -213,12 +213,8 @@ class SleepTestCase(TestCase):
self.assertEqual(sleep.duration, sleep.end - sleep.start)
def test_sleep_nap(self):
models.Sleep.settings.nap_start_min = (
timezone.now() - timezone.timedelta(hours=1)
).time()
models.Sleep.settings.nap_start_max = (
timezone.now() + timezone.timedelta(hours=1)
).time()
models.Sleep.settings.nap_start_min = datetime.time(0, 0, 0)
models.Sleep.settings.nap_start_max = datetime.time(23, 59, 59)
sleep = models.Sleep.objects.create(
child=self.child,
start=timezone.now(),
@ -227,12 +223,8 @@ class SleepTestCase(TestCase):
self.assertTrue(sleep.nap)
def test_sleep_not_nap(self):
models.Sleep.settings.nap_start_min = (
timezone.now() + timezone.timedelta(hours=1)
).time()
models.Sleep.settings.nap_start_max = (
timezone.now() + timezone.timedelta(hours=2)
).time()
models.Sleep.settings.nap_start_min = datetime.time(0, 0, 0)
models.Sleep.settings.nap_start_max = datetime.time(0, 0, 0)
sleep = models.Sleep.objects.create(
child=self.child,
start=timezone.now(),