mirror of https://github.com/snachodog/mybuddy.git
Add a Diaper Change amounts report
This commit is contained in:
parent
7f258f972d
commit
3a7c8c92fd
|
@ -16,6 +16,7 @@
|
|||
aria-haspopup="true"
|
||||
aria-expanded="false"><i class="icon icon-graph" aria-hidden="true"></i></button>
|
||||
<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-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>
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
from .diaperchange_amounts import diaperchange_amounts # NOQA
|
||||
from .diaperchange_lifetimes import diaperchange_lifetimes # NOQA
|
||||
from .diaperchange_types import diaperchange_types # NOQA
|
||||
from .feeding_amounts import feeding_amounts # NOQA
|
||||
|
|
|
@ -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)
|
|
@ -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 %}
|
|
@ -33,6 +33,8 @@ class ViewsTestCase(TestCase):
|
|||
child = models.Child.objects.first()
|
||||
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))
|
||||
self.assertEqual(page.status_code, 200)
|
||||
page = self.c.get('{}/changes/types/'.format(base_url))
|
||||
|
|
|
@ -6,6 +6,11 @@ from . import views
|
|||
app_name = 'reports'
|
||||
|
||||
urlpatterns = [
|
||||
path(
|
||||
'children/<slug:slug>/reports/changes/amounts/',
|
||||
views.DiaperChangeAmounts.as_view(),
|
||||
name='report-diaperchange-amounts-child'
|
||||
),
|
||||
path(
|
||||
'children/<slug:slug>/reports/changes/lifetimes/',
|
||||
views.DiaperChangeLifetimesChildReport.as_view(),
|
||||
|
|
|
@ -7,6 +7,24 @@ from core import models
|
|||
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):
|
||||
"""
|
||||
Graph of diaper "lifetimes" - time between diaper changes.
|
||||
|
|
Loading…
Reference in New Issue