From f29515a04dac5653afc4c8824f7bc9650067a343 Mon Sep 17 00:00:00 2001 From: Jean-Frederic Berthelot Date: Tue, 21 Jul 2020 00:33:59 +0200 Subject: [PATCH] Add a "Today's Feeding" card It is useful to know today's cumulated Feeding amount. Right now, this can be inferred from the "Feeding Amount" report, but is not made readily available in the dashboard, where the Feeding-related cards are the last Feeding and its method. This adds a new card `Today's Feeding`, heavily based on "Today's Sleep". --- dashboard/templates/cards/feeding_day.html | 18 ++++++++++++++++ dashboard/templates/dashboard/child.html | 1 + dashboard/templatetags/cards.py | 25 ++++++++++++++++++++++ dashboard/tests/tests_templatetags.py | 6 ++++++ 4 files changed, 50 insertions(+) create mode 100644 dashboard/templates/cards/feeding_day.html diff --git a/dashboard/templates/cards/feeding_day.html b/dashboard/templates/cards/feeding_day.html new file mode 100644 index 00000000..e67b2693 --- /dev/null +++ b/dashboard/templates/cards/feeding_day.html @@ -0,0 +1,18 @@ +{% extends 'cards/base.html' %} +{% load duration i18n %} + +{% block header %}{% trans "Today's Feeding" %}{% endblock %} + +{% block title %} + {% if total %} + {{ total }} + {% else %} + {% trans "None" %} + {% endif %} +{% endblock %} + +{% block content %} + {% if count > 0 %} + {% blocktrans %}{{ count }} feeding entries{% endblocktrans %} + {% endif %} +{% endblock %} \ No newline at end of file diff --git a/dashboard/templates/dashboard/child.html b/dashboard/templates/dashboard/child.html index 604de7b8..7ecfc0ed 100644 --- a/dashboard/templates/dashboard/child.html +++ b/dashboard/templates/dashboard/child.html @@ -13,6 +13,7 @@
{% card_feeding_last object %} {% card_feeding_last_method object %} + {% card_feeding_day object %} {% card_timer_list object %} {% card_statistics object %} {% card_sleep_last object %} diff --git a/dashboard/templatetags/cards.py b/dashboard/templatetags/cards.py index 78b2382c..9e324f10 100644 --- a/dashboard/templatetags/cards.py +++ b/dashboard/templatetags/cards.py @@ -68,6 +68,31 @@ def card_diaperchange_types(child, date=None): return {'type': 'diaperchange', 'stats': stats, 'total': week_total} +@register.inclusion_tag('cards/feeding_day.html') +def card_feeding_day(child, date=None): + """ + Filters Feeding instances to get total amount for a specific date. + :param child: an instance of the Child model. + :param date: a Date object for the day to filter. + :returns: a dict with count and total amount for the Feeding instances. + """ + if not date: + date = timezone.localtime().date() + instances = models.Feeding.objects.filter(child=child).filter( + start__year=date.year, + start__month=date.month, + start__day=date.day) \ + | models.Feeding.objects.filter(child=child).filter( + end__year=date.year, + end__month=date.month, + end__day=date.day) + + total = sum([instance.amount for instance in instances if instance.amount]) + count = len(instances) + + return {'type': 'feeding', 'total': total, 'count': count} + + @register.inclusion_tag('cards/feeding_last.html') def card_feeding_last(child): """ diff --git a/dashboard/tests/tests_templatetags.py b/dashboard/tests/tests_templatetags.py index dcd9fb74..36934f95 100644 --- a/dashboard/tests/tests_templatetags.py +++ b/dashboard/tests/tests_templatetags.py @@ -46,6 +46,12 @@ class TemplateTagsTestCase(TestCase): } self.assertEqual(data['stats'], stats) + def test_card_feeding_day(self): + data = cards.card_feeding_day(self.child, self.date) + self.assertEqual(data['type'], 'feeding') + self.assertEqual(data['total'], 2.5) + self.assertEqual(data['count'], 3) + def test_card_feeding_last(self): data = cards.card_feeding_last(self.child) self.assertEqual(data['type'], 'feeding')