mirror of https://github.com/snachodog/mybuddy.git
Merge pull request #138 from phardy/method-select
Auto select "bottle" method for formula and fortified milk types
This commit is contained in:
commit
8898a04edf
|
@ -43,12 +43,16 @@ def set_initial_values(kwargs, form_type):
|
||||||
'end': timer.end or timezone.now()
|
'end': timer.end or timezone.now()
|
||||||
})
|
})
|
||||||
|
|
||||||
# Set initial type value for Feeding instance based on last type used.
|
# Set type and method values for Feeding instance based on last feed.
|
||||||
if form_type == FeedingForm and 'child' in kwargs['initial']:
|
if form_type == FeedingForm and 'child' in kwargs['initial']:
|
||||||
last_feeding = models.Feeding.objects.filter(
|
last_feeding = models.Feeding.objects.filter(
|
||||||
child=kwargs['initial']['child']).order_by('end').last()
|
child=kwargs['initial']['child']).order_by('end').last()
|
||||||
if last_feeding:
|
if last_feeding:
|
||||||
kwargs['initial'].update({'type': last_feeding.type})
|
last_type = last_feeding.type
|
||||||
|
last_feed_args = {'type': last_feeding.type}
|
||||||
|
if last_type in ['formula', 'fortified breast milk']:
|
||||||
|
last_feed_args['method'] = 'bottle'
|
||||||
|
kwargs['initial'].update(last_feed_args)
|
||||||
|
|
||||||
# Remove custom kwargs so they do not interfere with `super` calls.
|
# Remove custom kwargs so they do not interfere with `super` calls.
|
||||||
for key in ['child', 'timer']:
|
for key in ['child', 'timer']:
|
||||||
|
|
|
@ -35,5 +35,11 @@
|
||||||
defaultDate: false
|
defaultDate: false
|
||||||
});
|
});
|
||||||
BabyBuddy.DatetimePicker.init($('#datetimepicker_end'));
|
BabyBuddy.DatetimePicker.init($('#datetimepicker_end'));
|
||||||
|
$('#id_type').change(function() {
|
||||||
|
var feed_type=$('#id_type').val();
|
||||||
|
if (feed_type=='formula'||feed_type=='fortified breast milk') {
|
||||||
|
$('#id_method').val('bottle');
|
||||||
|
}
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
{% endblock %}
|
{% endblock %}
|
|
@ -87,29 +87,51 @@ class InitialValuesTestCase(FormsTestCaseBase):
|
||||||
last_name='Two',
|
last_name='Two',
|
||||||
birth_date=timezone.localdate()
|
birth_date=timezone.localdate()
|
||||||
)
|
)
|
||||||
|
child_three = models.Child.objects.create(
|
||||||
|
first_name='Child',
|
||||||
|
last_name='Three',
|
||||||
|
birth_date=timezone.localdate()
|
||||||
|
)
|
||||||
|
start_time = timezone.localtime() - timezone.timedelta(hours=4)
|
||||||
|
end_time = timezone.localtime() - timezone.timedelta(hours=3,
|
||||||
|
minutes=30)
|
||||||
f_one = models.Feeding.objects.create(
|
f_one = models.Feeding.objects.create(
|
||||||
child=self.child,
|
child=self.child,
|
||||||
start=timezone.localtime() - timezone.timedelta(hours=4),
|
start=start_time,
|
||||||
end=timezone.localtime() - timezone.timedelta(hours=3, minutes=30),
|
end=end_time,
|
||||||
type='breast milk',
|
type='breast milk',
|
||||||
method='left breast'
|
method='left breast'
|
||||||
)
|
)
|
||||||
f_two = models.Feeding.objects.create(
|
f_two = models.Feeding.objects.create(
|
||||||
child=child_two,
|
child=child_two,
|
||||||
start=timezone.localtime() - timezone.timedelta(hours=4),
|
start=start_time,
|
||||||
end=timezone.localtime() - timezone.timedelta(hours=3, minutes=30),
|
end=end_time,
|
||||||
type='formula',
|
type='formula',
|
||||||
method='bottle'
|
method='bottle'
|
||||||
)
|
)
|
||||||
|
f_three = models.Feeding.objects.create(
|
||||||
|
child=child_three,
|
||||||
|
start=start_time,
|
||||||
|
end=end_time,
|
||||||
|
type='fortified breast milk',
|
||||||
|
method='bottle'
|
||||||
|
)
|
||||||
|
|
||||||
page = self.c.get('/feedings/add/')
|
page = self.c.get('/feedings/add/')
|
||||||
self.assertTrue('type' not in page.context['form'].initial)
|
self.assertTrue('type' not in page.context['form'].initial)
|
||||||
|
|
||||||
page = self.c.get('/feedings/add/?child={}'.format(self.child.slug))
|
page = self.c.get('/feedings/add/?child={}'.format(self.child.slug))
|
||||||
self.assertEqual(page.context['form'].initial['type'], f_one.type)
|
self.assertEqual(page.context['form'].initial['type'], f_one.type)
|
||||||
|
self.assertFalse('method' in page.context['form'].initial)
|
||||||
|
|
||||||
page = self.c.get('/feedings/add/?child={}'.format(child_two.slug))
|
page = self.c.get('/feedings/add/?child={}'.format(child_two.slug))
|
||||||
self.assertEqual(page.context['form'].initial['type'], f_two.type)
|
self.assertEqual(page.context['form'].initial['type'], f_two.type)
|
||||||
|
self.assertEqual(page.context['form'].initial['method'], f_two.method)
|
||||||
|
|
||||||
|
page = self.c.get('/feedings/add/?child={}'.format(child_three.slug))
|
||||||
|
self.assertEqual(page.context['form'].initial['type'], f_three.type)
|
||||||
|
self.assertEqual(page.context['form'].initial['method'],
|
||||||
|
f_three.method)
|
||||||
|
|
||||||
def test_timer_set(self):
|
def test_timer_set(self):
|
||||||
self.timer.stop()
|
self.timer.stop()
|
||||||
|
|
Loading…
Reference in New Issue