2017-08-21 23:21:08 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
from django.contrib.auth.mixins import PermissionRequiredMixin
|
|
|
|
from django.views.generic.detail import DetailView
|
2017-09-15 12:11:41 +00:00
|
|
|
from django.utils import timezone
|
2017-08-21 23:21:08 +00:00
|
|
|
|
2017-10-28 00:46:48 +00:00
|
|
|
from core.models import Child, DiaperChange, Sleep
|
2017-08-21 23:21:08 +00:00
|
|
|
|
2017-09-25 17:14:14 +00:00
|
|
|
from .graphs import (diaperchange_types, diaperchange_lifetimes, sleep_pattern,
|
|
|
|
sleep_totals, timeline)
|
|
|
|
|
|
|
|
|
|
|
|
class DiaperChangeLifetimesChildReport(PermissionRequiredMixin, DetailView):
|
2017-10-28 00:46:48 +00:00
|
|
|
"""Graph of diaper changes by day and type.
|
|
|
|
"""
|
2017-09-25 17:14:14 +00:00
|
|
|
model = Child
|
|
|
|
permission_required = ('core.view_child',)
|
|
|
|
template_name = 'reports/diaperchange_lifetimes.html'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2017-10-22 19:05:58 +00:00
|
|
|
context = super(
|
|
|
|
DiaperChangeLifetimesChildReport, self).get_context_data(**kwargs)
|
2017-09-25 17:14:14 +00:00
|
|
|
child = context['object']
|
2017-10-28 00:46:48 +00:00
|
|
|
changes = DiaperChange.objects.filter(child=child)
|
|
|
|
if changes and changes.count() > 1:
|
|
|
|
context['html'], context['javascript'] = diaperchange_lifetimes(
|
|
|
|
changes)
|
2017-09-25 17:14:14 +00:00
|
|
|
return context
|
2017-08-23 13:04:05 +00:00
|
|
|
|
2017-08-24 18:54:01 +00:00
|
|
|
|
2017-09-13 15:25:12 +00:00
|
|
|
class DiaperChangeTypesChildReport(PermissionRequiredMixin, DetailView):
|
2017-10-28 00:46:48 +00:00
|
|
|
"""Graph of diaper changes by day and type.
|
|
|
|
"""
|
2017-08-21 23:21:08 +00:00
|
|
|
model = Child
|
|
|
|
permission_required = ('core.view_child',)
|
2017-09-13 15:25:12 +00:00
|
|
|
template_name = 'reports/diaperchange_types.html'
|
2017-08-23 01:37:10 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2017-09-15 12:11:41 +00:00
|
|
|
context = super(DiaperChangeTypesChildReport, self).get_context_data(
|
|
|
|
**kwargs)
|
2017-08-23 01:37:10 +00:00
|
|
|
child = context['object']
|
2017-10-28 00:46:48 +00:00
|
|
|
changes = DiaperChange.objects.filter(child=child)
|
|
|
|
if changes:
|
|
|
|
context['html'], context['javascript'] = diaperchange_types(
|
|
|
|
changes)
|
2017-08-24 18:54:01 +00:00
|
|
|
return context
|
2017-08-23 01:37:10 +00:00
|
|
|
|
|
|
|
|
2017-09-13 15:25:12 +00:00
|
|
|
class SleepPatternChildReport(PermissionRequiredMixin, DetailView):
|
2017-10-28 00:46:48 +00:00
|
|
|
"""Graph of sleep pattern comparing sleep to wake times by day.
|
|
|
|
"""
|
2017-08-23 13:04:05 +00:00
|
|
|
model = Child
|
|
|
|
permission_required = ('core.view_child',)
|
2017-09-13 15:25:12 +00:00
|
|
|
template_name = 'reports/sleep_pattern.html'
|
2017-08-25 16:19:06 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
2017-09-13 15:25:12 +00:00
|
|
|
super(SleepPatternChildReport, self).__init__()
|
2017-08-25 16:19:06 +00:00
|
|
|
self.html = ''
|
|
|
|
self.javascript = ''
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2017-09-15 12:11:41 +00:00
|
|
|
context = super(SleepPatternChildReport, self).get_context_data(
|
|
|
|
**kwargs)
|
2017-08-25 16:19:06 +00:00
|
|
|
child = context['object']
|
2017-10-28 00:46:48 +00:00
|
|
|
instances = Sleep.objects.filter(child=child).order_by('start')
|
|
|
|
if instances:
|
|
|
|
context['html'], context['javascript'] = sleep_pattern(instances)
|
2017-08-25 16:19:06 +00:00
|
|
|
return context
|
2017-09-13 15:53:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SleepTotalsChildReport(PermissionRequiredMixin, DetailView):
|
2017-10-28 00:46:48 +00:00
|
|
|
"""Graph of total sleep by day.
|
|
|
|
"""
|
2017-09-13 15:53:32 +00:00
|
|
|
model = Child
|
|
|
|
permission_required = ('core.view_child',)
|
|
|
|
template_name = 'reports/sleep_totals.html'
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super(SleepTotalsChildReport, self).__init__()
|
|
|
|
self.html = ''
|
|
|
|
self.javascript = ''
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2017-09-15 12:11:41 +00:00
|
|
|
context = super(SleepTotalsChildReport, self).get_context_data(
|
|
|
|
**kwargs)
|
2017-09-13 15:53:32 +00:00
|
|
|
child = context['object']
|
2017-10-28 00:46:48 +00:00
|
|
|
instances = Sleep.objects.filter(child=child).order_by('start')
|
|
|
|
if instances:
|
|
|
|
context['html'], context['javascript'] = sleep_totals(instances)
|
2017-09-13 15:53:32 +00:00
|
|
|
return context
|
2017-09-15 12:11:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TimelineChildReport(PermissionRequiredMixin, DetailView):
|
2017-10-31 00:58:12 +00:00
|
|
|
"""Chronological daily view of events (non-graph).
|
|
|
|
"""
|
2017-09-15 12:11:41 +00:00
|
|
|
model = Child
|
|
|
|
permission_required = ('core.view_child',)
|
|
|
|
template_name = 'reports/timeline.html'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(TimelineChildReport, self).get_context_data(**kwargs)
|
2017-10-31 00:58:12 +00:00
|
|
|
date = self.request.GET.get('date', str(timezone.localdate()))
|
2017-09-16 21:17:09 +00:00
|
|
|
date = timezone.datetime.strptime(date, '%Y-%m-%d')
|
|
|
|
date = timezone.localtime(timezone.make_aware(date))
|
|
|
|
context['objects'] = timeline(self.object, date)
|
|
|
|
context['date'] = date
|
2017-09-20 15:52:30 +00:00
|
|
|
context['date_previous'] = date - timezone.timedelta(days=1)
|
|
|
|
context['date_next'] = date + timezone.timedelta(days=1)
|
2017-09-15 12:11:41 +00:00
|
|
|
return context
|