Add a total sleep on date card.

This commit is contained in:
Christopher Charbonneau Wells 2017-09-13 10:31:56 -04:00
parent 944d33390d
commit f89a8ce289
3 changed files with 51 additions and 0 deletions

View File

@ -0,0 +1,22 @@
{% extends 'cards/sleep.html' %}
{% load duration %}
{% block header %}Today's Sleep{% endblock %}
{% block title %}
{% if total %}
<strong>{{ total|duration_string }}</strong>
{% else %}
<i class="fa fa-frown-o" aria-hidden="true"></i>
<strong>None yet today</strong>
<i class="fa fa-frown-o" aria-hidden="true"></i>
{% endif %}
{% endblock %}
{% block content %}
<div class="text-muted">
Average sleep duration: <strong>{{ average|duration_string }}</strong>.
</div>
{% endblock %}
{% block footer %}{{ count }} sleep entries{% endblock %}

View File

@ -35,6 +35,7 @@
</div> </div>
<div class="col-lg-4 col-md-6 col-sm-12"> <div class="col-lg-4 col-md-6 col-sm-12">
{% card_sleep_last object %} {% card_sleep_last object %}
{% card_sleep_day object %}
{% card_tummytime_day object %} {% card_tummytime_day object %}
</div> </div>
<div class="col-lg-4 col-md-6 col-sm-12"> <div class="col-lg-4 col-md-6 col-sm-12">

View File

@ -98,3 +98,31 @@ def card_sleep_last(child):
""" """
instance = Sleep.objects.filter(child=child).order_by('-end').first() instance = Sleep.objects.filter(child=child).order_by('-end').first()
return {'sleep': instance} return {'sleep': instance}
@register.inclusion_tag('cards/sleep_day.html')
def card_sleep_day(child, date=None):
"""Total sleep time for a child on the current day.
"""
if not date:
date = timezone.localtime().date()
instances = Sleep.objects.filter(child=child).filter(start__day=date.day) \
| Sleep.objects.filter(child=child).filter(end__day=date.day)
total = timezone.timedelta(seconds=0)
for instance in instances:
start = timezone.localtime(instance.start)
end = timezone.localtime(instance.end)
# Account for dates crossing midnight.
if start.date() != date:
start = start.replace(day=end.day, hour=0, minute=0, second=0)
elif end.date() != date:
end = start.replace(day=start.day, hour=23, minute=59, second=59)
total += end - start
return {
'total': total,
'count': len(instances),
'average': total/len(instances)
}