Add empty diapers support to dashboard card

This commit is contained in:
Christopher C. Wells 2022-04-15 20:50:59 -07:00 committed by Christopher Charbonneau Wells
parent 4916a810e0
commit 81ecff0296
3 changed files with 76 additions and 13 deletions

View File

@ -17,20 +17,31 @@
{% block content %}
{% for key, info in stats.items %}
{% if info.wet > 0 or info.solid > 0 %}
{% if info.wet > 0 or info.solid > 0 or info.empty > 0 %}
<div class="progress mt-3">
{% if info.wet_pct > 0 %}
<div class="progress-bar bg-primary lead"
role="progressbar"
style="width: {{ info.wet_pct|safe }}%;">{{ info.wet|floatformat:'0' }}&nbsp;{% trans "wet" %}</div>
style="width: {{ info.wet_pct|safe }}%;">
{{ info.wet|floatformat:'0' }}&nbsp;{% trans "wet" %}
</div>
{% endif %}
{% if info.solid_pct > 0 %}
<div class="progress-bar bg-secondary lead"
role="progressbar"
style="width: {{ info.solid_pct|safe }}%;">
{{ info.solid|floatformat:'0' }}&nbsp;{% trans "solid" %}</div>
{{ info.solid|floatformat:'0' }}&nbsp;{% trans "solid" %}
</div>
{% endif %}
{% if info.empty_pct > 0 %}
<div class="progress-bar bg-transparent lead"
role="progressbar"
style="width: {{ info.empty_pct|safe }}%;">
{{ info.empty|floatformat:'0' }}
</div>
{% endif %}
</div>
<div class="text-center text-light small">

View File

@ -55,7 +55,7 @@ def card_diaperchange_types(context, child, date=None):
seven days.
:param child: an instance of the Child model.
:param date: a Date object for the day to filter.
:returns: a dictionary with the wet/dry statistics.
:returns: a dictionary with the wet/solid/empty statistics.
"""
if not date:
time = timezone.localtime()
@ -70,7 +70,7 @@ def card_diaperchange_types(context, child, date=None):
)
for x in range(7):
stats[x] = {"wet": 0.0, "solid": 0.0}
stats[x] = {"wet": 0.0, "solid": 0.0, "empty": 0.0}
instances = (
models.DiaperChange.objects.filter(child=child)
@ -86,13 +86,16 @@ def card_diaperchange_types(context, child, date=None):
stats[key]["wet"] += 1
if instance.solid:
stats[key]["solid"] += 1
if not instance.wet and not instance.solid:
stats[key]["empty"] += 1
for key, info in stats.items():
total = info["wet"] + info["solid"]
total = info["wet"] + info["solid"] + info["empty"]
week_total += total
if total > 0:
stats[key]["wet_pct"] = info["wet"] / total * 100
stats[key]["solid_pct"] = info["solid"] / total * 100
stats[key]["empty_pct"] = info["empty"] / total * 100
return {
"type": "diaperchange",

View File

@ -92,13 +92,62 @@ class TemplateTagsTestCase(TestCase):
data = cards.card_diaperchange_types(self.context, self.child, self.date)
self.assertEqual(data["type"], "diaperchange")
stats = {
0: {"wet_pct": 50.0, "solid_pct": 50.0, "solid": 1, "wet": 1},
1: {"wet_pct": 0.0, "solid_pct": 100.0, "solid": 2, "wet": 0},
2: {"wet_pct": 100.0, "solid_pct": 0.0, "solid": 0, "wet": 2},
3: {"wet_pct": 75.0, "solid_pct": 25.0, "solid": 1, "wet": 3},
4: {"wet_pct": 100.0, "solid_pct": 0.0, "solid": 0, "wet": 1},
5: {"wet_pct": 100.0, "solid_pct": 0.0, "solid": 0, "wet": 2},
6: {"wet_pct": 100.0, "solid_pct": 0.0, "solid": 0, "wet": 1},
0: {
"wet_pct": 50.0,
"solid_pct": 50.0,
"empty_pct": 0.0,
"solid": 1,
"wet": 1,
"empty": 0.0,
},
1: {
"wet_pct": 0.0,
"solid_pct": 100.0,
"empty_pct": 0.0,
"solid": 2,
"wet": 0,
"empty": 0.0,
},
2: {
"wet_pct": 100.0,
"solid_pct": 0.0,
"empty_pct": 0.0,
"solid": 0,
"wet": 2,
"empty": 0.0,
},
3: {
"wet_pct": 75.0,
"solid_pct": 25.0,
"empty_pct": 0.0,
"solid": 1,
"wet": 3,
"empty": 0.0,
},
4: {
"wet_pct": 100.0,
"solid_pct": 0.0,
"empty_pct": 0.0,
"solid": 0,
"wet": 1,
"empty": 0.0,
},
5: {
"wet_pct": 100.0,
"solid_pct": 0.0,
"empty_pct": 0.0,
"solid": 0,
"wet": 2,
"empty": 0.0,
},
6: {
"wet_pct": 100.0,
"solid_pct": 0.0,
"empty_pct": 0.0,
"solid": 0,
"wet": 1,
"empty": 0.0,
},
}
self.assertEqual(data["stats"], stats)