Add simple weight report.

This commit is contained in:
Christopher Charbonneau Wells 2017-11-10 07:20:34 -05:00
parent 067be4bf07
commit 5d4db5de6b
7 changed files with 104 additions and 21 deletions

View File

@ -18,6 +18,7 @@
<a class="dropdown-item" href="{% url 'reports:report-diaperchange-lifetimes-child' object.slug %}">Diaper Lifetimes</a> <a class="dropdown-item" href="{% url 'reports:report-diaperchange-lifetimes-child' object.slug %}">Diaper Lifetimes</a>
<a class="dropdown-item" href="{% url 'reports:report-sleep-pattern-child' object.slug %}">Sleep Pattern</a> <a class="dropdown-item" href="{% url 'reports:report-sleep-pattern-child' object.slug %}">Sleep Pattern</a>
<a class="dropdown-item" href="{% url 'reports:report-sleep-totals-child' object.slug %}">Sleep Totals</a> <a class="dropdown-item" href="{% url 'reports:report-sleep-totals-child' object.slug %}">Sleep Totals</a>
<a class="dropdown-item" href="{% url 'reports:report-weight-weight-child' object.slug %}">Weight</a>
</div> </div>
</div> </div>

View File

@ -2,3 +2,4 @@ from .diaperchange_lifetimes import diaperchange_lifetimes # NOQA
from .diaperchange_types import diaperchange_types # NOQA from .diaperchange_types import diaperchange_types # NOQA
from .sleep_pattern import sleep_pattern # NOQA from .sleep_pattern import sleep_pattern # NOQA
from .sleep_totals import sleep_totals # NOQA from .sleep_totals import sleep_totals # NOQA
from .weight_weight import weight_weight # NOQA

View File

@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import plotly.offline as plotly
import plotly.graph_objs as go
from reports import utils
def weight_weight(objects):
"""
Create a graph showing weight over time.
:param objects: a QuerySet of Weight instances.
:returns: a tuple of the the graph's html and javascript.
"""
objects = objects.order_by('-date')
trace = go.Scatter(
name='Weight',
x=list(objects.values_list('date', flat=True)),
y=list(objects.values_list('weight', flat=True)),
fill='tozeroy',
)
layout_args = utils.default_graph_layout_options()
layout_args['barmode'] = 'stack'
layout_args['title'] = '<b>Weight</b>'
layout_args['xaxis']['title'] = 'Date'
layout_args['xaxis']['rangeselector'] = utils.rangeselector_date()
layout_args['yaxis']['title'] = 'Weight'
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,13 @@
{% extends 'reports/report_base.html' %}
{% block title %}Weight - {{ object }}{% endblock %}
{% block breadcrumbs %}
{{ block.super }}
<li class="breadcrumb-item active" aria-current="page">Weight</li>
{% endblock %}
{% block javascript %}
{{ block.super }}
{{ javascript|safe }}
{% endblock %}

View File

@ -44,3 +44,6 @@ class ViewsTestCase(TestCase):
self.assertEqual(page.status_code, 200) self.assertEqual(page.status_code, 200)
page = self.c.get('{}/sleep/totals/'.format(base_url)) page = self.c.get('{}/sleep/totals/'.format(base_url))
self.assertEqual(page.status_code, 200) self.assertEqual(page.status_code, 200)
page = self.c.get('{}/weight/weight/'.format(base_url))
self.assertEqual(page.status_code, 200)

View File

@ -19,4 +19,8 @@ urlpatterns = [
url(r'^children/(?P<slug>[^/.]+)/reports/sleep/totals/$', url(r'^children/(?P<slug>[^/.]+)/reports/sleep/totals/$',
views.SleepTotalsChildReport.as_view(), views.SleepTotalsChildReport.as_view(),
name='report-sleep-totals-child'), name='report-sleep-totals-child'),
url(r'^children/(?P<slug>[^/.]+)/reports/weight/weight/$',
views.WeightWeightChildReoport.as_view(),
name='report-weight-weight-child'),
] ]

View File

