Add an active timers list card.

This commit is contained in:
Christopher Charbonneau Wells 2017-09-25 12:13:37 -04:00
parent 4be3d66a85
commit 17abac0829
4 changed files with 85 additions and 39 deletions

View File

@ -0,0 +1,14 @@
<div class="card card-timer border-secondary mb-3">
<div class="card-header text-white bg-secondary h4">
<i class="icon icon-timer pull-left" aria-hidden="true"></i>
{% block header %}{% endblock %}
</div>
<div class="card-body text-secondary">
<h4 class="card-title">{% block title %}{% endblock %}</h4>
<div class="card-text">{% block content %}{% endblock %}</div>
</div>
{% block listgroup %}{% endblock %}
<div class="card-footer text-muted">
{% block footer %}{% endblock %}
</div>
</div>

View File

@ -0,0 +1,23 @@
{% extends 'cards/timer.html' %}
{% block header %}Active Timers{% endblock %}
{% block title %}
{% with instances|length as count %}
<strong>{{ count }}</strong> active timer{{ count|pluralize }}
{% endwith %}
{% endblock %}
{% block listgroup %}
<ul class="list-group list-group-flush">
{% for instance in instances %}
<a href="{% url 'timer-detail' instance.id %}" class="list-group-item list-group-item-action">
<strong>{{ instance }}</strong> <p class="text-muted small m-0">Started by {{ instance.user }} at {{ instance.start|time }}</p>
</a>
{% endfor %}
</ul>
{% endblock %}
{% block footer %}
Timers help us time things...
{% endblock %}

View File

@ -1,5 +1,5 @@
{% extends 'babyblotter/page.html' %} {% extends 'babyblotter/page.html' %}
{% load dashboard %} {% load cards %}
{% block title %}Dashboard - {{ object }}{% endblock %} {% block title %}Dashboard - {{ object }}{% endblock %}
@ -28,6 +28,7 @@
<div class="col-lg-4 col-md-6 col-sm-12"> <div class="col-lg-4 col-md-6 col-sm-12">
{% card_feeding_last object %} {% card_feeding_last object %}
{% card_feeding_last_method object %} {% card_feeding_last_method object %}
{% card_timer_list %}
</div> </div>
<div class="col-lg-4 col-md-6 col-sm-12"> <div class="col-lg-4 col-md-6 col-sm-12">
{% card_sleep_last object %} {% card_sleep_last object %}

View File

@ -4,28 +4,12 @@ from __future__ import unicode_literals
from django import template from django import template
from django.utils import timezone from django.utils import timezone
from core.models import DiaperChange, Feeding, Sleep, TummyTime from core.models import DiaperChange, Feeding, Sleep, Timer, TummyTime
register = template.Library() register = template.Library()
@register.inclusion_tag('cards/feeding_last.html')
def card_feeding_last(child):
"""Information about the most recent feeding.
"""
instance = Feeding.objects.filter(child=child).order_by('-end').first()
return {'feeding': instance}
@register.inclusion_tag('cards/feeding_last_method.html')
def card_feeding_last_method(child):
"""Information about the most recent feeding _method_.
"""
instance = Feeding.objects.filter(child=child).order_by('-end').first()
return {'feeding': instance}
@register.inclusion_tag('cards/diaperchange_last.html') @register.inclusion_tag('cards/diaperchange_last.html')
def card_diaperchange_last(child): def card_diaperchange_last(child):
"""Information about the most recent diaper change. """Information about the most recent diaper change.
@ -48,7 +32,7 @@ def card_diaperchange_types(child):
for x in range(6): for x in range(6):
stats[x] = {'wet': 0, 'solid': 0} stats[x] = {'wet': 0, 'solid': 0}
instances = DiaperChange.objects.filter(child=child)\ instances = DiaperChange.objects.filter(child=child) \
.filter(time__gt=min_date).filter(time__lt=max_date).order_by('-time') .filter(time__gt=min_date).filter(time__lt=max_date).order_by('-time')
for instance in instances: for instance in instances:
key = (max_date - instance.time).days key = (max_date - instance.time).days
@ -66,30 +50,20 @@ def card_diaperchange_types(child):
return {'stats': stats, 'last_change': instances.first()} return {'stats': stats, 'last_change': instances.first()}
@register.inclusion_tag('cards/tummytime_last.html') @register.inclusion_tag('cards/feeding_last.html')
def card_tummytime_last(child): def card_feeding_last(child):
"""Information about the most recent tummy time. """Information about the most recent feeding.
""" """
instance = TummyTime.objects.filter(child=child).order_by('-end').first() instance = Feeding.objects.filter(child=child).order_by('-end').first()
return {'tummytime': instance} return {'feeding': instance}
@register.inclusion_tag('cards/tummytime_day.html') @register.inclusion_tag('cards/feeding_last_method.html')
def card_tummytime_day(child, date=None): def card_feeding_last_method(child):
"""Tummy time over the course of `date`. """Information about the most recent feeding _method_.
""" """
if not date: instance = Feeding.objects.filter(child=child).order_by('-end').first()
date = timezone.localtime().date() return {'feeding': instance}
instances = TummyTime.objects.filter(
child=child, end__day=date.day).order_by('-end')
stats = {
'total': timezone.timedelta(seconds=0),
'count': instances.count()
}
for instance in instances:
stats['total'] += timezone.timedelta(
seconds=instance.duration_td().seconds)
return {'stats': stats, 'instances': instances, 'last': instances.first()}
@register.inclusion_tag('cards/sleep_last.html') @register.inclusion_tag('cards/sleep_last.html')
@ -128,3 +102,37 @@ def card_sleep_day(child, date=None):
average = 0 average = 0
return {'total': total, 'count': count, 'average': average} return {'total': total, 'count': count, 'average': average}
@register.inclusion_tag('cards/timer_list.html')
def card_timer_list():
"""Information about currently active timers.
"""
instances = Timer.objects.filter(active=True).order_by('-start')
return {'instances': list(instances)}
@register.inclusion_tag('cards/tummytime_last.html')
def card_tummytime_last(child):
"""Information about the most recent tummy time.
"""
instance = TummyTime.objects.filter(child=child).order_by('-end').first()
return {'tummytime': instance}
@register.inclusion_tag('cards/tummytime_day.html')
def card_tummytime_day(child, date=None):
"""Tummy time over the course of `date`.
"""
if not date:
date = timezone.localtime().date()
instances = TummyTime.objects.filter(
child=child, end__day=date.day).order_by('-end')
stats = {
'total': timezone.timedelta(seconds=0),
'count': instances.count()
}
for instance in instances:
stats['total'] += timezone.timedelta(
seconds=instance.duration_td().seconds)
return {'stats': stats, 'instances': instances, 'last': instances.first()}