From 853ab54b8c179bfabe739318767baabf7c6e629e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20H=C3=A4ublein?= Date: Sun, 15 Mar 2020 17:18:21 +0100 Subject: [PATCH 1/2] add enhanced feedings statistics --- dashboard/templatetags/cards.py | 47 ++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/dashboard/templatetags/cards.py b/dashboard/templatetags/cards.py index da0f8de3..52af5c2c 100644 --- a/dashboard/templatetags/cards.py +++ b/dashboard/templatetags/cards.py @@ -5,6 +5,8 @@ from django.db.models.functions import TruncDate from django.utils import timezone from django.utils.translation import gettext as _ +from datetime import date, datetime, time + from core import models @@ -170,10 +172,11 @@ def card_statistics(child): 'title': _('Diaper change frequency')}) feedings = _feeding_statistics(child) - stats.append({ - 'type': 'duration', - 'stat': feedings['btwn_average'], - 'title': _('Feeding frequency')}) + for item in feedings: + stats.append({ + 'type': 'duration', + 'stat': item['btwn_average'], + 'title': item['title']}) naps = _nap_statistics(child) stats.append({ @@ -235,22 +238,40 @@ def _feeding_statistics(child): :param child: an instance of the Child model. :returns: a dictionary of statistics. """ + feedings = [ + { + 'start': timezone.now()-timezone.timedelta(days=3), + 'title': _('Feeding frequency')+ " 3 "+_("days") + }, + { + 'start': timezone.now()-timezone.timedelta(weeks=2), + 'title': _('Feeding frequency') + " 2 "+_("weeks") + }, + { + 'start': timezone.make_aware(datetime.combine(date.min, time(0,0)) + timezone.timedelta(days=1) ), + 'title': _('Feeding frequency') + } + ] + for timespan in feedings: + timespan['btwn_total'] = timezone.timedelta(0) + timespan['btwn_count'] = 0 + timespan['btwn_average'] = 0.0 + instances = models.Feeding.objects.filter(child=child).order_by('start') - feedings = { - 'btwn_total': timezone.timedelta(0), - 'btwn_count': instances.count() - 1, - 'btwn_average': 0.0} last_instance = None for instance in instances: if last_instance: - feedings['btwn_total'] += instance.start - last_instance.end + for timespan in feedings: + if last_instance.start > timespan['start']: + timespan['btwn_total'] += instance.start - last_instance.end + timespan['btwn_count'] += 1 last_instance = instance - if feedings['btwn_count'] > 0: - feedings['btwn_average'] = \ - feedings['btwn_total'] / feedings['btwn_count'] - + for timespan in feedings: + if timespan['btwn_count'] > 0: + timespan['btwn_average'] = \ + timespan['btwn_total'] / timespan['btwn_count'] return feedings From aeba14f714c37c56611858ff9ebc9e7b15353bda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20H=C3=A4ublein?= Date: Mon, 16 Mar 2020 18:53:36 +0100 Subject: [PATCH 2/2] linting --- dashboard/templatetags/cards.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/dashboard/templatetags/cards.py b/dashboard/templatetags/cards.py index 52af5c2c..4cdf6b6b 100644 --- a/dashboard/templatetags/cards.py +++ b/dashboard/templatetags/cards.py @@ -240,15 +240,17 @@ def _feeding_statistics(child): """ feedings = [ { - 'start': timezone.now()-timezone.timedelta(days=3), - 'title': _('Feeding frequency')+ " 3 "+_("days") - }, - { - 'start': timezone.now()-timezone.timedelta(weeks=2), - 'title': _('Feeding frequency') + " 2 "+_("weeks") + 'start': timezone.now()-timezone.timedelta(days=3), + 'title': _('Feeding frequency (past 3 days)') }, { - 'start': timezone.make_aware(datetime.combine(date.min, time(0,0)) + timezone.timedelta(days=1) ), + 'start': timezone.now()-timezone.timedelta(weeks=2), + 'title': _('Feeding frequency (past 2 weeks)') + }, + { + 'start': timezone.make_aware( + datetime.combine(date.min, time(0, 0)) + + timezone.timedelta(days=1)), 'title': _('Feeding frequency') } ] @@ -264,7 +266,8 @@ def _feeding_statistics(child): if last_instance: for timespan in feedings: if last_instance.start > timespan['start']: - timespan['btwn_total'] += instance.start - last_instance.end + timespan['btwn_total'] += (instance.start + - last_instance.end) timespan['btwn_count'] += 1 last_instance = instance