mirror of https://github.com/snachodog/mybuddy.git
Add a total sleep on date card.
This commit is contained in:
parent
944d33390d
commit
f89a8ce289
|
@ -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 %}
|
|
@ -35,6 +35,7 @@
|
|||
</div>
|
||||
<div class="col-lg-4 col-md-6 col-sm-12">
|
||||
{% card_sleep_last object %}
|
||||
{% card_sleep_day object %}
|
||||
{% card_tummytime_day object %}
|
||||
</div>
|
||||
<div class="col-lg-4 col-md-6 col-sm-12">
|
||||
|
|
|
@ -98,3 +98,31 @@ def card_sleep_last(child):
|
|||
"""
|
||||
instance = Sleep.objects.filter(child=child).order_by('-end').first()
|
||||
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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue