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-11-10 00:50:54 +00:00
|
|
|
from core import models
|
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-11-10 00:50:54 +00:00
|
|
|
if instance is None and models.Child.objects.count() == 1:
|
|
|
|
kwargs['initial'].update({'child': models.Child.objects.first()})
|
2017-08-18 04:42:37 +00:00
|
|
|
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-11-10 00:50:54 +00:00
|
|
|
instance = models.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:
|
2017-11-10 00:50:54 +00:00
|
|
|
model = models.Child
|
2017-08-16 18:17:27 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
2017-11-01 00:49:10 +00:00
|
|
|
class ChildDeleteForm(forms.ModelForm):
|
|
|
|
confirm_name = forms.CharField(max_length=511)
|
|
|
|
|
|
|
|
class Meta:
|
2017-11-10 00:50:54 +00:00
|
|
|
model = models.Child
|
2017-11-01 00:49:10 +00:00
|
|
|
fields = []
|
|
|
|
|
|
|
|
def clean_confirm_name(self):
|
|
|
|
confirm_name = self.cleaned_data['confirm_name']
|
|
|
|
if confirm_name != str(self.instance):
|
2017-11-01 20:14:42 +00:00
|
|
|
raise forms.ValidationError(
|
|
|
|
'Name does not match child name.', code='confirm_mismatch')
|
2017-11-01 00:49:10 +00:00
|
|
|
return confirm_name
|
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
instance = self.instance
|
|
|
|
self.instance.delete()
|
|
|
|
return instance
|
|
|
|
|
|
|
|
|
2017-08-16 18:57:46 +00:00
|
|
|
class DiaperChangeForm(forms.ModelForm):
|
|
|
|
class Meta:
|
2017-11-10 00:50:54 +00:00
|
|
|
model = models.DiaperChange
|
2017-08-16 18:57:46 +00:00
|
|
|
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:
|
2017-11-10 00:50:54 +00:00
|
|
|
model = models.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-11-10 00:50:54 +00:00
|
|
|
timer = models.Timer.objects.get(id=self.timer_id)
|
2017-09-09 16:47:41 +00:00
|
|
|
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-11-10 00:50:54 +00:00
|
|
|
class NoteForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = models.Note
|
|
|
|
fields = ['child', 'note']
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
kwargs = set_default_child(kwargs)
|
|
|
|
super(NoteForm, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
|
2017-08-16 18:57:46 +00:00
|
|
|
class SleepForm(forms.ModelForm):
|
|
|
|
class Meta:
|
2017-11-10 00:50:54 +00:00
|
|
|
model = models.Sleep
|
2017-08-16 18:57:46 +00:00
|
|
|
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-11-10 00:50:54 +00:00
|
|
|
timer = models.Timer.objects.get(id=self.timer_id)
|
2017-09-09 16:47:41 +00:00
|
|
|
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:
|
2017-11-10 00:50:54 +00:00
|
|
|
model = models.Timer
|
2017-10-28 17:27:33 +00:00
|
|
|
fields = ['name', 'start']
|
|
|
|
widgets = {
|
|
|
|
'start': forms.DateTimeInput(attrs={
|
|
|
|
'class': 'datepicker-input',
|
|
|
|
'data-target': '#datetimepicker_start',
|
|
|
|
})
|
|
|
|
}
|
2017-08-17 01:57:01 +00:00
|
|
|
|
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:
|
2017-11-10 00:50:54 +00:00
|
|
|
model = models.TummyTime
|
2017-08-16 18:57:46 +00:00
|
|
|
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-11-10 00:50:54 +00:00
|
|
|
timer = models.Timer.objects.get(id=self.timer_id)
|
2017-09-09 16:47:41 +00:00
|
|
|
timer.stop(instance.end)
|
2017-08-18 05:53:48 +00:00
|
|
|
instance.save()
|
|
|
|
return instance
|
2017-11-10 02:15:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WeightForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = models.Weight
|
|
|
|
fields = ['child', 'weight', 'date']
|
|
|
|
widgets = {
|
|
|
|
'date': forms.DateInput(attrs={
|
|
|
|
'class': 'datepicker-input',
|
|
|
|
'data-target': '#datetimepicker_date',
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
kwargs = set_default_child(kwargs)
|
|
|
|
super(WeightForm, self).__init__(*args, **kwargs)
|