Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow zero for project allocation on hourly projects #1730

Merged
merged 2 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need the staticmethod decorator here? Shouldn't we call it as TimecardObjectForm._is_empty_or_zero to be clearer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't strictly need to use @staticmethod, but I think it's good practice when we don't need to use any properties of the instance or the class. As far as calling the method, self works and is much less verbose than having to repeat the class name.

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())