Use day precision for child age (#167)

This commit is contained in:
Christopher C. Wells 2020-10-25 20:06:21 -07:00
parent 19c91822e0
commit 95a2cdf06d
4 changed files with 41 additions and 8 deletions

View File

@ -1,5 +1,5 @@
{% extends 'babybuddy/page.html' %}
{% load i18n static thumbnail %}
{% load i18n static thumbnail duration %}
{% block title %}{{ object }}{% endblock %}
@ -21,7 +21,7 @@
<div class="child-name display-4">{{ object }}</div>
<p class="lead">
{% trans "Born" %} <span class="text-secondary">{{ object.birth_date }}</span><br/>
{% trans "Age" %} <span class="text-secondary">{{ object.birth_date|timesince }}</span>
{% trans "Age" %} <span class="text-secondary">{{ object.birth_date|child_age_string }}</span>
</p>
{% include 'dashboard/child_button_group.html' %}
</div>

View File

@ -1,12 +1,27 @@
# -*- coding: utf-8 -*-
from django import template
from core.utils import duration_parts, duration_string as d_string
from core import utils
register = template.Library()
@register.filter
def child_age_string(birth_date):
"""
Format a Child's age with monkey-patched timesince.
:param birth_date: datetime instance
:return: a string representation of time since `birth_date`.
"""
if not birth_date:
return ''
try:
return utils.child_age_string(birth_date)
except (ValueError, TypeError):
return ''
@register.filter
def duration_string(duration, precision='s'):
"""
@ -19,7 +34,7 @@ def duration_string(duration, precision='s'):
if not duration:
return ''
try:
return d_string(duration, precision)
return utils.duration_string(duration, precision)
except (ValueError, TypeError):
return ''
@ -34,7 +49,7 @@ def hours(duration):
if not duration:
return 0
try:
h, m, s = duration_parts(duration)
h, m, s = utils.duration_parts(duration)
return h
except (ValueError, TypeError):
return 0
@ -50,7 +65,7 @@ def minutes(duration):
if not duration:
return 0
try:
h, m, s = duration_parts(duration)
h, m, s = utils.duration_parts(duration)
return m
except (ValueError, TypeError):
return 0
@ -66,7 +81,7 @@ def seconds(duration):
if not duration:
return 0
try:
h, m, s = duration_parts(duration)
h, m, s = utils.duration_parts(duration)
return s
except (ValueError, TypeError):
return 0

View File

@ -16,6 +16,12 @@ class TemplateTagsTestCase(TestCase):
bootstrap.bool_icon(False),
'<i class="icon icon-false text-danger" aria-hidden="true"></i>')
def test_child_age_string(self):
date = timezone.localdate() - timezone.timedelta(days=0, hours=6)
self.assertEqual('0\xa0days', duration.child_age_string(date))
date = timezone.localdate() - timezone.timedelta(days=1, hours=6)
self.assertEqual('1\xa0day', duration.child_age_string(date))
def test_duration_duration_string(self):
delta = timezone.timedelta(hours=1, minutes=30, seconds=15)
self.assertEqual(

View File

@ -1,8 +1,20 @@
# -*- coding: utf-8 -*-
from django.utils import timezone
from django.utils import timesince, timezone
from django.utils.translation import ngettext
def child_age_string(duration):
"""Monkey patch timesince function to day precision only.
"""
timesince.TIMESINCE_CHUNKS = (
(60 * 60 * 24 * 365, 'year'),
(60 * 60 * 24 * 30, 'month'),
(60 * 60 * 24 * 7, 'week'),
(60 * 60 * 24, 'day'),
)
return timesince.timesince(duration)
def duration_string(duration, precision='s'):
"""Format hours, minutes and seconds as a human-friendly string (e.g. "2
hours, 25 minutes, 31 seconds") with precision to h = hours, m = minutes or