/
forms.py
255 lines (216 loc) · 7.92 KB
/
forms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# -*- coding: utf-8 -*-
from django import forms
from django.conf import settings
from django.utils import timezone
from django.utils.translation import gettext as _
from core import models
def set_initial_values(kwargs, form_type):
"""
Sets initial value for add forms based on provided kwargs.
:param kwargs: Keyword arguments.
:param form_type: Class of the type of form being initialized.
:return: Keyword arguments with updated "initial" values.
"""
# Never update initial values for existing instance (e.g. edit operation).
if kwargs.get('instance', None):
return kwargs
# Add the "initial" kwarg if it does not already exist.
if not kwargs.get('initial'):
kwargs.update(initial={})
# 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:
timer = models.Timer.objects.get(id=timer_id)
kwargs['initial'].update({
'timer': timer,
'start': timer.start,
'end': timer.end or timezone.now()
})
# Set type and method values for Feeding instance based on last feed.
if form_type == FeedingForm and 'child' in kwargs['initial']:
last_feeding = models.Feeding.objects.filter(
child=kwargs['initial']['child']).order_by('end').last()
if last_feeding:
last_method = last_feeding.method
last_feed_args = {'type': last_feeding.type}
if last_method not in ['left breast', 'right breast']:
last_feed_args['method'] = last_method
kwargs['initial'].update(last_feed_args)
# Remove custom kwargs so they do not interfere with `super` calls.
for key in ['child', 'timer']:
try:
kwargs.pop(key)
except KeyError:
pass
return kwargs
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
class ChildForm(forms.ModelForm):
class Meta:
model = models.Child
fields = [
'first_name',
'last_name',
'birth_date'
]
if settings.BABY_BUDDY['ALLOW_UPLOADS']:
fields.append('picture')
widgets = {
'birth_date': forms.DateInput(attrs={
'autocomplete': 'off',
'readonly': 'readonly',
'data-target': '#datetimepicker_date',
}),
}
class ChildDeleteForm(forms.ModelForm):
confirm_name = forms.CharField(max_length=511)
class Meta:
model = models.Child
fields = []
def clean_confirm_name(self):
confirm_name = self.cleaned_data['confirm_name']
if confirm_name != str(self.instance):
raise forms.ValidationError(
_('Name does not match child name.'), code='confirm_mismatch')
return confirm_name
def save(self, commit=True):
instance = self.instance
self.instance.delete()
return instance
class DiaperChangeForm(CoreModelForm):
class Meta:
model = models.DiaperChange
fields = ['child', 'time', 'wet', 'solid', 'color', 'amount', 'notes']
widgets = {
'time': forms.DateTimeInput(attrs={
'autocomplete': 'off',
'readonly': 'readonly',
'data-target': '#datetimepicker_time',
}),
'notes': forms.Textarea(attrs={'rows': 5}),
}
class FeedingForm(CoreModelForm):
class Meta:
model = models.Feeding
fields = ['child', 'start', 'end', 'type', 'method', 'amount', 'notes']
widgets = {
'start': forms.DateTimeInput(attrs={
'autocomplete': 'off',
'readonly': 'readonly',
'data-target': '#datetimepicker_start',
}),
'end': forms.DateTimeInput(attrs={
'autocomplete': 'off',
'readonly': 'readonly',
'data-target': '#datetimepicker_end',
}),
'notes': forms.Textarea(attrs={'rows': 5}),
}
class NoteForm(CoreModelForm):
class Meta:
model = models.Note
fields = ['child', 'note', 'time']
widgets = {
'time': forms.DateTimeInput(attrs={
'autocomplete': 'off',
'readonly': 'readonly',
'data-target': '#datetimepicker_time',
}),
}
class SleepForm(CoreModelForm):
class Meta:
model = models.Sleep
fields = ['child', 'start', 'end', 'notes']
widgets = {
'start': forms.DateTimeInput(attrs={
'autocomplete': 'off',
'readonly': 'readonly',
'data-target': '#datetimepicker_start',
}),
'end': forms.DateTimeInput(attrs={
'autocomplete': 'off',
'readonly': 'readonly',
'data-target': '#datetimepicker_end',
}),
'notes': forms.Textarea(attrs={'rows': 5}),
}
class TemperatureForm(CoreModelForm):
class Meta:
model = models.Temperature
fields = ['child', 'temperature', 'time', 'notes']
widgets = {
'time': forms.DateTimeInput(attrs={
'autocomplete': 'off',
'readonly': 'readonly',
'data-target': '#datetimepicker_time',
}),
'notes': forms.Textarea(attrs={'rows': 5}),
}
class TimerForm(CoreModelForm):
class Meta:
model = models.Timer
fields = ['child', 'name', 'start']
widgets = {
'start': forms.DateTimeInput(attrs={
'autocomplete': 'off',
'readonly': 'readonly',
'data-target': '#datetimepicker_start',
})
}
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
class TummyTimeForm(CoreModelForm):
class Meta:
model = models.TummyTime
fields = ['child', 'start', 'end', 'milestone']
widgets = {
'start': forms.DateTimeInput(attrs={
'autocomplete': 'off',
'readonly': 'readonly',
'data-target': '#datetimepicker_start',
}),
'end': forms.DateTimeInput(attrs={
'autocomplete': 'off',
'readonly': 'readonly',
'data-target': '#datetimepicker_end',
}),
}
class WeightForm(CoreModelForm):
class Meta:
model = models.Weight
fields = ['child', 'weight', 'date', 'notes']
widgets = {
'date': forms.DateInput(attrs={
'autocomplete': 'off',
'readonly': 'readonly',
'data-target': '#datetimepicker_date',
}),
'notes': forms.Textarea(attrs={'rows': 5}),
}