mirror of https://github.com/snachodog/mybuddy.git
Stop timer on instance save instead of form load.
This commit is contained in:
parent
9ac8432ff8
commit
f08ec94e3c
|
@ -5,6 +5,7 @@ from django import forms
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
from .models import Child, DiaperChange, Feeding, Sleep, Timer, TummyTime
|
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.
|
# Sets the default Child instance if only one exists in the database.
|
||||||
|
@ -25,14 +26,13 @@ def set_default_duration(kwargs):
|
||||||
kwargs.update(initial={})
|
kwargs.update(initial={})
|
||||||
if not instance and timer_id:
|
if not instance and timer_id:
|
||||||
timer_instance = Timer.objects.get(id=timer_id)
|
timer_instance = Timer.objects.get(id=timer_id)
|
||||||
timer_instance.end = timezone.now()
|
|
||||||
kwargs['initial'].update({
|
kwargs['initial'].update({
|
||||||
|
'timer': timer_instance,
|
||||||
'start': timer_instance.start,
|
'start': timer_instance.start,
|
||||||
'end': timer_instance.end
|
'end': timezone.now()
|
||||||
})
|
})
|
||||||
timer_instance.save()
|
|
||||||
try:
|
try:
|
||||||
kwargs.pop('timer') # This is not part of the model so must be removed.
|
kwargs.pop('timer')
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
return kwargs
|
return kwargs
|
||||||
|
@ -87,9 +87,17 @@ class FeedingForm(forms.ModelForm):
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
kwargs = set_default_child(kwargs)
|
kwargs = set_default_child(kwargs)
|
||||||
|
self.timer_id = kwargs.get('timer', None)
|
||||||
kwargs = set_default_duration(kwargs)
|
kwargs = set_default_duration(kwargs)
|
||||||
super(FeedingForm, self).__init__(*args, **kwargs)
|
super(FeedingForm, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
def save(self, commit=True):
|
||||||
|
instance = super(FeedingForm, self).save(commit=False)
|
||||||
|
if self.timer_id:
|
||||||
|
timer_stop(self.timer_id, instance.end)
|
||||||
|
instance.save()
|
||||||
|
return instance
|
||||||
|
|
||||||
|
|
||||||
class SleepForm(forms.ModelForm):
|
class SleepForm(forms.ModelForm):
|
||||||
class Meta:
|
class Meta:
|
||||||
|
@ -110,9 +118,17 @@ class SleepForm(forms.ModelForm):
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
kwargs = set_default_child(kwargs)
|
kwargs = set_default_child(kwargs)
|
||||||
|
self.timer_id = kwargs.get('timer', None)
|
||||||
kwargs = set_default_duration(kwargs)
|
kwargs = set_default_duration(kwargs)
|
||||||
super(SleepForm, self).__init__(*args, **kwargs)
|
super(SleepForm, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
def save(self, commit=True):
|
||||||
|
instance = super(SleepForm, self).save(commit=False)
|
||||||
|
if self.timer_id:
|
||||||
|
timer_stop(self.timer_id, instance.end)
|
||||||
|
instance.save()
|
||||||
|
return instance
|
||||||
|
|
||||||
|
|
||||||
class TimerForm(forms.ModelForm):
|
class TimerForm(forms.ModelForm):
|
||||||
next = forms.CharField(required=False)
|
next = forms.CharField(required=False)
|
||||||
|
@ -151,5 +167,13 @@ class TummyTimeForm(forms.ModelForm):
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
kwargs = set_default_child(kwargs)
|
kwargs = set_default_child(kwargs)
|
||||||
|
self.timer_id = kwargs.get('timer', None)
|
||||||
kwargs = set_default_duration(kwargs)
|
kwargs = set_default_duration(kwargs)
|
||||||
super(TummyTimeForm, self).__init__(*args, **kwargs)
|
super(TummyTimeForm, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
def save(self, commit=True):
|
||||||
|
instance = super(TummyTimeForm, self).save(commit=False)
|
||||||
|
if self.timer_id:
|
||||||
|
timer_stop(self.timer_id, instance.end)
|
||||||
|
instance.save()
|
||||||
|
return instance
|
||||||
|
|
|
@ -3,6 +3,8 @@ from __future__ import unicode_literals
|
||||||
|
|
||||||
from math import floor
|
from math import floor
|
||||||
|
|
||||||
|
from django.utils import timezone
|
||||||
|
|
||||||
|
|
||||||
def duration_string(start, end, short=False):
|
def duration_string(start, end, short=False):
|
||||||
diff = end - start
|
diff = end - start
|
||||||
|
@ -35,3 +37,13 @@ def filter_by_params(request, model, available_params):
|
||||||
queryset = queryset.filter(**{param: value})
|
queryset = queryset.filter(**{param: value})
|
||||||
|
|
||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
|
|
||||||
|
# Stop a timer instance by setting it's end field.
|
||||||
|
def timer_stop(timer_id, end=None):
|
||||||
|
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()
|
||||||
|
|
Loading…
Reference in New Issue