From 21e4af6f58f46b29c2caf76e012f9b571fda2477 Mon Sep 17 00:00:00 2001 From: Christopher Charbonneau Wells Date: Sun, 20 Aug 2017 10:04:20 -0400 Subject: [PATCH] Move duration tools to a separate template tag collection. --- core/templates/core/feeding_list.html | 2 +- core/templates/core/sleep_list.html | 2 +- core/templates/core/timer_detail.html | 7 ++- core/templates/core/tummytime_list.html | 2 +- core/templatetags/duration.py | 53 +++++++++++++++++++ core/templatetags/timers.py | 32 ----------- dashboard/templates/cards/sleep_last.html | 2 +- dashboard/templates/cards/tummytime_last.html | 2 +- 8 files changed, 64 insertions(+), 38 deletions(-) create mode 100644 core/templatetags/duration.py diff --git a/core/templates/core/feeding_list.html b/core/templates/core/feeding_list.html index 8a2fda66..9fa11c77 100644 --- a/core/templates/core/feeding_list.html +++ b/core/templates/core/feeding_list.html @@ -1,6 +1,6 @@ {% extends 'babyblotter/page.html' %} {% load widget_tweaks %} -{% load timers %} +{% load duration %} {% block title %}Feedings{% endblock %} diff --git a/core/templates/core/sleep_list.html b/core/templates/core/sleep_list.html index e5e268ad..7dc0c12a 100644 --- a/core/templates/core/sleep_list.html +++ b/core/templates/core/sleep_list.html @@ -1,6 +1,6 @@ {% extends 'babyblotter/page.html' %} {% load widget_tweaks %} -{% load timers %} +{% load duration %} {% block title %}Sleep{% endblock %} diff --git a/core/templates/core/timer_detail.html b/core/templates/core/timer_detail.html index 2734a233..f5c9db08 100644 --- a/core/templates/core/timer_detail.html +++ b/core/templates/core/timer_detail.html @@ -1,11 +1,16 @@ {% extends 'babyblotter/page.html' %} {% load timers %} +{% load duration %} {% block title %}{{ object }}{% endblock %} {% block content %}
-

{{ object.current_duration|duration_string_short }}

+

+ {{ object.current_duration|hours }}h + {{ object.current_duration|minutes }}m + {{ object.current_duration|seconds }}s +

Started {{ object.start }} by {{ object.user }}

{% if perms.core.add_feeding %} diff --git a/core/templates/core/tummytime_list.html b/core/templates/core/tummytime_list.html index a67cd2cd..e46bf0bb 100644 --- a/core/templates/core/tummytime_list.html +++ b/core/templates/core/tummytime_list.html @@ -1,6 +1,6 @@ {% extends 'babyblotter/page.html' %} {% load widget_tweaks %} -{% load timers %} +{% load duration %} {% block title %}Tummy Time{% endblock %} diff --git a/core/templatetags/duration.py b/core/templatetags/duration.py new file mode 100644 index 00000000..e12026b9 --- /dev/null +++ b/core/templatetags/duration.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from datetime import timedelta + +from django import template + + +register = template.Library() + + +@register.filter +def duration_string(duration): + h, m, s = _get_hms(duration) + + duration = '' + if h > 0: + duration = '{} hour{}'.format(h, 's' if h > 1 else '') + if m > 0: + duration += '{}{} minute{}'.format( + '' if duration is '' else ', ', m, 's' if m > 1 else '') + if s > 0: + duration += '{}{} second{}'.format( + '' if duration is '' else ', ', s, 's' if s > 1 else '') + + return duration + + +@register.filter +def hours(duration): + h, m, s = _get_hms(duration) + return h + + +@register.filter +def minutes(duration): + h, m, s = _get_hms(duration) + return m + + +@register.filter +def seconds(duration): + h, m, s = _get_hms(duration) + return s + + +def _get_hms(duration): + """Get hours, minutes and seconds from a timedelta.""" + if not isinstance(duration, timedelta): + return 0, 0, 0 + h, remainder = divmod(duration.seconds, 3600) + m, s = divmod(remainder, 60) + return h, m, s diff --git a/core/templatetags/timers.py b/core/templatetags/timers.py index 686f2041..7b6a2533 100644 --- a/core/templatetags/timers.py +++ b/core/templatetags/timers.py @@ -30,35 +30,3 @@ def timer_nav(context, active=True): @register.inclusion_tag('core/timer_add.html') def add_timer(success_url): return {'success_url': success_url} - - -@register.filter -def duration_string(duration): - if not isinstance(duration, timedelta): - return duration - - h, remainder = divmod(duration.seconds, 3600) - m, s = divmod(remainder, 60) - - duration = '' - if h > 0: - duration = '{} hour{}'.format(h, 's' if h > 1 else '') - if m > 0: - duration += '{}{} minute{}'.format( - '' if duration is '' else ', ', m, 's' if m > 1 else '') - if s > 0: - duration += '{}{} second{}'.format( - '' if duration is '' else ', ', s, 's' if s > 1 else '') - - return duration - - -@register.filter -def duration_string_short(duration): - if not isinstance(duration, timedelta): - return duration - - h, remainder = divmod(duration.seconds, 3600) - m, s = divmod(remainder, 60) - - return '{}h {}m {}s'.format(h, m, s) diff --git a/dashboard/templates/cards/sleep_last.html b/dashboard/templates/cards/sleep_last.html index 5f18a927..51b3315e 100644 --- a/dashboard/templates/cards/sleep_last.html +++ b/dashboard/templates/cards/sleep_last.html @@ -1,5 +1,5 @@ {% extends 'cards/sleep.html' %} -{% load timers %} +{% load duration %} {% block header %}Last Slept{% endblock %} diff --git a/dashboard/templates/cards/tummytime_last.html b/dashboard/templates/cards/tummytime_last.html index 9cbe11c2..ba3d10e8 100644 --- a/dashboard/templates/cards/tummytime_last.html +++ b/dashboard/templates/cards/tummytime_last.html @@ -1,5 +1,5 @@ {% extends 'cards/tummytime.html' %} -{% load timers %} +{% load duration %} {% block header %}Last Tummy Time{% endblock %}