Include past three methods is Last Feeding Method card (#117)

This commit is contained in:
Christopher C. Wells 2020-02-15 13:34:57 -08:00 committed by Christopher Charbonneau Wells
parent 785b87f66d
commit cb1712377b
3 changed files with 39 additions and 10 deletions

View File

@ -4,9 +4,35 @@
{% block header %}{% trans "Last Feeding Method" %}{% endblock %}
{% block title %}
{% if feeding %}
<div class="display-4 text-center">{{ feeding.get_method_display }}</div>
{% if feedings|length > 0 %}
<div id="feeding-methods-carousel" class="carousel slide" data-interval="false">
<div class="carousel-inner">
{% for feeding in feedings %}
<div class="carousel-item{% if forloop.counter == feedings|length %} active{% endif %}">
<div class="display-4 text-center">{{ feeding.get_method_display }}</div>
<div class="text-center small text-muted">
{% if forloop.last %}
{% trans "most recent" %}
{% else %}
{% blocktrans trimmed with n=forloop.revcounter0 plural=forloop.revcounter0|pluralize %}
{{ n }} feeding{{ plural }} ago
{% endblocktrans %}
{% endif %}
</div>
</div>
{% endfor %}
</div>
<a class="carousel-control-prev" href="#feeding-methods-carousel" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">{% trans "Previous" %}</span>
</a>
<a class="carousel-control-next" href="#feeding-methods-carousel" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">{% trans "Next" %}</span>
</a>
</div>
{% else %}
{% trans "None" %}
{% endif %}
{% endblock %}

View File

@ -79,13 +79,14 @@ def card_feeding_last(child):
@register.inclusion_tag('cards/feeding_last_method.html')
def card_feeding_last_method(child):
"""
Information about the most recent feeding method.
Information about the three most recent feeding methods.
:param child: an instance of the Child model.
:returns: a dictionary with the most recent Feeding instance.
:returns: a dictionary with the most recent Feeding instances.
"""
instance = models.Feeding.objects.filter(child=child) \
.order_by('-end').first()
return {'type': 'feeding', 'feeding': instance}
instances = models.Feeding.objects.filter(child=child) \
.order_by('-end')[:3]
# Results are reversed for carousel forward/back behavior.
return {'type': 'feeding', 'feedings': list(reversed(instances))}
@register.inclusion_tag('cards/sleep_last.html')

View File

@ -53,11 +53,13 @@ class TemplateTagsTestCase(TestCase):
self.assertEqual(data['feeding'], models.Feeding.objects.first())
def test_card_feeding_last_method(self):
data = cards.card_feeding_last(self.child)
data = cards.card_feeding_last_method(self.child)
self.assertEqual(data['type'], 'feeding')
self.assertIsInstance(data['feeding'], models.Feeding)
self.assertEqual(len(data['feedings']), 3)
for feeding in data['feedings']:
self.assertIsInstance(feeding, models.Feeding)
self.assertEqual(
data['feeding'].method,
data['feedings'][2].method,
models.Feeding.objects.first().method)
def test_card_sleep_last(self):