Fix zero division error when a day has no sleep entries.

This commit is contained in:
Christopher Charbonneau Wells 2017-09-15 07:09:43 -04:00
parent 6ec4307173
commit 0ae9c5a408
2 changed files with 12 additions and 8 deletions

View File

@ -14,9 +14,11 @@
{% endblock %}
{% block content %}
<div class="text-muted">
Average sleep duration: <strong>{{ average|duration_string }}</strong>.
</div>
{% if total %}
<div class="text-muted">
Average sleep duration: <strong>{{ average|duration_string }}</strong>.
</div>
{% endif %}
{% endblock %}
{% block footer %}{{ count }} sleep entries{% endblock %}

View File

@ -121,8 +121,10 @@ def card_sleep_day(child, date=None):
total += end - start
return {
'total': total,
'count': len(instances),
'average': total/len(instances)
}
count = len(instances)
if count > 0:
average = total/count
else:
average = 0
return {'total': total, 'count': count, 'average': average}