Don't include duration on timeline entries with no duration

This commit is contained in:
Christopher C. Wells 2023-10-20 19:50:49 -07:00 committed by Christopher Charbonneau Wells
parent 9baec8d906
commit 31f8f951ad
2 changed files with 47 additions and 40 deletions

View File

@ -1,10 +1,12 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from datetime import timedelta
from django.urls import reverse from django.urls import reverse
from django.utils import timezone, timesince 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, Note, Sleep, TummyTime, Temperature from core.models import DiaperChange, Feeding, Note, Sleep, TummyTime, Temperature
from datetime import timedelta from core.utils import duration_string
def get_objects(date, child=None): def get_objects(date, child=None):
@ -60,19 +62,21 @@ def _add_tummy_times(min_date, max_date, events, child=None):
"tags": instance.tags.all(), "tags": instance.tags.all(),
} }
) )
events.append(
{ end = {
"time": timezone.localtime(instance.end), "time": timezone.localtime(instance.end),
"event": _("%(child)s finished tummy time.") "event": _("%(child)s finished tummy time.")
% {"child": instance.child.first_name}, % {"child": instance.child.first_name},
"details": details, "details": details,
"edit_link": edit_link, "edit_link": edit_link,
"duration": timesince.timesince(instance.start, now=instance.end),
"model_name": instance.model_name, "model_name": instance.model_name,
"type": "end", "type": "end",
"tags": instance.tags.all(), "tags": instance.tags.all(),
} }
) if instance.duration > timedelta(seconds=0):
end["duration"] = duration_string(instance.duration)
events.append(end)
def _add_sleeps(min_date, max_date, events, child=None): def _add_sleeps(min_date, max_date, events, child=None):
@ -98,18 +102,19 @@ def _add_sleeps(min_date, max_date, events, child=None):
"tags": instance.tags.all(), "tags": instance.tags.all(),
} }
) )
events.append(
{ end = {
"time": timezone.localtime(instance.end), "time": timezone.localtime(instance.end),
"event": _("%(child)s woke up.") % {"child": instance.child.first_name}, "event": _("%(child)s woke up.") % {"child": instance.child.first_name},
"details": details, "details": details,
"edit_link": edit_link, "edit_link": edit_link,
"duration": timesince.timesince(instance.start, now=instance.end),
"model_name": instance.model_name, "model_name": instance.model_name,
"type": "end", "type": "end",
"tags": instance.tags.all(), "tags": instance.tags.all(),
} }
) if instance.duration > timedelta(seconds=0):
end["duration"] = duration_string(instance.duration)
events.append(end)
def _add_feedings(min_date, max_date, events, child=None): def _add_feedings(min_date, max_date, events, child=None):
@ -153,8 +158,8 @@ def _add_feedings(min_date, max_date, events, child=None):
"tags": instance.tags.all(), "tags": instance.tags.all(),
} }
) )
events.append(
{ end = {
"time": timezone.localtime(instance.end), "time": timezone.localtime(instance.end),
"event": _("%(child)s finished feeding.") "event": _("%(child)s finished feeding.")
% {"child": instance.child.first_name}, % {"child": instance.child.first_name},
@ -165,7 +170,9 @@ def _add_feedings(min_date, max_date, events, child=None):
"type": "end", "type": "end",
"tags": instance.tags.all(), "tags": instance.tags.all(),
} }
) if instance.duration > timedelta(seconds=0):
end["duration"] = duration_string(instance.duration)
events.append(end)
def _add_diaper_changes(min_date, max_date, events, child): def _add_diaper_changes(min_date, max_date, events, child):

View File

@ -38,7 +38,7 @@ def duration_string(duration, precision="s"):
duration = "" duration = ""
if h > 0: if h > 0:
duration = ngettext("%(hours)s hour", "%(hours)s hours", h) % {"hours": h} duration = ngettext("%(hours)s hour", "%(hours)s hours", h) % {"hours": h}
if m >= 0 and precision != "h": if m > 0 and precision != "h":
if duration != "": if duration != "":
duration += ", " duration += ", "
duration += ngettext("%(minutes)s minute", "%(minutes)s minutes", m) % { duration += ngettext("%(minutes)s minute", "%(minutes)s minutes", m) % {