From 31f8f951adf0aed10ce2e13e2f2e6d4a5fa216c4 Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Fri, 20 Oct 2023 19:50:49 -0700 Subject: [PATCH] Don't include duration on timeline entries with no duration --- core/timeline.py | 85 ++++++++++++++++++++++++++---------------------- core/utils.py | 2 +- 2 files changed, 47 insertions(+), 40 deletions(-) diff --git a/core/timeline.py b/core/timeline.py index 7dcd69fb..9b204afb 100644 --- a/core/timeline.py +++ b/core/timeline.py @@ -1,10 +1,12 @@ # -*- coding: utf-8 -*- +from datetime import timedelta + from django.urls import reverse from django.utils import timezone, timesince from django.utils.translation import gettext as _ 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): @@ -60,19 +62,21 @@ def _add_tummy_times(min_date, max_date, events, child=None): "tags": instance.tags.all(), } ) - events.append( - { - "time": timezone.localtime(instance.end), - "event": _("%(child)s finished tummy time.") - % {"child": instance.child.first_name}, - "details": details, - "edit_link": edit_link, - "duration": timesince.timesince(instance.start, now=instance.end), - "model_name": instance.model_name, - "type": "end", - "tags": instance.tags.all(), - } - ) + + end = { + "time": timezone.localtime(instance.end), + "event": _("%(child)s finished tummy time.") + % {"child": instance.child.first_name}, + "details": details, + "edit_link": edit_link, + "model_name": instance.model_name, + "type": "end", + "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): @@ -98,18 +102,19 @@ def _add_sleeps(min_date, max_date, events, child=None): "tags": instance.tags.all(), } ) - events.append( - { - "time": timezone.localtime(instance.end), - "event": _("%(child)s woke up.") % {"child": instance.child.first_name}, - "details": details, - "edit_link": edit_link, - "duration": timesince.timesince(instance.start, now=instance.end), - "model_name": instance.model_name, - "type": "end", - "tags": instance.tags.all(), - } - ) + + end = { + "time": timezone.localtime(instance.end), + "event": _("%(child)s woke up.") % {"child": instance.child.first_name}, + "details": details, + "edit_link": edit_link, + "model_name": instance.model_name, + "type": "end", + "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): @@ -153,19 +158,21 @@ def _add_feedings(min_date, max_date, events, child=None): "tags": instance.tags.all(), } ) - events.append( - { - "time": timezone.localtime(instance.end), - "event": _("%(child)s finished feeding.") - % {"child": instance.child.first_name}, - "details": details, - "edit_link": edit_link, - "duration": timesince.timesince(instance.start, now=instance.end), - "model_name": instance.model_name, - "type": "end", - "tags": instance.tags.all(), - } - ) + + end = { + "time": timezone.localtime(instance.end), + "event": _("%(child)s finished feeding.") + % {"child": instance.child.first_name}, + "details": details, + "edit_link": edit_link, + "duration": timesince.timesince(instance.start, now=instance.end), + "model_name": instance.model_name, + "type": "end", + "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): diff --git a/core/utils.py b/core/utils.py index 9acfc014..71f0e164 100644 --- a/core/utils.py +++ b/core/utils.py @@ -38,7 +38,7 @@ def duration_string(duration, precision="s"): duration = "" if h > 0: 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 != "": duration += ", " duration += ngettext("%(minutes)s minute", "%(minutes)s minutes", m) % {