Add "time since previous" to feedings in timeline

Fixes #266
This commit is contained in:
Ohad Lutzky 2021-08-05 22:13:21 +01:00 committed by Christopher Charbonneau Wells
parent ce507761cd
commit 508292317f
2 changed files with 18 additions and 1 deletions

View File

@ -67,6 +67,11 @@
<small>Duration: {{ object.duration }}</small> <small>Duration: {{ object.duration }}</small>
</div> </div>
{% endif %} {% endif %}
{% if object.time_since_prev %}
<div>
<small>{{ object.time_since_prev }} since previous</small>
</div>
{% endif %}
{% if object.edit_link %} {% if object.edit_link %}
<div> <div>
<small><a href="{{ object.edit_link }}">Edit</a></small> <small><a href="{{ object.edit_link }}">Edit</a></small>

View File

@ -4,6 +4,7 @@ from django.utils import timezone, timesince
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from core.models import DiaperChange, Feeding, Sleep, TummyTime from core.models import DiaperChange, Feeding, Sleep, TummyTime
from datetime import timedelta
def get_objects(child, date): def get_objects(child, date):
@ -80,9 +81,19 @@ def _add_sleeps(child, min_date, max_date, events):
def _add_feedings(child, min_date, max_date, events): def _add_feedings(child, min_date, max_date, events):
yesterday = min_date - timedelta(days=1) # So first feeding has a previous
prev_start = None
instances = Feeding.objects.filter(child=child).filter( instances = Feeding.objects.filter(child=child).filter(
start__range=(min_date, max_date)).order_by('-start') start__range=(yesterday, max_date)).order_by('start')
for instance in instances: for instance in instances:
time_since_prev = None
if prev_start:
time_since_prev = \
timesince.timesince(prev_start, now=instance.start)
prev_start = instance.start
if instance.start < min_date:
continue
details = None details = None
edit_link = reverse('core:feeding-update', args=[instance.id]) edit_link = reverse('core:feeding-update', args=[instance.id])
if instance.amount: if instance.amount:
@ -96,6 +107,7 @@ def _add_feedings(child, min_date, max_date, events):
}, },
'details': details, 'details': details,
'edit_link': edit_link, 'edit_link': edit_link,
'time_since_prev': time_since_prev,
'model_name': instance.model_name, 'model_name': instance.model_name,
'type': 'start' 'type': 'start'
}) })