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:
|
|
|
|
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
|
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',
|
|
|
|
'data-toggle': 'datetimepicker',
|
|
|
|
'data-target': '#id_birth_date',
|
|
|
|
}),
|
|
|
|
}
|
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',
|
|
|
|
'data-toggle': 'datetimepicker',
|
|
|
|
'data-target': '#id_time',
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
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',
|
|
|
|
'data-toggle': 'datetimepicker',
|
|
|
|
'data-target': '#id_start',
|
|
|
|
}),
|
|
|
|
'end': forms.DateTimeInput(attrs={
|
|
|
|
'class': 'datepicker-input',
|
|
|
|
'data-toggle': 'datetimepicker',
|
|
|
|
'data-target': '#id_end',
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
2017-08-18 00:59:55 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
kwargs = set_default_child(kwargs)
|
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-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',
|
|
|
|
'data-toggle': 'datetimepicker',
|
|
|
|
'data-target': '#id_start',
|
|
|
|
}),
|
|
|
|
'end': forms.DateTimeInput(attrs={
|
|
|
|
'class': 'datepicker-input',
|
|
|
|
'data-toggle': 'datetimepicker',
|
|
|
|
'data-target': '#id_end',
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
2017-08-18 00:59:55 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
kwargs = set_default_child(kwargs)
|
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-16 18:57:46 +00:00
|
|
|
|
2017-08-17 01:57:01 +00:00
|
|
|
class TimerForm(forms.ModelForm):
|
|
|
|
next = forms.CharField(required=False)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Timer
|
|
|
|
fields = ['name']
|
|
|
|
|
2017-08-18 00:42:47 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.user = kwargs.pop('user')
|
|
|
|
super(TimerForm, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
instance = super(TimerForm, self).save(commit=False)
|
|
|
|
instance.user = self.user
|
|
|
|
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',
|
|
|
|
'data-toggle': 'datetimepicker',
|
|
|
|
'data-target': '#id_start',
|
|
|
|
}),
|
|
|
|
'end': forms.DateTimeInput(attrs={
|
|
|
|
'class': 'datepicker-input',
|
|
|
|
'data-toggle': 'datetimepicker',
|
|
|
|
'data-target': '#id_end',
|
|
|
|
}),
|
|
|
|
}
|
2017-08-18 00:59:55 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
kwargs = set_default_child(kwargs)
|
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)
|