2017-08-16 18:17:27 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
from django import forms
|
2017-08-18 04:42:37 +00:00
|
|
|
from django.utils import timezone
|
2017-08-16 18:17:27 +00:00
|
|
|
|
2017-08-17 01:57:01 +00:00
|
|
|
from .models import Child, DiaperChange, Feeding, Sleep, Timer, TummyTime
|
2017-08-16 18:17:27 +00:00
|
|
|
|
|
|
|
|
2017-08-18 00:59:55 +00:00
|
|
|
# Sets the default Child instance if only one exists in the database.
|
|
|
|
def set_default_child(kwargs):
|
|
|
|
instance = kwargs.get('instance', None)
|
2017-08-18 04:42:37 +00:00
|
|
|
if not kwargs.get('initial'):
|
|
|
|
kwargs.update(initial={})
|
2017-08-18 00:59:55 +00:00
|
|
|
if instance is None and Child.objects.count() == 1:
|
2017-08-18 04:42:37 +00:00
|
|
|
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:
|
2017-09-08 18:36:22 +00:00
|
|
|
instance = Timer.objects.get(id=timer_id)
|
2017-08-18 04:42:37 +00:00
|
|
|
kwargs['initial'].update({
|
2017-09-08 18:36:22 +00:00
|
|
|
'timer': instance,
|
|
|
|
'start': instance.start,
|
|
|
|
'end': instance.end or timezone.now()
|
2017-08-18 04:42:37 +00:00
|
|
|
})
|
|
|
|
try:
|
2017-08-18 05:53:48 +00:00
|
|
|
kwargs.pop('timer')
|
2017-08-18 04:42:37 +00:00
|
|
|
except KeyError:
|
|
|
|
pass
|
2017-08-18 00:59:55 +00:00
|
|
|
return kwargs
|
|
|
|
|
|
|
|
|
2017-08-16 18:57:46 +00:00
|
|
|
class ChildForm(forms.ModelForm):
|
2017-08-16 18:17:27 +00:00
|
|
|
class Meta:
|
|
|
|
model = Child
|
|
|
|
fields = ['first_name', 'last_name', 'birth_date']
|
|
|
|
widgets = {
|
|
|
|
'birth_date': forms.DateInput(attrs={
|
|
|
|
'class': 'datepicker-input',
|
2017-09-17 19:48:23 +00:00
|
|
|
'data-target': '#datetimepicker_date',
|
2017-08-16 18:17:27 +00:00
|
|
|
}),
|
|
|
|
}
|
2017-08-16 18:57:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DiaperChangeForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = DiaperChange
|
|
|
|
fields = ['child', 'time', 'wet', 'solid', 'color']
|
|
|
|
widgets = {
|
|
|
|
'time': forms.DateTimeInput(attrs={
|
|
|
|
'class': 'datepicker-input',
|
2017-09-17 19:48:23 +00:00
|
|
|
'data-target': '#datetimepicker_time',
|
2017-08-16 18:57:46 +00:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
2017-08-18 00:59:55 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
kwargs = set_default_child(kwargs)
|
|
|
|
super(DiaperChangeForm, self).__init__(*args, **kwargs)
|
|
|
|
|
2017-08-16 18:57:46 +00:00
|
|
|
|
|
|
|
class FeedingForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Feeding
|
2017-08-16 22:07:25 +00:00
|
|
|
fields = ['child', 'start', 'end', 'type', 'method', 'amount']
|
2017-08-16 18:57:46 +00:00
|
|
|
widgets = {
|
|
|
|
'start': forms.DateTimeInput(attrs={
|
|
|
|
'class': 'datepicker-input',
|
2017-09-17 19:48:23 +00:00
|
|
|
'data-target': '#datetimepicker_start',
|
2017-08-16 18:57:46 +00:00
|
|
|
}),
|
|
|
|
'end': forms.DateTimeInput(attrs={
|
|
|
|
'class': 'datepicker-input',
|
2017-09-17 19:48:23 +00:00
|
|
|
'data-target': '#datetimepicker_end',
|
2017-08-16 18:57:46 +00:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
2017-08-18 00:59:55 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
kwargs = set_default_child(kwargs)
|
2017-08-18 05:53:48 +00:00
|
|
|
self.timer_id = kwargs.get('timer', None)
|
2017-08-18 04:42:37 +00:00
|
|
|
kwargs = set_default_duration(kwargs)
|
2017-08-18 00:59:55 +00:00
|
|
|
super(FeedingForm, self).__init__(*args, **kwargs)
|
|
|
|
|
2017-08-18 05:53:48 +00:00
|
|
|
def save(self, commit=True):
|
|
|
|
instance = super(FeedingForm, self).save(commit=False)
|
|
|
|
if self.timer_id:
|
2017-09-09 16:47:41 +00:00
|
|
|
timer = Timer.objects.get(id=self.timer_id)
|
|
|
|
timer.stop(instance.end)
|
2017-08-18 05:53:48 +00:00
|
|
|
instance.save()
|
|
|
|
return instance
|
|
|
|
|
2017-08-16 18:57:46 +00:00
|
|
|
|
|
|
|
class SleepForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Sleep
|
|
|
|
fields = ['child', 'start', 'end']
|
|
|
|
widgets = {
|
|
|
|
'start': forms.DateTimeInput(attrs={
|
|
|
|
'class': 'datepicker-input',
|
2017-09-17 19:48:23 +00:00
|
|
|
'data-target': '#datetimepicker_start',
|
2017-08-16 18:57:46 +00:00
|
|
|
}),
|
|
|
|
'end': forms.DateTimeInput(attrs={
|
|
|
|
'class': 'datepicker-input',
|
2017-09-17 19:48:23 +00:00
|
|
|
'data-target': '#datetimepicker_end',
|
2017-08-16 18:57:46 +00:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
2017-08-18 00:59:55 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
kwargs = set_default_child(kwargs)
|
2017-08-18 05:53:48 +00:00
|
|
|
self.timer_id = kwargs.get('timer', None)
|
2017-08-18 04:42:37 +00:00
|
|
|
kwargs = set_default_duration(kwargs)
|
2017-08-18 00:59:55 +00:00
|
|
|
super(SleepForm, self).__init__(*args, **kwargs)
|
|
|
|
|
2017-08-18 05:53:48 +00:00
|
|
|
def save(self, commit=True):
|
|
|
|
instance = super(SleepForm, self).save(commit=False)
|
|
|
|
if self.timer_id:
|
2017-09-09 16:47:41 +00:00
|
|
|
timer = Timer.objects.get(id=self.timer_id)
|
|
|
|
timer.stop(instance.end)
|
2017-08-18 05:53:48 +00:00
|
|
|
instance.save()
|
|
|
|
return instance
|
|
|
|
|
2017-08-16 18:57:46 +00:00
|
|
|
|
2017-08-17 01:57:01 +00:00
|
|
|
class TimerForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Timer
|
|
|
|
fields = ['name']
|
|
|
|
|
2017-08-18 00:42:47 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
2017-09-10 13:50:16 +00:00
|
|
|
self.user = kwargs.pop('user')
|
2017-08-18 00:42:47 +00:00
|
|
|
super(TimerForm, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
instance = super(TimerForm, self).save(commit=False)
|
2017-09-10 13:50:16 +00:00
|
|
|
instance.user = self.user
|
2017-08-18 00:42:47 +00:00
|
|
|
instance.save()
|
|
|
|
return instance
|
|
|
|
|
2017-08-17 01:57:01 +00:00
|
|
|
|
2017-08-16 18:57:46 +00:00
|
|
|
class TummyTimeForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = TummyTime
|
|
|
|
fields = ['child', 'start', 'end', 'milestone']
|
|
|
|
widgets = {
|
|
|
|
'start': forms.DateTimeInput(attrs={
|
|
|
|
'class': 'datepicker-input',
|
2017-09-17 19:48:23 +00:00
|
|
|
'data-target': '#datetimepicker_start',
|
2017-08-16 18:57:46 +00:00
|
|
|
}),
|
|
|
|
'end': forms.DateTimeInput(attrs={
|
|
|
|
'class': 'datepicker-input',
|
2017-09-17 19:48:23 +00:00
|
|
|
'data-target': '#datetimepicker_end',
|
2017-08-16 18:57:46 +00:00
|
|
|
}),
|
|
|
|
}
|
2017-08-18 00:59:55 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
kwargs = set_default_child(kwargs)
|
2017-08-18 05:53:48 +00:00
|
|
|
self.timer_id = kwargs.get('timer', None)
|
2017-08-18 04:42:37 +00:00
|
|
|
kwargs = set_default_duration(kwargs)
|
2017-08-18 00:59:55 +00:00
|
|
|
super(TummyTimeForm, self).__init__(*args, **kwargs)
|
2017-08-18 05:53:48 +00:00
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
instance = super(TummyTimeForm, self).save(commit=False)
|
|
|
|
if self.timer_id:
|
2017-09-09 16:47:41 +00:00
|
|
|
timer = Timer.objects.get(id=self.timer_id)
|
|
|
|
timer.stop(instance.end)
|
2017-08-18 05:53:48 +00:00
|
|
|
instance.save()
|
|
|
|
return instance
|