From 66a91fe534000cf0bc110bb9f1fdfa1ff936070f Mon Sep 17 00:00:00 2001 From: Christopher Charbonneau Wells Date: Mon, 23 Oct 2017 19:52:20 -0400 Subject: [PATCH] Add a "naps" card. --- dashboard/templates/cards/sleep_naps_day.html | 22 +++++++++++++++++ dashboard/templates/dashboard/child.html | 1 + dashboard/templatetags/cards.py | 24 +++++++++++++++---- 3 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 dashboard/templates/cards/sleep_naps_day.html diff --git a/dashboard/templates/cards/sleep_naps_day.html b/dashboard/templates/cards/sleep_naps_day.html new file mode 100644 index 00000000..887e043c --- /dev/null +++ b/dashboard/templates/cards/sleep_naps_day.html @@ -0,0 +1,22 @@ +{% extends 'cards/sleep.html' %} +{% load duration %} + +{% block header %}Today's Naps{% endblock %} + +{% block title %} + {% if count %} + {{ count }} naps + {% else %} + + None yet today + + {% endif %} +{% endblock %} + +{% block content %} +
+ {{ total.duration__sum|duration_string }} +
+{% endblock %} + +{% block footer %}Naps are sleep entries starting 7AM - 7PM.{% endblock %} \ No newline at end of file diff --git a/dashboard/templates/dashboard/child.html b/dashboard/templates/dashboard/child.html index 4f3ceee8..68c70c53 100644 --- a/dashboard/templates/dashboard/child.html +++ b/dashboard/templates/dashboard/child.html @@ -19,6 +19,7 @@
{% card_sleep_last object %} {% card_sleep_day object %} + {% card_sleep_naps_day object %} {% card_tummytime_day object %}
diff --git a/dashboard/templatetags/cards.py b/dashboard/templatetags/cards.py index 286502b5..70d55e9b 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.db.models import Sum from django.utils import timezone from core.models import DiaperChange, Feeding, Sleep, Timer, TummyTime @@ -103,12 +104,25 @@ def card_sleep_day(child, date=None): total += end - start count = len(instances) - if count > 0: - average = total/count - else: - average = 0 - return {'total': total, 'count': count, 'average': average} + return {'total': total, 'count': count} + + +@register.inclusion_tag('cards/sleep_naps_day.html') +def card_sleep_naps_day(child, date=None): + """Nap information for the current day. + """ + local = timezone.localtime(date) + start_lower = local.replace( + hour=7, minute=0, second=0).astimezone(timezone.utc) + start_upper = local.replace( + hour=19, minute=0, second=0).astimezone(timezone.utc) + instances = Sleep.objects.filter(child=child) \ + .filter(start__gte=start_lower, start__lte=start_upper) + return { + 'total': instances.aggregate(Sum('duration')), + 'count': len(instances) + } @register.inclusion_tag('cards/timer_list.html')