From a9bc52dfff0438e850bc0f91542f5c84818374b0 Mon Sep 17 00:00:00 2001
From: Christopher Charbonneau Wells
Date: Sat, 9 Sep 2017 12:47:41 -0400
Subject: [PATCH] Move timer_stop to a model method and add a restart method.
---
core/forms.py | 10 +++++----
core/models.py | 15 ++++++++++++++
core/templates/core/timer_detail.html | 30 +++++++++++++++------------
core/utils.py | 10 ---------
core/views.py | 16 ++++++++++++--
5 files changed, 52 insertions(+), 29 deletions(-)
diff --git a/core/forms.py b/core/forms.py
index 6bfdf259..55774c21 100644
--- a/core/forms.py
+++ b/core/forms.py
@@ -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
diff --git a/core/models.py b/core/models.py
index 6235341b..575b804e 100644
--- a/core/models.py
+++ b/core/models.py
@@ -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
diff --git a/core/templates/core/timer_detail.html b/core/templates/core/timer_detail.html
index f37f54d2..9c861e2e 100644
--- a/core/templates/core/timer_detail.html
+++ b/core/templates/core/timer_detail.html
@@ -22,34 +22,38 @@
{% if perms.core.add_feeding %}
- Feeding
{% endif %}
{% if perms.core.add_sleep %}
- Sleep
{% endif %}
{% if perms.core.add_tummytime %}
- Tummy Time
{% endif %}
- {% if perms.core.delete_timer %}
- Delete Timer
- {% endif %}
+
- {% if object.active and perms.core.change_timer %}
-
Stop Timer
- {% endif %}
+ {% if perms.core.delete_timer %}
+
+ {% endif %}
+
+ {% if object.active and perms.core.change_timer %}
+
+ {% endif %}
+
+
{% endblock %}
diff --git a/core/utils.py b/core/utils.py
index 9d205dc1..192d2ec8 100644
--- a/core/utils.py
+++ b/core/utils.py
@@ -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")"""
diff --git a/core/views.py b/core/views.py
index 94600c9c..9100f122 100644
--- a/core/views.py
+++ b/core/views.py
@@ -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):