From f6dd38a891d97419d878156937b3fe4d42dba9d1 Mon Sep 17 00:00:00 2001 From: Christopher Charbonneau Wells Date: Sat, 4 Nov 2017 07:59:28 -0400 Subject: [PATCH] Make "nap" bounds configurable with a 6AM - 6PM default. --- babybuddy/settings/base.py | 9 +++++++++ core/admin.py | 2 +- core/models.py | 10 ++++++++++ core/templates/core/sleep_list.html | 5 +++-- dashboard/templates/cards/sleep_naps_day.html | 2 +- dashboard/templatetags/cards.py | 18 +++++++++++++++--- 6 files changed, 39 insertions(+), 7 deletions(-) diff --git a/babybuddy/settings/base.py b/babybuddy/settings/base.py index 5f365f0f..fa6e3ac9 100644 --- a/babybuddy/settings/base.py +++ b/babybuddy/settings/base.py @@ -144,3 +144,12 @@ REST_FRAMEWORK = { 'rest_framework.pagination.LimitOffsetPagination', 'PAGE_SIZE': 100 } + +# Baby Buddy configuration + +BABY_BUDDY = { + # A sleep entry with a start time between NAP_START_MIN and NAP_START_MAX + # (in the current TZ) will be categorized as a nap. Use the format %H:%M. + 'NAP_START_MIN': '06:00', + 'NAP_START_MAX': '18:00' +} diff --git a/core/admin.py b/core/admin.py index 544ea2a2..ed182d64 100644 --- a/core/admin.py +++ b/core/admin.py @@ -39,7 +39,7 @@ class NoteAdmin(admin.ModelAdmin): @admin.register(Sleep) class SleepAdmin(admin.ModelAdmin): - list_display = ('start', 'end', 'duration', 'child',) + list_display = ('start', 'end', 'duration', 'child', 'nap') list_filter = ('child',) search_fields = ('child__first_name', 'child__last_name',) diff --git a/core/models.py b/core/models.py index f4b0024f..aac813c2 100644 --- a/core/models.py +++ b/core/models.py @@ -3,6 +3,7 @@ from __future__ import unicode_literals from datetime import timedelta +from django.conf import settings from django.core.exceptions import ValidationError from django.db import models from django.template.defaultfilters import slugify @@ -190,6 +191,15 @@ class Sleep(models.Model): def __str__(self): return 'Sleep' + def nap(self): + # TODO: Add a way to filter naps from Sleep.objects() easily. + nap_start_min = timezone.datetime.strptime( + settings.BABY_BUDDY['NAP_START_MIN'], '%H:%M').time() + nap_start_max = timezone.datetime.strptime( + settings.BABY_BUDDY['NAP_START_MAX'], '%H:%M').time() + local_start_time = timezone.localtime(self.start).time() + return nap_start_min <= local_start_time <= nap_start_max + def save(self, *args, **kwargs): if self.start and self.end: self.duration = self.end - self.start diff --git a/core/templates/core/sleep_list.html b/core/templates/core/sleep_list.html index be1f7920..a5757472 100644 --- a/core/templates/core/sleep_list.html +++ b/core/templates/core/sleep_list.html @@ -1,6 +1,5 @@ {% extends 'babybuddy/page.html' %} -{% load widget_tweaks %} -{% load duration %} +{% load bootstrap duration widget_tweaks %} {% block title %}Sleep{% endblock %} @@ -19,6 +18,7 @@ Duration Start End + Nap Actions @@ -29,6 +29,7 @@ {{ sleep.duration|duration_string }} {{ sleep.start|date:'n/j/y G:i' }} {{ sleep.end|date:'n/j/y G:i' }} + {{ sleep.nap|bool_icon }}
diff --git a/dashboard/templates/cards/sleep_naps_day.html b/dashboard/templates/cards/sleep_naps_day.html index 44b0dc0c..4dbbac0b 100644 --- a/dashboard/templates/cards/sleep_naps_day.html +++ b/dashboard/templates/cards/sleep_naps_day.html @@ -1,7 +1,7 @@ {% extends 'cards/base.html' %} {% load duration %} -{% block header %}Today's Naps (7AM - 7PM){% endblock %} +{% block header %}Today's Naps{% endblock %} {% block title %} {% if count %} diff --git a/dashboard/templatetags/cards.py b/dashboard/templatetags/cards.py index d07254da..9a3c5276 100644 --- a/dashboard/templatetags/cards.py +++ b/dashboard/templatetags/cards.py @@ -2,6 +2,7 @@ from __future__ import unicode_literals from django import template +from django.conf import settings from django.db.models import Sum from django.utils import timezone @@ -196,17 +197,28 @@ def card_sleep_naps_day(child, date=None): specific date. :param child: an instance of the Child model. :param date: a Date object for the day to filter. - :returns: a dictionary of nap data statistics. + :returns: a dictionary of nap data statistics and the nap bounds. """ + nap_start_min = timezone.datetime.strptime( + settings.BABY_BUDDY['NAP_START_MIN'], '%H:%M').time() + nap_start_max = timezone.datetime.strptime( + settings.BABY_BUDDY['NAP_START_MAX'], '%H:%M').time() + local = timezone.localtime(date) start_lower = local.replace( - hour=7, minute=0, second=0).astimezone(timezone.utc) + hour=nap_start_min.hour, + minute=nap_start_min.minute, + second=0).astimezone(timezone.utc) start_upper = local.replace( - hour=19, minute=0, second=0).astimezone(timezone.utc) + hour=nap_start_max.hour, + minute=nap_start_max.minute, + second=0).astimezone(timezone.utc) instances = Sleep.objects.filter(child=child) \ .filter(start__gte=start_lower, start__lte=start_upper) return { 'type': 'sleep', + 'min_time': nap_start_min, + 'max_time': nap_start_max, 'total': instances.aggregate(Sum('duration')), 'count': len(instances)}