Make "nap" bounds configurable with a 6AM - 6PM default.

This commit is contained in:
Christopher Charbonneau Wells 2017-11-04 07:59:28 -04:00
parent c6c603d592
commit f6dd38a891
6 changed files with 39 additions and 7 deletions

View File

@ -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'
}

View File

@ -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',)

View File

@ -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

View File

@ -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 @@
<th>Duration</th>
<th>Start</th>
<th>End</th>
<th class="text-center">Nap</th>
<th class="text-center">Actions</th>
</tr>
</thead>
@ -29,6 +29,7 @@
<td>{{ sleep.duration|duration_string }}</td>
<td>{{ sleep.start|date:'n/j/y G:i' }}</td>
<td>{{ sleep.end|date:'n/j/y G:i' }}</td>
<td class="text-center">{{ sleep.nap|bool_icon }}</td>
<td class="text-center">
<div class="btn-group btn-group-sm" role="group" aria-label="Actions">

View File

@ -1,7 +1,7 @@
{% extends 'cards/base.html' %}
{% load duration %}
{% block header %}Today's Naps <small><em>(7AM - 7PM)</em></small>{% endblock %}
{% block header %}Today's Naps{% endblock %}
{% block title %}
{% if count %}

View File

@ -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)}