From 4978208d7fafe393b4c7a62104eb1aac9baadeca Mon Sep 17 00:00:00 2001 From: Peter Hardy Date: Thu, 26 Mar 2020 22:15:08 +1100 Subject: [PATCH 1/5] Prepopulate method for formula types (#127). --- core/forms.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/core/forms.py b/core/forms.py index d0e8e3ce..0afe5465 100644 --- a/core/forms.py +++ b/core/forms.py @@ -43,12 +43,16 @@ def set_initial_values(kwargs, form_type): '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']: last_feeding = models.Feeding.objects.filter( child=kwargs['initial']['child']).order_by('end').last() 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 == 'formula': + last_feed_args['method'] = 'bottle' + kwargs['initial'].update(last_feed_args) # Remove custom kwargs so they do not interfere with `super` calls. for key in ['child', 'timer']: From dce0cd1e10b4e2b6c6291b4db0d6def27dc8546e Mon Sep 17 00:00:00 2001 From: Peter Hardy Date: Thu, 26 Mar 2020 22:16:07 +1100 Subject: [PATCH 2/5] Update method select for when formula types selected (#127) --- core/templates/core/feeding_form.html | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/core/templates/core/feeding_form.html b/core/templates/core/feeding_form.html index dc2a8570..d4e51e82 100644 --- a/core/templates/core/feeding_form.html +++ b/core/templates/core/feeding_form.html @@ -35,5 +35,11 @@ defaultDate: false }); 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'); + } + }); -{% endblock %} \ No newline at end of file +{% endblock %} From 4581722e0b499e60ef1694ab3a7143e92f7f12da Mon Sep 17 00:00:00 2001 From: Peter Hardy Date: Thu, 26 Mar 2020 22:28:27 +1100 Subject: [PATCH 3/5] Actually check for FBM as well (#127). --- core/forms.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/forms.py b/core/forms.py index 0afe5465..68bfc1c8 100644 --- a/core/forms.py +++ b/core/forms.py @@ -50,7 +50,7 @@ def set_initial_values(kwargs, form_type): if last_feeding: last_type = last_feeding.type last_feed_args = {'type': last_feeding.type} - if last_type == 'formula': + if last_type in ['formula', 'fortified breast milk']: last_feed_args['method'] = 'bottle' kwargs['initial'].update(last_feed_args) From dfd89ae3394e3549e7043a41e85ed1e86b2c3478 Mon Sep 17 00:00:00 2001 From: Peter Hardy Date: Sat, 28 Mar 2020 21:54:07 +1100 Subject: [PATCH 4/5] Tests for initial feeding methods (#127) --- core/tests/tests_forms.py | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/core/tests/tests_forms.py b/core/tests/tests_forms.py index 26c949a6..4c8ba7b6 100644 --- a/core/tests/tests_forms.py +++ b/core/tests/tests_forms.py @@ -87,29 +87,49 @@ class InitialValuesTestCase(FormsTestCaseBase): last_name='Two', 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( child=self.child, - start=timezone.localtime() - timezone.timedelta(hours=4), - end=timezone.localtime() - timezone.timedelta(hours=3, minutes=30), + start=start_time, + end=end_time, type='breast milk', method='left breast' ) f_two = models.Feeding.objects.create( child=child_two, - start=timezone.localtime() - timezone.timedelta(hours=4), - end=timezone.localtime() - timezone.timedelta(hours=3, minutes=30), + start=start_time, + end=end_time, type='formula', 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/') self.assertTrue('type' not in page.context['form'].initial) page = self.c.get('/feedings/add/?child={}'.format(self.child.slug)) 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)) 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): self.timer.stop() From cbf6f0ce1ea5c91c457547a39d299e682356ef40 Mon Sep 17 00:00:00 2001 From: Christopher Charbonneau Wells Date: Mon, 30 Mar 2020 05:25:57 -0700 Subject: [PATCH 5/5] Fix minor linting issues --- core/tests/tests_forms.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/core/tests/tests_forms.py b/core/tests/tests_forms.py index 4c8ba7b6..b665b008 100644 --- a/core/tests/tests_forms.py +++ b/core/tests/tests_forms.py @@ -93,7 +93,8 @@ class InitialValuesTestCase(FormsTestCaseBase): birth_date=timezone.localdate() ) start_time = timezone.localtime() - timezone.timedelta(hours=4) - end_time = timezone.localtime() - timezone.timedelta(hours=3, minutes=30) + end_time = timezone.localtime() - timezone.timedelta(hours=3, + minutes=30) f_one = models.Feeding.objects.create( child=self.child, start=start_time, @@ -129,7 +130,8 @@ class InitialValuesTestCase(FormsTestCaseBase): 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) + self.assertEqual(page.context['form'].initial['method'], + f_three.method) def test_timer_set(self): self.timer.stop()