diff --git a/core/forms.py b/core/forms.py index 77b3f617..e6a241e5 100644 --- a/core/forms.py +++ b/core/forms.py @@ -2,6 +2,7 @@ from __future__ import unicode_literals from django import forms +from django.utils import timezone from .models import Child, DiaperChange, Feeding, Sleep, Timer, TummyTime @@ -9,8 +10,31 @@ from .models import Child, DiaperChange, Feeding, Sleep, Timer, TummyTime # Sets the default Child instance if only one exists in the database. def set_default_child(kwargs): instance = kwargs.get('instance', None) + if not kwargs.get('initial'): + kwargs.update(initial={}) if instance is None and Child.objects.count() == 1: - kwargs.update(initial={'child': Child.objects.first()}) + kwargs['initial'].update({'child': Child.objects.first()}) + return kwargs + + +# Uses a timer to set the default start and end date and updates the timer. +def set_default_duration(kwargs): + instance = kwargs.get('instance', None) + timer_id = kwargs.get('timer', None) + if not kwargs.get('initial'): + kwargs.update(initial={}) + if not instance and timer_id: + timer_instance = Timer.objects.get(id=timer_id) + timer_instance.end = timezone.now() + kwargs['initial'].update({ + 'start': timer_instance.start, + 'end': timer_instance.end + }) + timer_instance.save() + try: + kwargs.pop('timer') # This is not part of the model so must be removed. + except KeyError: + pass return kwargs @@ -63,6 +87,7 @@ class FeedingForm(forms.ModelForm): def __init__(self, *args, **kwargs): kwargs = set_default_child(kwargs) + kwargs = set_default_duration(kwargs) super(FeedingForm, self).__init__(*args, **kwargs) @@ -85,6 +110,7 @@ class SleepForm(forms.ModelForm): def __init__(self, *args, **kwargs): kwargs = set_default_child(kwargs) + kwargs = set_default_duration(kwargs) super(SleepForm, self).__init__(*args, **kwargs) @@ -125,4 +151,5 @@ class TummyTimeForm(forms.ModelForm): def __init__(self, *args, **kwargs): kwargs = set_default_child(kwargs) + kwargs = set_default_duration(kwargs) super(TummyTimeForm, self).__init__(*args, **kwargs) diff --git a/core/templates/timer_detail.html b/core/templates/timer_detail.html index 8f842d98..bdfd1ed7 100644 --- a/core/templates/timer_detail.html +++ b/core/templates/timer_detail.html @@ -6,14 +6,17 @@

{{ object.duration }}

Started {{ object.start }} by {{ object.user }}

- - Feeding - - Sleep - - Tummy Time - - Delete Timer -

+ Feeding + Sleep + Tummy Time + Delete Timer
{% endblock %} \ No newline at end of file diff --git a/core/views.py b/core/views.py index 622f5791..6f81c74b 100644 --- a/core/views.py +++ b/core/views.py @@ -79,6 +79,12 @@ class FeedingAdd(PermissionRequiredMixin, CreateView): form_class = FeedingForm success_url = '/feedings' + def get_form_kwargs(self): + kwargs = super(FeedingAdd, self).get_form_kwargs() + # Add timer to be used by FeedingForm.__init__ + kwargs.update({'timer': self.request.GET.get('timer', None)}) + return kwargs + class FeedingUpdate(PermissionRequiredMixin, UpdateView): model = Feeding @@ -129,6 +135,12 @@ class SleepAdd(PermissionRequiredMixin, CreateView): form_class = SleepForm success_url = '/sleep' + def get_form_kwargs(self): + kwargs = super(SleepAdd, self).get_form_kwargs() + # Add timer to be used by SleepForm.__init__ + kwargs.update({'timer': self.request.GET.get('timer', None)}) + return kwargs + class SleepUpdate(PermissionRequiredMixin, UpdateView): model = Sleep @@ -188,6 +200,12 @@ class TummyTimeAdd(PermissionRequiredMixin, CreateView): form_class = TummyTimeForm success_url = '/tummy-time' + def get_form_kwargs(self): + kwargs = super(TummyTimeAdd, self).get_form_kwargs() + # Add timer to be used by TummyTimeForm.__init__ + kwargs.update({'timer': self.request.GET.get('timer', None)}) + return kwargs + class TummyTimeUpdate(PermissionRequiredMixin, UpdateView): model = TummyTime