2017-08-21 23:21:08 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
from django.contrib.auth.mixins import PermissionRequiredMixin
|
2017-08-24 18:54:01 +00:00
|
|
|
from django.db.models import Count, Case, When
|
2017-08-23 13:04:05 +00:00
|
|
|
from django.db.models.functions import TruncDate
|
2017-08-21 23:21:08 +00:00
|
|
|
from django.views.generic.detail import DetailView
|
|
|
|
|
2017-08-23 13:04:05 +00:00
|
|
|
import plotly.offline as plotly
|
|
|
|
import plotly.graph_objs as go
|
2017-08-21 23:21:08 +00:00
|
|
|
|
2017-08-23 13:04:05 +00:00
|
|
|
from core.models import Child, DiaperChange
|
2017-08-21 23:21:08 +00:00
|
|
|
|
2017-08-24 18:54:01 +00:00
|
|
|
from .utils import default_graph_layout_options, split_graph_output
|
2017-08-23 13:04:05 +00:00
|
|
|
|
2017-08-24 18:54:01 +00:00
|
|
|
|
|
|
|
class DiaperChangesChildReport(PermissionRequiredMixin, DetailView):
|
2017-08-23 01:37:10 +00:00
|
|
|
"""All sleep data for a child."""
|
2017-08-21 23:21:08 +00:00
|
|
|
model = Child
|
|
|
|
permission_required = ('core.view_child',)
|
2017-08-23 13:04:05 +00:00
|
|
|
template_name = 'reports/diaperchange.html'
|
2017-08-23 01:37:10 +00:00
|
|
|
|
2017-08-24 18:54:01 +00:00
|
|
|
def __init__(self):
|
|
|
|
super(DiaperChangesChildReport, self).__init__()
|
|
|
|
self.html = ''
|
|
|
|
self.javascript = ''
|
|
|
|
|
2017-08-23 01:37:10 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
2017-08-24 18:54:01 +00:00
|
|
|
context = super(DiaperChangesChildReport, self).get_context_data(**kwargs)
|
2017-08-23 01:37:10 +00:00
|
|
|
child = context['object']
|
2017-08-24 18:54:01 +00:00
|
|
|
self._change_types_over_time(child)
|
|
|
|
context['html'] = self.html
|
|
|
|
context['javascript'] = self.javascript
|
|
|
|
return context
|
2017-08-23 01:37:10 +00:00
|
|
|
|
2017-08-24 18:54:01 +00:00
|
|
|
def _change_types_over_time(self, child):
|
|
|
|
changes = DiaperChange.objects.filter(child=child) \
|
|
|
|
.annotate(date=TruncDate('time')) \
|
|
|
|
.values('date') \
|
|
|
|
.annotate(wet_count=Count(Case(When(wet=True, then=1)))) \
|
|
|
|
.annotate(solid_count=Count(Case(When(solid=True, then=1)))) \
|
|
|
|
.annotate(total=Count('id')) \
|
2017-08-23 13:04:05 +00:00
|
|
|
.order_by('-date')
|
|
|
|
|
2017-08-24 18:54:01 +00:00
|
|
|
solid_trace = go.Scatter(
|
|
|
|
mode='markers',
|
|
|
|
name='Solid changes',
|
2017-08-23 13:04:05 +00:00
|
|
|
x=list(changes.values_list('date', flat=True)),
|
2017-08-24 18:54:01 +00:00
|
|
|
y=list(changes.values_list('solid_count', flat=True)),
|
|
|
|
)
|
|
|
|
wet_trace = go.Scatter(
|
|
|
|
mode='markers',
|
|
|
|
name='Wet changes',
|
|
|
|
x=list(changes.values_list('date', flat=True)),
|
|
|
|
y=list(changes.values_list('wet_count', flat=True))
|
|
|
|
)
|
|
|
|
total_trace = go.Scatter(
|
|
|
|
name='Total changes',
|
|
|
|
x=list(changes.values_list('date', flat=True)),
|
|
|
|
y=list(changes.values_list('total', flat=True))
|
2017-08-23 13:04:05 +00:00
|
|
|
)
|
|
|
|
|
2017-08-24 18:54:01 +00:00
|
|
|
layout_args = default_graph_layout_options()
|
|
|
|
layout_args['barmode'] = 'stack'
|
|
|
|
layout_args['title'] = 'Diaper change types over time'
|
|
|
|
layout_args['xaxis']['title'] = 'Date'
|
|
|
|
layout_args['yaxis']['title'] = 'Number of changes'
|
2017-08-23 01:37:10 +00:00
|
|
|
|
2017-08-24 18:54:01 +00:00
|
|
|
fig = go.Figure({
|
|
|
|
'data': [solid_trace, wet_trace, total_trace],
|
|
|
|
'layout': go.Layout(**layout_args)
|
|
|
|
})
|
|
|
|
output = plotly.plot(fig, output_type='div', include_plotlyjs=False)
|
|
|
|
html, javascript = split_graph_output(output)
|
|
|
|
self.html += '<a name="change_types_over_time">' + html
|
|
|
|
self.javascript += javascript
|
2017-08-23 01:37:10 +00:00
|
|
|
|
|
|
|
|
2017-08-23 13:04:05 +00:00
|
|
|
class SleepReport(PermissionRequiredMixin, DetailView):
|
|
|
|
"""All sleep data for a child."""
|
|
|
|
model = Child
|
|
|
|
permission_required = ('core.view_child',)
|
|
|
|
template_name = 'reports/sleep.html'
|
2017-08-23 01:37:10 +00:00
|
|
|
|
2017-08-23 13:04:05 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(SleepReport, self).get_context_data(**kwargs)
|
2017-08-23 01:37:10 +00:00
|
|
|
return context
|