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 %} +