mirror of https://github.com/snachodog/mybuddy.git
Add ability to create instances from timer detail view.
This commit is contained in:
parent
24d797db01
commit
62a1142ad6
|
@ -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)
|
||||
|
|
|
@ -6,14 +6,17 @@
|
|||
<div class="jumbotron text-center">
|
||||
<h1 class="display-1">{{ object.duration }}</h1>
|
||||
<p class="lead text-muted">Started {{ object.start }} by {{ object.user }}</p>
|
||||
<a class="btn btn-success btn-lg btn-block p-3 mb-3" href="{% url 'feeding-add' %}" role="button">
|
||||
<i class="fa fa-spoon" aria-hidden="true"></i> Feeding</a>
|
||||
<a class="btn btn-success btn-lg btn-block p-3 mb-3" href="{% url 'sleep-add' %}" role="button">
|
||||
<i class="fa fa-bed" aria-hidden="true"></i> Sleep</a>
|
||||
<a class="btn btn-success btn-lg btn-block p-3 mb-3" href="{% url 'tummytime-add' %}" role="button">
|
||||
<i class="fa fa-smile-o" aria-hidden="true"></i> Tummy Time</a>
|
||||
<a class="btn btn-danger btn-lg btn-block p-3 mb-3" href="#" role="button">
|
||||
<i class="fa fa-times-circle" aria-hidden="true"></i> Delete Timer</a>
|
||||
</p>
|
||||
<a class="btn btn-success btn-lg btn-block p-3 mb-3"
|
||||
href="{% url 'feeding-add' %}?timer={{ timer.id }}"
|
||||
role="button"><i class="fa fa-spoon" aria-hidden="true"></i> Feeding</a>
|
||||
<a class="btn btn-success btn-lg btn-block p-3 mb-3"
|
||||
href="{% url 'sleep-add' %}?timer={{ timer.id }}"
|
||||
role="button"><i class="fa fa-bed" aria-hidden="true"></i> Sleep</a>
|
||||
<a class="btn btn-success btn-lg btn-block p-3 mb-3"
|
||||
href="{% url 'tummytime-add' %}?timer={{ timer.id }}"
|
||||
role="button"><i class="fa fa-smile-o" aria-hidden="true"></i> Tummy Time</a>
|
||||
<a class="btn btn-danger btn-lg btn-block p-3 mb-3"
|
||||
href="#"
|
||||
role="button"><i class="fa fa-times-circle" aria-hidden="true"></i> Delete Timer</a>
|
||||
</div>
|
||||
{% endblock %}
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue