2017-08-16 18:17:27 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from django import forms
|
2017-11-19 01:57:29 +00:00
|
|
|
from django.conf import settings
|
2019-04-14 05:51:44 +00:00
|
|
|
from django.utils import timezone
|
|
|
|
from django.utils.translation import gettext as _
|
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
|
|
|
|
|
|
|
|
2020-01-29 18:50:23 +00:00
|
|
|
def set_initial_values(kwargs, form_type):
|
2020-01-29 17:45:26 +00:00
|
|
|
"""
|
2020-01-29 18:50:23 +00:00
|
|
|
Sets initial value for add forms based on provided kwargs.
|
2020-01-29 17:45:26 +00:00
|
|
|
|
2020-01-29 18:50:23 +00:00
|
|
|
:param kwargs: Keyword arguments.
|
|
|
|
:param form_type: Class of the type of form being initialized.
|
|
|
|
:return: Keyword arguments with updated "initial" values.
|
2020-01-29 17:45:26 +00:00
|
|
|
"""
|
|
|
|
|
2020-01-29 18:50:23 +00:00
|
|
|
# Never update initial values for existing instance (e.g. edit operation).
|
|
|
|
if kwargs.get('instance', None):
|
|
|
|
return kwargs
|
2020-01-29 17:45:26 +00:00
|
|
|
|
2020-01-29 18:50:23 +00:00
|
|
|
# Add the "initial" kwarg if it does not already exist.
|
2017-08-18 04:42:37 +00:00
|
|
|
if not kwargs.get('initial'):
|
|
|
|
kwargs.update(initial={})
|
2020-01-29 17:45:26 +00:00
|
|
|
|
2020-01-29 18:50:23 +00:00
|
|
|
# Set Child based on `child` kwarg or single Chile database.
|
|
|
|
child_slug = kwargs.get('child', None)
|
|
|
|
if child_slug:
|
|
|
|
kwargs['initial'].update({
|
|
|
|
'child': models.Child.objects.filter(slug=child_slug).first(),
|
|
|
|
})
|
|
|
|
elif models.Child.count() == 1:
|
|
|
|
kwargs['initial'].update({'child': models.Child.objects.first()})
|
|
|
|
|
|
|
|
# Set start and end time based on Timer from `timer` kwarg.
|
|
|
|
timer_id = kwargs.get('timer', None)
|
|
|
|
if timer_id:
|
2020-01-29 06:51:16 +00:00
|
|
|
timer = models.Timer.objects.get(id=timer_id)
|
2017-08-18 04:42:37 +00:00
|
|
|
kwargs['initial'].update({
|
2020-01-29 06:51:16 +00:00
|
|
|
'timer': timer,
|
|
|
|
'start': timer.start,
|
|
|
|
'end': timer.end or timezone.now()
|
2017-08-18 04:42:37 +00:00
|
|
|
})
|
2020-01-29 17:45:26 +00:00
|
|
|
|
2020-01-29 18:50:23 +00:00
|
|
|
# Set initial type value for Feeding instance based on last type used.
|
|
|
|
if form_type == FeedingForm and 'child' in kwargs['initial']:
|
|
|
|
last_feeding = models.Feeding.objects.filter(
|
2020-02-08 22:02:47 +00:00
|
|
|
child=kwargs['initial']['child']).order_by('end').last()
|
2020-01-29 18:50:23 +00:00
|
|
|
if last_feeding:
|
|
|
|
kwargs['initial'].update({'type': last_feeding.type})
|
2017-08-18 00:59:55 +00:00
|
|
|
|
2020-01-29 18:50:23 +00:00
|
|
|
# Remove custom kwargs so they do not interfere with `super` calls.
|
|
|
|
for key in ['child', 'timer']:
|
|
|
|
try:
|
|
|
|
kwargs.pop(key)
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2017-08-18 00:59:55 +00:00
|
|
|
|
2018-03-14 02:49:21 +00:00
|
|
|
return kwargs
|
|
|
|
|
|
|
|
|
2020-01-29 18:50:23 +00:00
|
|
|
class CoreModelForm(forms.ModelForm):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
# Set `timer_id` so the Timer can be stopped in the `save` method.
|
|
|
|
self.timer_id = kwargs.get('timer', None)
|
|
|
|
kwargs = set_initial_values(kwargs, type(self))
|
|
|
|
super(CoreModelForm, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
# If `timer_id` is present, stop the Timer.
|
|
|
|
instance = super(CoreModelForm, self).save(commit=False)
|
|
|
|
if self.timer_id:
|
|
|
|
timer = models.Timer.objects.get(id=self.timer_id)
|
|
|
|
timer.stop(instance.end)
|
|
|
|
if commit:
|
|
|
|
instance.save()
|
|
|
|
return instance
|
|
|
|
|
|
|
|
|
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-11-19 01:57:29 +00:00
|
|
|
fields = [
|
|
|
|
'first_name',
|
|
|
|
'last_name',
|
|
|
|
'birth_date'
|
|
|
|
]
|
2017-12-01 23:07:08 +00:00
|
|
|
if settings.BABY_BUDDY['ALLOW_UPLOADS']:
|
2017-11-19 01:57:29 +00:00
|
|
|
fields.append('picture')
|
2017-08-16 18:17:27 +00:00
|
|
|
widgets = {
|
|
|
|
'birth_date': forms.DateInput(attrs={
|
2020-01-28 04:07:42 +00:00
|
|
|
'readonly': 'readonly',
|
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(
|
2019-04-14 05:51:44 +00:00
|
|
|
_('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
|
|
|
|
|
|
|
|
|
2020-01-29 18:50:23 +00:00
|
|
|
class DiaperChangeForm(CoreModelForm):
|
2017-08-16 18:57:46 +00:00
|
|
|
class Meta:
|
2017-11-10 00:50:54 +00:00
|
|
|
model = models.DiaperChange
|
2020-02-15 04:10:08 +00:00
|
|
|
fields = ['child', 'time', 'wet', 'solid', 'color', 'amount', 'notes']
|
2017-08-16 18:57:46 +00:00
|
|
|
widgets = {
|
|
|
|
'time': forms.DateTimeInput(attrs={
|
2020-01-28 04:07:42 +00:00
|
|
|
'readonly': 'readonly',
|
2017-09-17 19:48:23 +00:00
|
|
|
'data-target': '#datetimepicker_time',
|
2017-08-16 18:57:46 +00:00
|
|
|
}),
|
2020-02-16 22:03:50 +00:00
|
|
|
'notes': forms.Textarea(attrs={'rows': 5}),
|
2017-08-16 18:57:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-29 18:50:23 +00:00
|
|
|
class FeedingForm(CoreModelForm):
|
2017-08-16 18:57:46 +00:00
|
|
|
class Meta:
|
2017-11-10 00:50:54 +00:00
|
|
|
model = models.Feeding
|
2020-02-15 04:10:08 +00:00
|
|
|
fields = ['child', 'start', 'end', 'type', 'method', 'amount', 'notes']
|
2017-08-16 18:57:46 +00:00
|
|
|
widgets = {
|
|
|
|
'start': forms.DateTimeInput(attrs={
|
2020-01-28 04:07:42 +00:00
|
|
|
'readonly': 'readonly',
|
2017-09-17 19:48:23 +00:00
|
|
|
'data-target': '#datetimepicker_start',
|
2017-08-16 18:57:46 +00:00
|
|
|
}),
|
|
|
|
'end': forms.DateTimeInput(attrs={
|
2020-01-28 04:07:42 +00:00
|
|
|
'readonly': 'readonly',
|
2017-09-17 19:48:23 +00:00
|
|
|
'data-target': '#datetimepicker_end',
|
2017-08-16 18:57:46 +00:00
|
|
|
}),
|
2020-02-16 22:03:50 +00:00
|
|
|
'notes': forms.Textarea(attrs={'rows': 5}),
|
2017-08-16 18:57:46 +00:00
|
|
|
}
|
|
|
|
|
2017-08-18 00:59:55 +00:00
|
|
|
|
2020-01-29 18:50:23 +00:00
|
|
|
class NoteForm(CoreModelForm):
|
2017-11-10 00:50:54 +00:00
|
|
|
class Meta:
|
|
|
|
model = models.Note
|
|
|
|
fields = ['child', 'note']
|
|
|
|
|
|
|
|
|
2020-01-29 18:50:23 +00:00
|
|
|
class SleepForm(CoreModelForm):
|
2017-08-16 18:57:46 +00:00
|
|
|
class Meta:
|
2017-11-10 00:50:54 +00:00
|
|
|
model = models.Sleep
|
2020-02-15 04:10:08 +00:00
|
|
|
fields = ['child', 'start', 'end', 'notes']
|
2017-08-16 18:57:46 +00:00
|
|
|
widgets = {
|
|
|
|
'start': forms.DateTimeInput(attrs={
|
2020-01-28 04:07:42 +00:00
|
|
|
'readonly': 'readonly',
|
2017-09-17 19:48:23 +00:00
|
|
|
'data-target': '#datetimepicker_start',
|
2017-08-16 18:57:46 +00:00
|
|
|
}),
|
|
|
|
'end': forms.DateTimeInput(attrs={
|
2020-01-28 04:07:42 +00:00
|
|
|
'readonly': 'readonly',
|
2017-09-17 19:48:23 +00:00
|
|
|
'data-target': '#datetimepicker_end',
|
2017-08-16 18:57:46 +00:00
|
|
|
}),
|
2020-02-16 22:03:50 +00:00
|
|
|
'notes': forms.Textarea(attrs={'rows': 5}),
|
2017-08-16 18:57:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-29 18:50:23 +00:00
|
|
|
class TemperatureForm(CoreModelForm):
|
2019-05-17 04:33:26 +00:00
|
|
|
class Meta:
|
|
|
|
model = models.Temperature
|
2020-02-15 04:10:08 +00:00
|
|
|
fields = ['child', 'temperature', 'time', 'notes']
|
2019-05-17 04:33:26 +00:00
|
|
|
widgets = {
|
|
|
|
'time': forms.DateTimeInput(attrs={
|
2020-01-28 04:07:42 +00:00
|
|
|
'readonly': 'readonly',
|
2019-05-17 04:33:26 +00:00
|
|
|
'data-target': '#datetimepicker_time',
|
|
|
|
}),
|
2020-02-16 22:03:50 +00:00
|
|
|
'notes': forms.Textarea(attrs={'rows': 5}),
|
2019-05-17 04:33:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-29 18:50:23 +00:00
|
|
|
class TimerForm(CoreModelForm):
|
2017-08-17 01:57:01 +00:00
|
|
|
class Meta:
|
2017-11-10 00:50:54 +00:00
|
|
|
model = models.Timer
|
2020-01-28 21:56:28 +00:00
|
|
|
fields = ['child', 'name', 'start']
|
2017-10-28 17:27:33 +00:00
|
|
|
widgets = {
|
|
|
|
'start': forms.DateTimeInput(attrs={
|
2020-01-28 04:07:42 +00:00
|
|
|
'readonly': 'readonly',
|
2017-10-28 17:27:33 +00:00
|
|
|
'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
|
|
|
|
2020-01-29 18:50:23 +00:00
|
|
|
class TummyTimeForm(CoreModelForm):
|
2017-08-16 18:57:46 +00:00
|
|
|
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={
|
2020-01-28 04:07:42 +00:00
|
|
|
'readonly': 'readonly',
|
2017-09-17 19:48:23 +00:00
|
|
|
'data-target': '#datetimepicker_start',
|
2017-08-16 18:57:46 +00:00
|
|
|
}),
|
|
|
|
'end': forms.DateTimeInput(attrs={
|
2020-01-28 04:07:42 +00:00
|
|
|
'readonly': 'readonly',
|
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
|
|
|
|
2017-11-10 02:15:09 +00:00
|
|
|
|
2020-01-29 18:50:23 +00:00
|
|
|
class WeightForm(CoreModelForm):
|
2017-11-10 02:15:09 +00:00
|
|
|
class Meta:
|
|
|
|
model = models.Weight
|
2020-02-15 04:10:08 +00:00
|
|
|
fields = ['child', 'weight', 'date', 'notes']
|
2017-11-10 02:15:09 +00:00
|
|
|
widgets = {
|
|
|
|
'date': forms.DateInput(attrs={
|
2020-01-28 04:07:42 +00:00
|
|
|
'readonly': 'readonly',
|
2017-11-10 02:15:09 +00:00
|
|
|
'data-target': '#datetimepicker_date',
|
|
|
|
}),
|
2020-02-16 22:03:50 +00:00
|
|
|
'notes': forms.Textarea(attrs={'rows': 5}),
|
2017-11-10 02:15:09 +00:00
|
|
|
}
|