Merge pull request #149 from JeanFred/add-card-feeding-day

Add a "Today's Feeding" card
This commit is contained in:
Christopher Charbonneau Wells 2020-07-21 19:17:44 -07:00 committed by GitHub
commit 72c157c661
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 0 deletions

View File

@ -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 %}

View File

@ -13,6 +13,7 @@
<div id="dashboard-child" class="card-columns"> <div id="dashboard-child" class="card-columns">
{% card_feeding_last object %} {% card_feeding_last object %}
{% card_feeding_last_method object %} {% card_feeding_last_method object %}
{% card_feeding_day object %}
{% card_timer_list object %} {% card_timer_list object %}
{% card_statistics object %} {% card_statistics object %}
{% card_sleep_last object %} {% card_sleep_last object %}

View File

@ -68,6 +68,31 @@ def card_diaperchange_types(child, date=None):
return {'type': 'diaperchange', 'stats': stats, 'total': week_total} 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') @register.inclusion_tag('cards/feeding_last.html')
def card_feeding_last(child): def card_feeding_last(child):
""" """

View File

@ -46,6 +46,12 @@ class TemplateTagsTestCase(TestCase):
} }
self.assertEqual(data['stats'], stats) 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): def test_card_feeding_last(self):
data = cards.card_feeding_last(self.child) data = cards.card_feeding_last(self.child)
self.assertEqual(data['type'], 'feeding') self.assertEqual(data['type'], 'feeding')