From 8059aad26af693d392e141ac90e024616395dac0 Mon Sep 17 00:00:00 2001 From: Neil Martinsen-Burrell Date: Wed, 13 Mar 2024 10:20:29 -0500 Subject: [PATCH 1/2] Failing test for 0 project allocation --- tock/hours/tests/test_forms.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tock/hours/tests/test_forms.py b/tock/hours/tests/test_forms.py index c51f49d8..708758db 100644 --- a/tock/hours/tests/test_forms.py +++ b/tock/hours/tests/test_forms.py @@ -394,3 +394,10 @@ def test_hourly_allocation_to_a_weekly_project(self): self.assertEqual(formset.errors[0]['project'][0], 'You cannot submit hourly billing for a ' 'project under weekly billing') + + def test_zero_project_allocation_to_hourly_project(self): + """Should be able to submit zero project allocation to an hourly billing project""" + form_data = self.form_data() + form_data["timecardobjects-0-project_allocation"] = "0" + formset = TimecardFormSet(form_data, instance=self.timecard) + self.assertTrue(formset.is_valid()) From fca12713f99b5c801ab2a5abc30a219340f335c0 Mon Sep 17 00:00:00 2001 From: Neil Martinsen-Burrell Date: Wed, 13 Mar 2024 10:21:20 -0500 Subject: [PATCH 2/2] Accept 0 project allocation for hourly billing --- tock/hours/forms.py | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/tock/hours/forms.py b/tock/hours/forms.py index 97e107fc..22c8e8e8 100644 --- a/tock/hours/forms.py +++ b/tock/hours/forms.py @@ -170,6 +170,17 @@ def clean_project(self): def clean_hours_spent(self): return self.cleaned_data.get('hours_spent') or 0 + @staticmethod + def _is_empty_or_zero(field): + """Check that a field is either the empty string or the number 0.""" + if field == "": + return True + else: + # not an empty string + if Decimal(field) == 0: + return True + return False + def clean(self): if 'project' in self.cleaned_data: if self.cleaned_data['project'].is_weekly_bill: @@ -183,8 +194,12 @@ def clean(self): ), ) else: - if ('project_allocation' in self.cleaned_data and \ - self.cleaned_data['project_allocation'] != ''): + if ( + "project_allocation" in self.cleaned_data + and not self._is_empty_or_zero( + self.cleaned_data['project_allocation'] + ) + ): self.add_error( 'project', forms.ValidationError( @@ -263,7 +278,10 @@ def clean(self): if form.cleaned_data: if form.cleaned_data.get('DELETE'): continue - if (form.cleaned_data.get('hours_spent') == 0 or None) and (form.cleaned_data.get('project_allocation') == 0 or None): + if ( + form.cleaned_data.get("hours_spent") == 0 + and form.cleaned_data.get("project_allocation") == 0 + ): raise forms.ValidationError( 'If you have a project listed, the number of hours ' 'cannot be blank.' @@ -273,7 +291,10 @@ def clean(self): if not self.save_only: for form in self.forms: try: - if (form.cleaned_data['hours_spent'] == 0 or None) and (form.cleaned_data['project_allocation'] == 0): + if ( + form.cleaned_data['hours_spent'] == 0 + and form.cleaned_data['project_allocation'] == 0 + ): form.cleaned_data.update({'DELETE': True}) except KeyError: pass