Add a Diaper Change amounts report

This commit is contained in:
Christopher C. Wells 2020-01-26 14:12:27 -08:00
parent 7f258f972d
commit 3a7c8c92fd
7 changed files with 82 additions and 0 deletions

View File

@ -16,6 +16,7 @@
aria-haspopup="true" aria-haspopup="true"
aria-expanded="false"><i class="icon icon-graph" aria-hidden="true"></i></button> aria-expanded="false"><i class="icon icon-graph" aria-hidden="true"></i></button>
<div class="dropdown-menu" aria-labelledby="reports-dropdown"> <div class="dropdown-menu" aria-labelledby="reports-dropdown">
<a class="dropdown-item" href="{% url 'reports:report-diaperchange-amounts-child' object.slug %}">{% trans "Diaper Change Amounts" %}</a>
<a class="dropdown-item" href="{% url 'reports:report-diaperchange-types-child' object.slug %}">{% trans "Diaper Change Types" %}</a> <a class="dropdown-item" href="{% url 'reports:report-diaperchange-types-child' object.slug %}">{% trans "Diaper Change Types" %}</a>
<a class="dropdown-item" href="{% url 'reports:report-diaperchange-lifetimes-child' object.slug %}">{% trans "Diaper Lifetimes" %}</a> <a class="dropdown-item" href="{% url 'reports:report-diaperchange-lifetimes-child' object.slug %}">{% trans "Diaper Lifetimes" %}</a>
<a class="dropdown-item" href="{% url 'reports:report-feeding-amounts-child' object.slug %}">{% trans "Feeding Amounts" %}</a> <a class="dropdown-item" href="{% url 'reports:report-feeding-amounts-child' object.slug %}">{% trans "Feeding Amounts" %}</a>

View File

@ -1,3 +1,4 @@
from .diaperchange_amounts import diaperchange_amounts # NOQA
from .diaperchange_lifetimes import diaperchange_lifetimes # NOQA from .diaperchange_lifetimes import diaperchange_lifetimes # NOQA
from .diaperchange_types import diaperchange_types # NOQA from .diaperchange_types import diaperchange_types # NOQA
from .feeding_amounts import feeding_amounts # NOQA from .feeding_amounts import feeding_amounts # NOQA

View File

@ -0,0 +1,46 @@
# -*- coding: utf-8 -*-
from django.utils import timezone
from django.utils.translation import gettext as _
import plotly.offline as plotly
import plotly.graph_objs as go
from reports import utils
def diaperchange_amounts(instances):
"""
Create a graph showing daily diaper change amounts over time.
:param instances: a QuerySet of DiaperChange instances.
:returns: a tuple of the the graph's html and javascript.
"""
totals = {}
for instance in instances:
time_local = timezone.localtime(instance.time)
date = time_local.date()
if date not in totals.keys():
totals[date] = 0
totals[date] += instance.amount or 0
amounts = [round(amount, 2) for amount in totals.values()]
trace = go.Bar(
name=_('Diaper change amount'),
x=list(totals.keys()),
y=amounts,
hoverinfo='text',
textposition='outside',
text=amounts
)
layout_args = utils.default_graph_layout_options()
layout_args['title'] = _('<b>Diaper Change Amounts</b>')
layout_args['xaxis']['title'] = _('Date')
layout_args['xaxis']['rangeselector'] = utils.rangeselector_date()
layout_args['yaxis']['title'] = _('Change amount')
fig = go.Figure({
'data': [trace],
'layout': go.Layout(**layout_args)
})
output = plotly.plot(fig, output_type='div', include_plotlyjs=False)
return utils.split_graph_output(output)

View File

@ -0,0 +1,9 @@
{% extends 'reports/report_base.html' %}
{% load i18n %}
{% block title %}{% trans "Diaper Amounts" %} - {{ object }}{% endblock %}
{% block breadcrumbs %}
{{ block.super }}
<li class="breadcrumb-item active" aria-current="page">{% trans "Diaper Amounts" %}</li>
{% endblock %}

View File

@ -33,6 +33,8 @@ class ViewsTestCase(TestCase):
child = models.Child.objects.first() child = models.Child.objects.first()
base_url = '/children/{}/reports'.format(child.slug) base_url = '/children/{}/reports'.format(child.slug)
page = self.c.get('{}/changes/amounts/'.format(base_url))
self.assertEqual(page.status_code, 200)
page = self.c.get('{}/changes/lifetimes/'.format(base_url)) page = self.c.get('{}/changes/lifetimes/'.format(base_url))
self.assertEqual(page.status_code, 200) self.assertEqual(page.status_code, 200)
page = self.c.get('{}/changes/types/'.format(base_url)) page = self.c.get('{}/changes/types/'.format(base_url))

View File

@ -6,6 +6,11 @@ from . import views
app_name = 'reports' app_name = 'reports'
urlpatterns = [ urlpatterns = [
path(
'children/<slug:slug>/reports/changes/amounts/',
views.DiaperChangeAmounts.as_view(),
name='report-diaperchange-amounts-child'
),
path( path(
'children/<slug:slug>/reports/changes/lifetimes/', 'children/<slug:slug>/reports/changes/lifetimes/',
views.DiaperChangeLifetimesChildReport.as_view(), views.DiaperChangeLifetimesChildReport.as_view(),

View File

@ -7,6 +7,24 @@ from core import models
from . import graphs from . import graphs
class DiaperChangeAmounts(PermissionRequired403Mixin, DetailView):
"""
Graph of diaper "amounts" - measurements of urine output.
"""
model = models.Child
permission_required = ('core.view_child',)
template_name = 'reports/diaperchange_amounts.html'
def get_context_data(self, **kwargs):
context = super(DiaperChangeAmounts, self).get_context_data(**kwargs)
child = context['object']
changes = models.DiaperChange.objects.filter(child=child, amount__gt=0)
if changes and changes.count() > 0:
context['html'], context['js'] = \
graphs.diaperchange_amounts(changes)
return context
class DiaperChangeLifetimesChildReport(PermissionRequired403Mixin, DetailView): class DiaperChangeLifetimesChildReport(PermissionRequired403Mixin, DetailView):
""" """
Graph of diaper "lifetimes" - time between diaper changes. Graph of diaper "lifetimes" - time between diaper changes.