Finalize child timeline graph/view.

This commit is contained in:
Christopher Charbonneau Wells 2017-09-20 11:52:30 -04:00
parent 98963572ba
commit 6562fa2390
4 changed files with 42 additions and 21 deletions

View File

@ -16,15 +16,10 @@
aria-haspopup="true"
aria-expanded="false"><i class="icon icon-graph" aria-hidden="true"></i> Reports</button>
<div class="dropdown-menu" aria-labelledby="reports-dropdown">
{% if perms.core.view_diaperchange %}
<h6 class="dropdown-header"><i class="icon icon-delete" aria-hidden="true"></i> Diaper Changes</h6>
<a class="dropdown-item" href="{% url 'reports:report-diaperchange-types-child' object.slug %}">Change Types</a>
{% endif %}
{% if perms.core.view_sleep %}
<h6 class="dropdown-header"><i class="icon icon-sleep" aria-hidden="true"></i> Sleep</h6>
<a class="dropdown-item" href="{% url 'reports:report-sleep-pattern-child' object.slug %}">Sleep Pattern</a>
<a class="dropdown-item" href="{% url 'reports:report-sleep-totals-child' object.slug %}">Sleep Totals</a>
{% endif %}
<a class="dropdown-item" href="{% url 'reports:report-diaperchange-types-child' object.slug %}">Diaper Change Types</a>
<a class="dropdown-item" href="{% url 'reports:report-sleep-pattern-child' object.slug %}">Sleep Pattern</a>
<a class="dropdown-item" href="{% url 'reports:report-sleep-totals-child' object.slug %}">Sleep Totals</a>
<a class="dropdown-item" href="{% url 'reports:report-timeline-child' object.slug %}">Timeline</a>
</div>
</div>
</div>

View File

@ -251,40 +251,48 @@ def timeline(child, date):
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
'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
'model_name': instance.model_name,
'type': 'start'
})
events.append({
'time': timezone.localtime(instance.end),
'event': '{} finished feeding.'.format(instance.child.first_name),
'model_name': instance.model_name
'model_name': instance.model_name,
'type': 'end'
})
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
'model_name': instance.model_name,
'type': 'start'
})
events.append({
'time': timezone.localtime(instance.end),
'event': '{} woke up.'.format(instance.child.first_name),
'model_name': instance.model_name
'model_name': instance.model_name,
'type': 'end'
})
instances = TummyTime.objects.filter(child=child).filter(
start__range=(min_date, max_date)).order_by('-start')
for instance in instances:
@ -292,13 +300,17 @@ def timeline(child, date):
'time': timezone.localtime(instance.start),
'event': '{} started tummy time!'.format(
instance.child.first_name),
'model_name': instance.model_name
'model_name': instance.model_name,
'type': 'start'
})
events.append({
'time': timezone.localtime(instance.end),
'event': '{} finished tummy time.'.format(
instance.child.first_name),
'model_name': instance.model_name
'model_name': instance.model_name,
'type': 'end'
})
events.sort(key=lambda x: x['time'], reverse=True)
return events

View File

@ -6,11 +6,23 @@
{% block content %}
<h1 class="text-center">Timeline</h1>
<h2 class="text-center text-muted">{{ object }}</h2>
<h3 class="text-center">{{ date|date }}</h3>
<ul class="timeline">
<h3 class="text-center">
<a class="btn btn-sm btn-default" href="?date={{ date_previous|date:"Y-m-d" }}" aria-label="Previous">
<i class="icon icon-chevron-left" aria-hidden="true"></i>
<span class="sr-only">Previous</span>
</a>
{{ date|date }}
<a class="btn btn-sm btn-default" href="?date={{ date_next|date:"Y-m-d" }}" aria-label="Next">
<i class="icon icon-chevron-right" aria-hidden="true"></i>
<span class="sr-only">Next</span>
</a>
</h3>
<ul class="timeline m-auto">
{% for object in objects %}
<li>
<div class="timeline-badge"><i class="icon icon-{{ object.model_name }}"></i></div>
<li{% cycle "" ' class="timeline-inverted"' %}>
<div class="timeline-badge {% if object.type == "start" %}bg-success{% elif object.type == "end" %}bg-danger{% else %}bg-info{% endif %}">
<i class="icon icon-{{ object.model_name }}"></i>
</div>
<div class="card text-right">
<div class="card-body">
{{ object.event }}

View File

@ -70,9 +70,11 @@ 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())
date = self.request.GET.get('date', str(timezone.now().date()))
date = timezone.datetime.strptime(date, '%Y-%m-%d')
date = timezone.localtime(timezone.make_aware(date))
context['objects'] = timeline(self.object, date)
context['date'] = date
context['date_previous'] = date - timezone.timedelta(days=1)
context['date_next'] = date + timezone.timedelta(days=1)
return context