Move timer_stop to a model method and add a restart method.

This commit is contained in:
Christopher Charbonneau Wells 2017-09-09 12:47:41 -04:00
parent 4def741d37
commit a9bc52dfff
5 changed files with 52 additions and 29 deletions

View File

@ -5,7 +5,6 @@ from django import forms
from django.utils import timezone
from .models import Child, DiaperChange, Feeding, Sleep, Timer, TummyTime
from .utils import timer_stop
# Sets the default Child instance if only one exists in the database.
@ -94,7 +93,8 @@ class FeedingForm(forms.ModelForm):
def save(self, commit=True):
instance = super(FeedingForm, self).save(commit=False)
if self.timer_id:
timer_stop(self.timer_id, instance.end)
timer = Timer.objects.get(id=self.timer_id)
timer.stop(instance.end)
instance.save()
return instance
@ -125,7 +125,8 @@ class SleepForm(forms.ModelForm):
def save(self, commit=True):
instance = super(SleepForm, self).save(commit=False)
if self.timer_id:
timer_stop(self.timer_id, instance.end)
timer = Timer.objects.get(id=self.timer_id)
timer.stop(instance.end)
instance.save()
return instance
@ -174,6 +175,7 @@ class TummyTimeForm(forms.ModelForm):
def save(self, commit=True):
instance = super(TummyTimeForm, self).save(commit=False)
if self.timer_id:
timer_stop(self.timer_id, instance.end)
timer = Timer.objects.get(id=self.timer_id)
timer.stop(instance.end)
instance.save()
return instance

View File

@ -171,6 +171,21 @@ class Timer(models.Model):
else:
return timezone.now() - self.start
def restart(self):
"""Restart the timer."""
self.start = timezone.now()
self.end = None
self.duration = None
self.active = True
self.save()
def stop(self, end=None):
"""Stop the timer."""
if not self.end:
self.end = timezone.now()
self.end = end
self.save()
def save(self, *args, **kwargs):
self.active = self.end is None
self.name = self.name or None

View File

@ -22,34 +22,38 @@
</p>
{% if perms.core.add_feeding %}
<a class="btn btn-success btn-lg btn-block p-3 mb-3"
<a class="btn btn-success btn-lg btn-block mb-3"
href="{% url 'feeding-add' %}?timer={{ timer.id }}"
role="button"><i class="fa fa-spoon" aria-hidden="true"></i> Feeding</a>
{% endif %}
{% if perms.core.add_sleep %}
<a class="btn btn-success btn-lg btn-block p-3 mb-3"
<a class="btn btn-success btn-lg btn-block mb-3"
href="{% url 'sleep-add' %}?timer={{ timer.id }}"
role="button"><i class="fa fa-bed" aria-hidden="true"></i> Sleep</a>
{% endif %}
{% if perms.core.add_tummytime %}
<a class="btn btn-success btn-lg btn-block p-3 mb-3"
<a class="btn btn-success btn-lg btn-block mb-3"
href="{% url 'tummytime-add' %}?timer={{ timer.id }}"
role="button"><i class="fa fa-smile-o" aria-hidden="true"></i> Tummy Time</a>
{% endif %}
{% if perms.core.delete_timer %}
<a class="btn btn-danger btn-lg btn-block p-3 mb-3"
href="{% url 'timer-delete' timer.id %}"
role="button"><i class="fa fa-trash" aria-hidden="true"></i> Delete Timer</a>
{% endif %}
<div class="btn-group btn-group-lg center-block" role="group" aria-label="Timer actions">
{% if object.active and perms.core.change_timer %}
<a class="btn btn-warning btn-lg btn-block p-3 mb-3"
href="{% url 'timer-stop' timer.id %}"
role="button"><i class="fa fa-stop" aria-hidden="true"></i> Stop Timer</a>
{% endif %}
{% if perms.core.delete_timer %}
<a class="btn btn-danger"
href="{% url 'timer-delete' timer.id %}"
role="button"><i class="fa fa-trash" aria-hidden="true"></i></a>
{% endif %}
{% if object.active and perms.core.change_timer %}
<a class="btn btn-warning"
href="{% url 'timer-stop' timer.id %}"
role="button"><i class="fa fa-stop" aria-hidden="true"></i></a>
{% endif %}
</div>
</div>
{% endblock %}

View File

@ -15,16 +15,6 @@ def filter_by_params(request, model, available_params):
return queryset
def timer_stop(timer_id, end=None):
"""Stop a timer instance by setting the end field value."""
if not end:
end = timezone.now()
from .models import Timer
timer_instance = Timer.objects.get(id=timer_id)
timer_instance.end = end
timer_instance.save()
def duration_string(duration):
"""Format hours, minutes and seconds in a human-friendly way (e.g. "2
hours, 25 minutes, 31 seconds")"""

View File

@ -12,7 +12,6 @@ from django.views.generic.list import ListView
from .models import Child, DiaperChange, Feeding, Note, Sleep, Timer, TummyTime
from .forms import (ChildForm, DiaperChangeForm, FeedingForm, SleepForm,
TimerForm, TummyTimeForm)
from .utils import timer_stop
class ChildList(PermissionRequiredMixin, ListView):
@ -191,11 +190,24 @@ class TimerAddQuick(PermissionRequiredMixin, RedirectView):
return super(TimerAddQuick, self).get(request, *args, **kwargs)
class TimerRestart(PermissionRequiredMixin, RedirectView):
permission_required = ('core.change_timer',)
def get(self, request, *args, **kwargs):
instance = Timer.objects.get(id=kwargs['pk'])
instance.restart()
return super(TimerRestart, self).get(request, *args, **kwargs)
def get_redirect_url(self, *args, **kwargs):
return '/timer/{}'.format(kwargs['pk'])
class TimerStop(PermissionRequiredMixin, RedirectView):
permission_required = ('core.change_timer',)
def get(self, request, *args, **kwargs):
timer_stop(kwargs['pk'])
instance = Timer.objects.get(id=kwargs['pk'])
instance.stop()
return super(TimerStop, self).get(request, *args, **kwargs)
def get_redirect_url(self, *args, **kwargs):