mirror of https://github.com/snachodog/mybuddy.git
Convert duration_string to a template tag (WIP).
This commit is contained in:
parent
c5fc35b177
commit
6d73cb69f6
|
@ -159,6 +159,12 @@ class Timer(models.Model):
|
|||
def __str__(self):
|
||||
return self.name or 'Timer #{}'.format(self.id)
|
||||
|
||||
def current_duration(self):
|
||||
if self.duration:
|
||||
return self.duration
|
||||
else:
|
||||
return timezone.now() - self.start
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
self.active = self.end is None
|
||||
self.name = self.name or None
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
{% extends 'babyblotter/page.html' %}
|
||||
{% load widget_tweaks %}
|
||||
{% load timers %}
|
||||
|
||||
{% block title %}Feedings{% endblock %}
|
||||
|
||||
|
@ -23,7 +24,7 @@
|
|||
<tr>
|
||||
<th scope="row">{{ feeding.child }}</th>
|
||||
<td>{{ feeding.start }}</td>
|
||||
<td>{{ feeding.duration }}</td>
|
||||
<td>{{ feeding.duration|duration_string }}</td>
|
||||
<td>{{ feeding.type }}</td>
|
||||
<td>{{ feeding.method }}</td>
|
||||
<td>{{ feeding.amount }}</td>
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
{% extends 'babyblotter/page.html' %}
|
||||
{% load widget_tweaks %}
|
||||
{% load timers %}
|
||||
|
||||
{% block title %}Sleep{% endblock %}
|
||||
|
||||
|
@ -22,7 +23,7 @@
|
|||
<th scope="row">{{ sleep.child }}</th>
|
||||
<td>{{ sleep.start }}</td>
|
||||
<td>{{ sleep.end }}</td>
|
||||
<td>{{ sleep.duration }}</td>
|
||||
<td>{{ sleep.duration|duration_string }}</td>
|
||||
<td class="text-center">
|
||||
<div class="btn-group btn-group-sm" role="group" aria-label="Actions">
|
||||
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
{% extends 'babyblotter/page.html' %}
|
||||
{% load timers %}
|
||||
|
||||
{% block title %}{{ object }}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="jumbotron text-center">
|
||||
<h1 class="display-1">{{ object.duration }}</h1>
|
||||
<h1 class="display-1">{{ object.current_duration|duration_string_short }}</h1>
|
||||
<p class="lead text-muted">Started {{ object.start }} by {{ object.user }}</p>
|
||||
|
||||
{% if perms.core.add_feeding %}
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
<div class="card border-success text-center mb-2">
|
||||
<div class="card-header text-white bg-success ">{{ timer.name }}</div>
|
||||
<div class="card-body text-success">
|
||||
<h4 class="card-text">{{ timer.duration }}</h4>
|
||||
<h4 class="card-text">{{ timer.current_duration }}</h4>
|
||||
</div>
|
||||
<div class="card-footer text-muted small">
|
||||
{{ timer.start }}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
{% extends 'babyblotter/page.html' %}
|
||||
{% load widget_tweaks %}
|
||||
{% load timers %}
|
||||
|
||||
{% block title %}Tummy Time{% endblock %}
|
||||
|
||||
|
@ -23,7 +24,7 @@
|
|||
<th scope="row">{{ tummytime.child }}</th>
|
||||
<td>{{ tummytime.start }}</td>
|
||||
<td>{{ tummytime.end }}</td>
|
||||
<td>{{ tummytime.duration }}</td>
|
||||
<td>{{ tummytime.duration|duration_string }}</td>
|
||||
<td>{{ tummytime.milestone }}</td>
|
||||
<td class="text-center">
|
||||
<div class="btn-group btn-group-sm" role="group" aria-label="Actions">
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from datetime import timedelta
|
||||
|
||||
from django import template
|
||||
|
||||
from core.models import Timer
|
||||
|
@ -29,3 +31,34 @@ def timer_nav(context, active=True):
|
|||
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)
|
||||
|
|
|
@ -1,33 +1,9 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from math import floor
|
||||
|
||||
from django.utils import timezone
|
||||
|
||||
|
||||
def duration_string(start, end, short=False):
|
||||
diff = end - start
|
||||
h = floor(diff.seconds / 3600)
|
||||
m = floor((diff.seconds - h * 3600) / 60)
|
||||
s = diff.seconds % 60
|
||||
|
||||
duration = ''
|
||||
if short:
|
||||
duration = '{}h {}m {}s'.format(h, m, s)
|
||||
else:
|
||||
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
|
||||
|
||||
|
||||
def filter_by_params(request, model, available_params):
|
||||
queryset = model.objects.all()
|
||||
|
||||
|
@ -39,8 +15,8 @@ def filter_by_params(request, model, available_params):
|
|||
return queryset
|
||||
|
||||
|
||||
# Stop a timer instance by setting it's end field.
|
||||
def timer_stop(timer_id, end=None):
|
||||
"""Stop a timer instance by setting it's end field."""
|
||||
if not end:
|
||||
end = timezone.now()
|
||||
from .models import Timer
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
{% extends 'cards/sleep.html' %}
|
||||
{% load timers %}
|
||||
|
||||
{% block header %}Last Slept{% endblock %}
|
||||
|
||||
|
@ -8,7 +9,7 @@
|
|||
|
||||
{% block content %}
|
||||
<div class="text-muted">
|
||||
{{ sleep.duration }}
|
||||
{{ sleep.duration|duration_string }}
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
{% extends 'cards/tummytime.html' %}
|
||||
{% load timers %}
|
||||
|
||||
{% block header %}Last Tummy Time{% endblock %}
|
||||
|
||||
|
@ -8,7 +9,7 @@
|
|||
|
||||
{% block content %}
|
||||
<div class="text-muted">
|
||||
{{ tummytime.duration }}
|
||||
{{ tummytime.duration|duration_string }}
|
||||
{% if tummytime.milestone %}
|
||||
<br /> {{ tummytime.milestone }}
|
||||
{% endif %}
|
||||
|
|
Loading…
Reference in New Issue