@ -4,16 +4,16 @@ from __future__ import unicode_literals
from django.contrib.auth.mixins import PermissionRequiredMixin from django.contrib.auth.mixins import PermissionRequiredMixin
from django.views.generic.detail import DetailView from django.views.generic.detail import DetailView
from core.models import Child, DiaperChange, Sleep from core import models
from .graphs import (diaperchange_types, diaperchange_lifetimes, sleep_pattern, from . import graphs
sleep_totals)
class DiaperChangeLifetimesChildReport(PermissionRequiredMixin, DetailView): class DiaperChangeLifetimesChildReport(PermissionRequiredMixin, DetailView):
"""Graph of diaper changes by day and type.
""" """
model = Child Graph of diaper "lifetimes" - time between diaper changes.
"""
model = models.Child
permission_required = ('core.view_child',) permission_required = ('core.view_child',)
template_name = 'reports/diaperchange_lifetimes.html' template_name = 'reports/diaperchange_lifetimes.html'
@ -21,17 +21,18 @@ class DiaperChangeLifetimesChildReport(PermissionRequiredMixin, DetailView):
context = super( context = super(
DiaperChangeLifetimesChildReport, self).get_context_data(**kwargs) DiaperChangeLifetimesChildReport, self).get_context_data(**kwargs)
child = context['object'] child = context['object']
changes = DiaperChange.objects.filter(child=child) changes = models.DiaperChange.objects.filter(child=child)
if changes and changes.count() > 1: if changes and changes.count() > 1:
context['html'], context['javascript'] = diaperchange_lifetimes( context['html'], context['javascript'] = \
changes) graphs.diaperchange_lifetimes(changes)
return context return context
class DiaperChangeTypesChildReport(PermissionRequiredMixin, DetailView): class DiaperChangeTypesChildReport(PermissionRequiredMixin, DetailView):
"""Graph of diaper changes by day and type.
""" """
model = Child Graph of diaper changes by day and type.
"""
model = models.Child
permission_required = ('core.view_child',) permission_required = ('core.view_child',)
template_name = 'reports/diaperchange_types.html' template_name = 'reports/diaperchange_types.html'
@ -39,17 +40,18 @@ class DiaperChangeTypesChildReport(PermissionRequiredMixin, DetailView):
context = super(DiaperChangeTypesChildReport, self).get_context_data( context = super(DiaperChangeTypesChildReport, self).get_context_data(
**kwargs) **kwargs)
child = context['object'] child = context['object']
changes = DiaperChange.objects.filter(child=child) changes = models.DiaperChange.objects.filter(child=child)
if changes: if changes:
context['html'], context['javascript'] = diaperchange_types( context['html'], context['javascript'] = \
changes) graphs.diaperchange_types(changes)
return context return context
class SleepPatternChildReport(PermissionRequiredMixin, DetailView): class SleepPatternChildReport(PermissionRequiredMixin, DetailView):
"""Graph of sleep pattern comparing sleep to wake times by day.
""" """
model = Child Graph of sleep pattern comparing sleep to wake times by day.
"""
model = models.Child
permission_required = ('core.view_child',) permission_required = ('core.view_child',)
template_name = 'reports/sleep_pattern.html' template_name = 'reports/sleep_pattern.html'
@ -62,16 +64,18 @@ class SleepPatternChildReport(PermissionRequiredMixin, DetailView):
context = super(SleepPatternChildReport, self).get_context_data( context = super(SleepPatternChildReport, self).get_context_data(
**kwargs) **kwargs)
child = context['object'] child = context['object']
instances = Sleep.objects.filter(child=child).order_by('start') instances = models.Sleep.objects.filter(child=child).order_by('start')
if instances: if instances:
context['html'], context['javascript'] = sleep_pattern(instances) context['html'], context['javascript'] = \
graphs.sleep_pattern(instances)
return context return context
class SleepTotalsChildReport(PermissionRequiredMixin, DetailView): class SleepTotalsChildReport(PermissionRequiredMixin, DetailView):
"""Graph of total sleep by day.
""" """
model = Child Graph of total sleep by day.
"""
model = models.Child
permission_required = ('core.view_child',) permission_required = ('core.view_child',)
template_name = 'reports/sleep_totals.html' template_name = 'reports/sleep_totals.html'
@ -84,7 +88,27 @@ class SleepTotalsChildReport(PermissionRequiredMixin, DetailView):
context = super(SleepTotalsChildReport, self).get_context_data( context = super(SleepTotalsChildReport, self).get_context_data(
**kwargs) **kwargs)
child = context['object'] child = context['object']
instances = Sleep.objects.filter(child=child).order_by('start') instances = models.Sleep.objects.filter(child=child).order_by('start')
if instances: if instances:
context['html'], context['javascript'] = sleep_totals(instances) context['html'], context['javascript'] = \
graphs.sleep_totals(instances)
return context
class WeightWeightChildReoport(PermissionRequiredMixin, DetailView):
"""
Graph of weight change over time.
"""
model = models.Child
permission_required = ('core.view_child',)
template_name = 'reports/weight_change.html'
def get_context_data(self, **kwargs):
context = super(WeightWeightChildReoport, self).get_context_data(
**kwargs)
child = context['object']
objects = models.Weight.objects.filter(child=child)
if objects:
context['html'], context['javascript'] = \
graphs.weight_weight(objects)
return context return context