mirror of https://github.com/snachodog/mybuddy.git
Add "Feeding Amounts" report. (#68)
This commit is contained in:
parent
bff17dd2a0
commit
3ba58c9352
|
@ -18,6 +18,7 @@
|
||||||
<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-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-duration-child' object.slug %}">{% trans "Feeding Durations (Average)" %}</a>
|
<a class="dropdown-item" href="{% url 'reports:report-feeding-duration-child' object.slug %}">{% trans "Feeding Durations (Average)" %}</a>
|
||||||
<a class="dropdown-item" href="{% url 'reports:report-sleep-pattern-child' object.slug %}">{% trans "Sleep Pattern" %}</a>
|
<a class="dropdown-item" href="{% url 'reports:report-sleep-pattern-child' object.slug %}">{% trans "Sleep Pattern" %}</a>
|
||||||
<a class="dropdown-item" href="{% url 'reports:report-sleep-totals-child' object.slug %}">{% trans "Sleep Totals" %}</a>
|
<a class="dropdown-item" href="{% url 'reports:report-sleep-totals-child' object.slug %}">{% trans "Sleep Totals" %}</a>
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
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_duration import feeding_duration # NOQA
|
from .feeding_duration import feeding_duration # 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
|
||||||
|
|
|
@ -0,0 +1,46 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
from django.db.models import Sum
|
||||||
|
from django.db.models.functions import TruncDate
|
||||||
|
from django.utils.translation import gettext as _
|
||||||
|
|
||||||
|
import plotly.offline as plotly
|
||||||
|
import plotly.graph_objs as go
|
||||||
|
|
||||||
|
from reports import utils
|
||||||
|
|
||||||
|
|
||||||
|
def feeding_amounts(instances):
|
||||||
|
"""
|
||||||
|
Create a graph showing daily feeding amounts over time.
|
||||||
|
:param instances: a QuerySet of Feeding instances.
|
||||||
|
:returns: a tuple of the the graph's html and javascript.
|
||||||
|
"""
|
||||||
|
totals = instances.annotate(date=TruncDate('start')) \
|
||||||
|
.values('date') \
|
||||||
|
.annotate(sum=Sum('amount')) \
|
||||||
|
.order_by('-date')
|
||||||
|
|
||||||
|
dates = [value['date'] for value in totals.values('date')]
|
||||||
|
amounts = [value['amount'] or 0 for value in totals.values('amount')]
|
||||||
|
|
||||||
|
trace = go.Bar(
|
||||||
|
name=_('Total feeding amount'),
|
||||||
|
x=dates,
|
||||||
|
y=amounts,
|
||||||
|
hoverinfo='text',
|
||||||
|
textposition='outside',
|
||||||
|
text=amounts
|
||||||
|
)
|
||||||
|
|
||||||
|
layout_args = utils.default_graph_layout_options()
|
||||||
|
layout_args['title'] = _('<b>Total Feeding Amounts</b>')
|
||||||
|
layout_args['xaxis']['title'] = _('Date')
|
||||||
|
layout_args['xaxis']['rangeselector'] = utils.rangeselector_date()
|
||||||
|
layout_args['yaxis']['title'] = _('Feeding 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 "Feeding Amounts" %} - {{ object }}{% endblock %}
|
||||||
|
|
||||||
|
{% block breadcrumbs %}
|
||||||
|
{{ block.super }}
|
||||||
|
<li class="breadcrumb-item active" aria-current="page">{% trans "Feeding Amounts" %}</li>
|
||||||
|
{% endblock %}
|
|
@ -38,6 +38,8 @@ class ViewsTestCase(TestCase):
|
||||||
page = self.c.get('{}/changes/types/'.format(base_url))
|
page = self.c.get('{}/changes/types/'.format(base_url))
|
||||||
self.assertEqual(page.status_code, 200)
|
self.assertEqual(page.status_code, 200)
|
||||||
|
|
||||||
|
page = self.c.get('{}/feeding/amounts/'.format(base_url))
|
||||||
|
self.assertEqual(page.status_code, 200)
|
||||||
page = self.c.get('{}/feeding/duration/'.format(base_url))
|
page = self.c.get('{}/feeding/duration/'.format(base_url))
|
||||||
self.assertEqual(page.status_code, 200)
|
self.assertEqual(page.status_code, 200)
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,11 @@ urlpatterns = [
|
||||||
views.DiaperChangeTypesChildReport.as_view(),
|
views.DiaperChangeTypesChildReport.as_view(),
|
||||||
name='report-diaperchange-types-child'
|
name='report-diaperchange-types-child'
|
||||||
),
|
),
|
||||||
|
path(
|
||||||
|
'children/<slug:slug>/reports/feeding/amounts/',
|
||||||
|
views.FeedingAmountsChildReport.as_view(),
|
||||||
|
name='report-feeding-amounts-child'
|
||||||
|
),
|
||||||
path(
|
path(
|
||||||
'children/<slug:slug>/reports/feeding/duration/',
|
'children/<slug:slug>/reports/feeding/duration/',
|
||||||
views.FeedingDurationChildReport.as_view(),
|
views.FeedingDurationChildReport.as_view(),
|
||||||
|
|
|
@ -45,6 +45,29 @@ class DiaperChangeTypesChildReport(PermissionRequired403Mixin, DetailView):
|
||||||
return context
|
return context
|
||||||
|
|
||||||
|
|
||||||
|
class FeedingAmountsChildReport(PermissionRequired403Mixin, DetailView):
|
||||||
|
"""
|
||||||
|
Graph of daily feeding amounts over time.
|
||||||
|
"""
|
||||||
|
model = models.Child
|
||||||
|
permission_required = ('core.view_child',)
|
||||||
|
template_name = 'reports/feeding_amounts.html'
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
super(FeedingAmountsChildReport, self).__init__()
|
||||||
|
self.html = ''
|
||||||
|
self.js = ''
|
||||||
|
|
||||||
|
def get_context_data(self, **kwargs):
|
||||||
|
context = super(FeedingAmountsChildReport, self).get_context_data(
|
||||||
|
**kwargs)
|
||||||
|
child = context['object']
|
||||||
|
instances = models.Feeding.objects.filter(child=child)
|
||||||
|
if instances:
|
||||||
|
context['html'], context['js'] = graphs.feeding_amounts(instances)
|
||||||
|
return context
|
||||||
|
|
||||||
|
|
||||||
class FeedingDurationChildReport(PermissionRequired403Mixin, DetailView):
|
class FeedingDurationChildReport(PermissionRequired403Mixin, DetailView):
|
||||||
"""
|
"""
|
||||||
Graph of feeding durations over time.
|
Graph of feeding durations over time.
|
||||||
|
@ -56,7 +79,7 @@ class FeedingDurationChildReport(PermissionRequired403Mixin, DetailView):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(FeedingDurationChildReport, self).__init__()
|
super(FeedingDurationChildReport, self).__init__()
|
||||||
self.html = ''
|
self.html = ''
|
||||||
self.javascript = ''
|
self.js = ''
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
context = super(FeedingDurationChildReport, self).get_context_data(
|
context = super(FeedingDurationChildReport, self).get_context_data(
|
||||||
|
@ -79,7 +102,7 @@ class SleepPatternChildReport(PermissionRequired403Mixin, DetailView):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(SleepPatternChildReport, self).__init__()
|
super(SleepPatternChildReport, self).__init__()
|
||||||
self.html = ''
|
self.html = ''
|
||||||
self.javascript = ''
|
self.js = ''
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
context = super(SleepPatternChildReport, self).get_context_data(
|
context = super(SleepPatternChildReport, self).get_context_data(
|
||||||
|
@ -102,7 +125,7 @@ class SleepTotalsChildReport(PermissionRequired403Mixin, DetailView):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(SleepTotalsChildReport, self).__init__()
|
super(SleepTotalsChildReport, self).__init__()
|
||||||
self.html = ''
|
self.html = ''
|
||||||
self.javascript = ''
|
self.js = ''
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
context = super(SleepTotalsChildReport, self).get_context_data(
|
context = super(SleepTotalsChildReport, self).get_context_data(
|
||||||
|
|
Loading…
Reference in New Issue