Change Diaper Change Types card to not care about dates (or timezones) for the data dict.

This commit is contained in:
Christopher Charbonneau Wells 2017-09-07 20:08:28 -04:00
parent 4b2e08e0fb
commit 422b381e50
2 changed files with 24 additions and 18 deletions

View File

@ -7,7 +7,7 @@
{% endblock %} {% endblock %}
{% block content %} {% block content %}
{% for date, info in stats.items %} {% for key, info in stats.items %}
<div class="progress mt-3"> <div class="progress mt-3">
{% if info.wet_pct > 0 %} {% if info.wet_pct > 0 %}
@ -24,7 +24,13 @@
{% endif %} {% endif %}
</div> </div>
<div class="text-center text-dark small"> <div class="text-center text-dark small">
{{ date|date:'D' }} {% if key == 0 %}
today
{% elif key == 1 %}
yesterday
{% else %}
{{ key }} days ago
{% endif %}
</div> </div>
{% endfor %} {% endfor %}
{% endblock %} {% endblock %}

View File

@ -1,10 +1,8 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals
from collections import OrderedDict
from django import template from django import template
from django.utils import timezone from django.utils import formats, timezone
from core.models import DiaperChange, Feeding, Sleep, TummyTime from core.models import DiaperChange, Feeding, Sleep, TummyTime
@ -41,27 +39,29 @@ def card_diaperchange_last(child):
def card_diaperchange_types(child): def card_diaperchange_types(child):
"""Diaper change statistics for the last seven days including today. """Diaper change statistics for the last seven days including today.
""" """
stats = OrderedDict() stats = {}
for x in range(0, 7): max_date = (timezone.localtime() + timezone.timedelta(
date = (timezone.localtime() - timezone.timedelta(days=x)).date() days=1)).replace(hour=0, minute=0, second=0)
stats[date] = {'wet': 0, 'solid': 0} min_date = (max_date - timezone.timedelta(
days=7)).replace(hour=0, minute=0, second=0)
for x in range(6):
stats[x] = {'wet': 0, 'solid': 0}
instances = DiaperChange.objects.filter(child=child)\ instances = DiaperChange.objects.filter(child=child)\
.filter(time__gt=list(stats.keys())[-1])\ .filter(time__gt=min_date).filter(time__lt=max_date).order_by('-time')
.filter(time__lt=timezone.localtime())\
.order_by('-time')
for instance in instances: for instance in instances:
date = instance.time.date() key = (max_date - instance.time).days
if instance.wet: if instance.wet:
stats[date]['wet'] += 1 stats[key]['wet'] += 1
if instance.solid: if instance.solid:
stats[date]['solid'] += 1 stats[key]['solid'] += 1
for date, info in stats.items(): for key, info in stats.items():
total = info['wet'] + info['solid'] total = info['wet'] + info['solid']
if total > 0: if total > 0:
stats[date]['wet_pct'] = info['wet'] / total * 100 stats[key]['wet_pct'] = info['wet'] / total * 100
stats[date]['solid_pct'] = info['solid'] / total * 100 stats[key]['solid_pct'] = info['solid'] / total * 100
return {'stats': stats, 'last_change': instances.first()} return {'stats': stats, 'last_change': instances.first()}