Move duration tools to a separate template tag collection.

This commit is contained in:
Christopher Charbonneau Wells 2017-08-20 10:04:20 -04:00
parent 6f4774ae9e
commit 21e4af6f58
8 changed files with 64 additions and 38 deletions

View File

@ -1,6 +1,6 @@
{% extends 'babyblotter/page.html' %}
{% load widget_tweaks %}
{% load timers %}
{% load duration %}
{% block title %}Feedings{% endblock %}

View File

@ -1,6 +1,6 @@
{% extends 'babyblotter/page.html' %}
{% load widget_tweaks %}
{% load timers %}
{% load duration %}
{% block title %}Sleep{% endblock %}

View File

@ -1,11 +1,16 @@
{% extends 'babyblotter/page.html' %}
{% load timers %}
{% load duration %}
{% block title %}{{ object }}{% endblock %}
{% block content %}
<div class="jumbotron text-center">
<h1 class="display-1">{{ object.current_duration|duration_string_short }}</h1>
<h1 class="display-1">
<span id="timer_hours">{{ object.current_duration|hours }}</span>h
<span id="timer_minutes">{{ object.current_duration|minutes }}</span>m
<span id="timer_seconds">{{ object.current_duration|seconds }}</span>s
</h1>
<p class="lead text-muted">Started {{ object.start }} by {{ object.user }}</p>
{% if perms.core.add_feeding %}

View File

@ -1,6 +1,6 @@
{% extends 'babyblotter/page.html' %}
{% load widget_tweaks %}
{% load timers %}
{% load duration %}
{% block title %}Tummy Time{% endblock %}

View File

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

View File

@ -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)

View File

@ -1,5 +1,5 @@
{% extends 'cards/sleep.html' %}
{% load timers %}
{% load duration %}
{% block header %}Last Slept{% endblock %}

View File

@ -1,5 +1,5 @@
{% extends 'cards/tummytime.html' %}
{% load timers %}
{% load duration %}
{% block header %}Last Tummy Time{% endblock %}