From ed96814b103991dfe83cf4e245c7acb6d42a32e8 Mon Sep 17 00:00:00 2001 From: Christopher Charbonneau Wells Date: Wed, 23 Aug 2017 09:04:05 -0400 Subject: [PATCH] Revert custom graph works and add Plotly-based example. --- Pipfile | 1 + reports/templates/reports/diaperchange.html | 19 ++++++ reports/templates/reports/report_base.html | 5 ++ reports/templates/reports/sleep.html | 52 +-------------- reports/urls.py | 2 + reports/views.py | 71 ++++++++++++--------- 6 files changed, 70 insertions(+), 80 deletions(-) create mode 100644 reports/templates/reports/diaperchange.html create mode 100644 reports/templates/reports/report_base.html diff --git a/Pipfile b/Pipfile index ab2eb6f6..8c285232 100644 --- a/Pipfile +++ b/Pipfile @@ -7,6 +7,7 @@ django = "*" djangorestframework = "*" django-filter = "*" django-widget-tweaks = "*" +plotly = "*" [dev-packages] faker = "*" diff --git a/reports/templates/reports/diaperchange.html b/reports/templates/reports/diaperchange.html new file mode 100644 index 00000000..f982abdc --- /dev/null +++ b/reports/templates/reports/diaperchange.html @@ -0,0 +1,19 @@ +{% extends 'reports/report_base.html' %} + +{% block title %}Diaper Change Report - {{ object }}{% endblock %} + +{% block content %} +
+ Diaper change report for {{ object }}. +
+ +
+ {{ html|safe }} +
+ +{% endblock %} + +{% block javascript %} + {{ block.super }} + {{ javascript|safe }} +{% endblock %} \ No newline at end of file diff --git a/reports/templates/reports/report_base.html b/reports/templates/reports/report_base.html new file mode 100644 index 00000000..370eabde --- /dev/null +++ b/reports/templates/reports/report_base.html @@ -0,0 +1,5 @@ +{% extends 'babyblotter/page.html' %} + +{% block javascript %} + +{% endblock %} \ No newline at end of file diff --git a/reports/templates/reports/sleep.html b/reports/templates/reports/sleep.html index 099878da..b2364caa 100644 --- a/reports/templates/reports/sleep.html +++ b/reports/templates/reports/sleep.html @@ -1,4 +1,4 @@ -{% extends 'babyblotter/page.html' %} +{% extends 'reports/report_base.html' %} {% block title %}Sleep Report - {{ object }}{% endblock %} @@ -7,54 +7,6 @@ Sleep report for {{ object }}. -
{{ stats|pprint }}
- -
-
- Tuesday, 22 Aug. -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
- Monday, 21 Aug. -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+ Test {% endblock %} \ No newline at end of file diff --git a/reports/urls.py b/reports/urls.py index f812e1e1..dd6ddc40 100644 --- a/reports/urls.py +++ b/reports/urls.py @@ -6,6 +6,8 @@ from django.conf.urls import url from . import views urlpatterns = [ + url(r'^reports/(?P[^/.]+)/changes/$', + views.DiaperChangeReport.as_view(), name='report-diaperchange'), url(r'^reports/(?P[^/.]+)/sleep/$', views.SleepReport.as_view(), name='report-sleep'), ] diff --git a/reports/views.py b/reports/views.py index ff32bb66..93634eed 100644 --- a/reports/views.py +++ b/reports/views.py @@ -1,13 +1,50 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals -from collections import OrderedDict - from django.contrib.auth.mixins import PermissionRequiredMixin -from django.utils import timezone +from django.db.models import Count +from django.db.models.functions import TruncDate from django.views.generic.detail import DetailView -from core.models import Child, Sleep +import plotly.offline as plotly +import plotly.graph_objs as go + +from core.models import Child, DiaperChange + + +class DiaperChangeReport(PermissionRequiredMixin, DetailView): + """All sleep data for a child.""" + model = Child + permission_required = ('core.view_child',) + template_name = 'reports/diaperchange.html' + + def get_context_data(self, **kwargs): + context = super(DiaperChangeReport, self).get_context_data(**kwargs) + child = context['object'] + + # TODO: Move this logic to the model? Where should data processing like + # this happen? + changes = DiaperChange.objects.filter(child=child)\ + .annotate(date=TruncDate('time'))\ + .values('date')\ + .annotate(count=Count('id'))\ + .order_by('-date') + + trace1 = go.Scatter( + x=list(changes.values_list('date', flat=True)), + y=list(changes.values_list('count', flat=True)) + ) + fig = go.Figure({ + "data": [trace1], + "layout": go.Layout(title='Diaper Changes') + }) + div = plotly.plot(fig, output_type='div', include_plotlyjs=False) + html, javascript = div.split('