Skip to content

Commit

Permalink
Merge pull request #1730 from 18F/nmb/1728-zero-allocation
Browse files Browse the repository at this point in the history
Allow zero for project allocation on hourly projects
  • Loading branch information
neilmb authored Mar 13, 2024
2 parents f2d5f31 + fca1271 commit 100a7fd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
29 changes: 25 additions & 4 deletions tock/hours/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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(
Expand Down Expand Up @@ -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.'
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions tock/hours/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

0 comments on commit 100a7fd

Please sign in to comment.