From ab1b9ee7a821ec34556cdd727b5f7427bec355da Mon Sep 17 00:00:00 2001 From: Christopher Charbonneau Wells Date: Sat, 16 Sep 2017 17:17:09 -0400 Subject: [PATCH] Add actual events to timeline (WIP). --- reports/graphs.py | 61 ++++++++++++++++++++++++- reports/static_src/scss/timeline.scss | 4 -- reports/templates/reports/timeline.html | 58 +++-------------------- reports/views.py | 11 ++--- 4 files changed, 72 insertions(+), 62 deletions(-) diff --git a/reports/graphs.py b/reports/graphs.py index 847ecf59..80f02f66 100644 --- a/reports/graphs.py +++ b/reports/graphs.py @@ -11,13 +11,14 @@ import pandas as pd import plotly.offline as plotly import plotly.graph_objs as go -from core.models import DiaperChange, Sleep +from core.models import DiaperChange, Feeding, Sleep, TummyTime from core.utils import duration_string, duration_string_short from .utils import default_graph_layout_options, split_graph_output def diaperchange_types(child): + """Create a graph showing types of totals for diaper changes.""" changes = DiaperChange.objects.filter(child=child) \ .annotate(date=TruncDate('time')) \ .values('date') \ @@ -243,3 +244,61 @@ def _add_sleep_entry(y_df, text_df, index, column, duration, text=''): y_df.set_value(index, column, duration) text_df.set_value(index, column, text) return index + 1 + + +def timeline(child, date): + """Create a time-sorted dictionary for all events for a child.""" + min_date = date + max_date = date.replace(hour=23, minute=59, second=59) + events = [] + instances = DiaperChange.objects.filter(child=child).filter( + time__range=(min_date, max_date)).order_by('-time') + for instance in instances: + events.append({ + 'time': timezone.localtime(instance.time), + 'event': '{} had a diaper change.'.format(child.first_name), + 'model_name': instance.model_name + }) + instances = Feeding.objects.filter(child=child).filter( + start__range=(min_date, max_date)).order_by('-start') + for instance in instances: + events.append({ + 'time': timezone.localtime(instance.start), + 'event': '{} started feeding.'.format(instance.child.first_name), + 'model_name': instance.model_name + }) + events.append({ + 'time': timezone.localtime(instance.end), + 'event': '{} finished feeding.'.format(instance.child.first_name), + 'model_name': instance.model_name + }) + instances = Sleep.objects.filter(child=child).filter( + start__range=(min_date, max_date)).order_by('-start') + for instance in instances: + events.append({ + 'time': timezone.localtime(instance.start), + 'event': '{} fell asleep.'.format(instance.child.first_name), + 'model_name': instance.model_name + }) + events.append({ + 'time': timezone.localtime(instance.end), + 'event': '{} woke up.'.format(instance.child.first_name), + 'model_name': instance.model_name + }) + instances = TummyTime.objects.filter(child=child).filter( + start__range=(min_date, max_date)).order_by('-start') + for instance in instances: + events.append({ + 'time': timezone.localtime(instance.start), + 'event': '{} started tummy time!'.format( + instance.child.first_name), + 'model_name': instance.model_name + }) + events.append({ + 'time': timezone.localtime(instance.end), + 'event': '{} finished tummy time.'.format( + instance.child.first_name), + 'model_name': instance.model_name + }) + events.sort(key=lambda x: x['time'], reverse=True) + return events diff --git a/reports/static_src/scss/timeline.scss b/reports/static_src/scss/timeline.scss index 54c967b1..39842bc5 100644 --- a/reports/static_src/scss/timeline.scss +++ b/reports/static_src/scss/timeline.scss @@ -34,11 +34,7 @@ .card { width: 47%; float: left; - border: 1px solid #d4d4d4; - border-radius: 2px; - padding: 10px; position: relative; - -webkit-box-shadow: 0 1px 6px rgba(0, 0, 0, 0.175); box-shadow: 0 1px 6px rgba(0, 0, 0, 0.175); &:before { diff --git a/reports/templates/reports/timeline.html b/reports/templates/reports/timeline.html index 164f0928..c9669f2c 100644 --- a/reports/templates/reports/timeline.html +++ b/reports/templates/reports/timeline.html @@ -6,64 +6,20 @@ {% block content %}

Timeline

{{ object }}

+

{{ date|date }}

{% endblock %} \ No newline at end of file diff --git a/reports/views.py b/reports/views.py index febe75ee..8b9e1de3 100644 --- a/reports/views.py +++ b/reports/views.py @@ -7,7 +7,7 @@ from django.utils import timezone from core.models import Child, DiaperChange -from .graphs import diaperchange_types, sleep_pattern, sleep_totals +from .graphs import diaperchange_types, sleep_pattern, sleep_totals, timeline class DiaperChangeTypesChildReport(PermissionRequiredMixin, DetailView): @@ -71,9 +71,8 @@ class TimelineChildReport(PermissionRequiredMixin, DetailView): def get_context_data(self, **kwargs): context = super(TimelineChildReport, self).get_context_data(**kwargs) date = self.request.GET.get('date', timezone.now().date()) - - changes = DiaperChange.objects.filter(child=self.object).filter( - time__contains=date).order_by('-time') - - context['objects'] = changes + date = timezone.datetime.strptime(date, '%Y-%m-%d') + date = timezone.localtime(timezone.make_aware(date)) + context['objects'] = timeline(self.object, date) + context['date'] = date return context