Display per-child timer lists on dashboard card

This commit is contained in:
Christopher C. Wells 2020-01-28 22:36:08 -08:00 committed by Christopher Charbonneau Wells
parent e5a5e330b9
commit eb9e2f3927
2 changed files with 13 additions and 5 deletions

View File

@ -13,7 +13,7 @@
<div id="dashboard-child" class="card-columns">
{% card_feeding_last object %}
{% card_feeding_last_method object %}
{% card_timer_list %}
{% card_timer_list object %}
{% card_statistics object %}
{% card_sleep_last object %}
{% card_sleep_day object %}

View File

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
from django import template
from django.db.models import Avg, Count, Sum
from django.db.models import Avg, Count, Q, Sum
from django.db.models.functions import TruncDate
from django.utils import timezone
from django.utils.translation import gettext as _
@ -324,11 +324,19 @@ def _weight_statistics(child):
@register.inclusion_tag('cards/timer_list.html')
def card_timer_list():
def card_timer_list(child=None):
"""
Filters for currently active Timer instances.
Filters for currently active Timer instances, optionally by child.
:param child: an instance of the Child model.
:returns: a dictionary with a list of active Timer instances.
"""
if child:
# Get active instances for the selected child _or_ None (no child).
instances = models.Timer.objects.filter(
Q(active=True),
Q(child=child) | Q(child=None)
).order_by('-start')
else:
instances = models.Timer.objects.filter(active=True).order_by('-start')
return {'type': 'timer', 'instances': list(instances)}