diff --git a/tock/hours/tests/test_views.py b/tock/hours/tests/test_views.py index ea9b0a5a..3f9f596b 100644 --- a/tock/hours/tests/test_views.py +++ b/tock/hours/tests/test_views.py @@ -1042,8 +1042,8 @@ def test_active_prefills_added_to_timecard(self): user=self.user, ) - # Only our prefilled object should appear in this form. - self.assertEqual(len(response.context['formset'].forms), 1) + # The single active prefill and an empty project line should appear in this form. + self.assertEqual(len(response.context['formset'].forms), 2) form = response.context['formset'].forms[0] @@ -1071,11 +1071,13 @@ def test_active_prefills_added_to_timecard_pulling_existing_timecard_info(self): user=self.user, ) - # Only our prefilled object should appear in this form. - self.assertEqual(len(response.context['formset'].forms), 2) + # We have 1 project which was previously tocked to + # 1 active prefill + # And we always render one extra so we should have 3 projects on our timecard prefill = response.context['formset'].forms[0] previous = response.context['formset'].forms[1] + empty = response.context['formset'].forms[2] # Check that our prefill information is what we expect it to be. self.assertEqual(prefill.initial['project'], self.pfd1.project.id) @@ -1089,6 +1091,10 @@ def test_active_prefills_added_to_timecard_pulling_existing_timecard_info(self): self.assertEqual(previous.initial['project'], tco.project.id) self.assertEqual(previous.initial['hours_spent'], None) + # Check that our extra row rendered without values + self.assertEqual(empty.initial, {}) + + def test_active_prefills_fill_in_hours_from_previous_timecard(self): tco_1 = hours.models.TimecardObject.objects.create( timecard=self.timecard_1, diff --git a/tock/hours/views.py b/tock/hours/views.py index a0141963..21adf605 100644 --- a/tock/hours/views.py +++ b/tock/hours/views.py @@ -536,18 +536,13 @@ def prefilled_formset(self): .values_list('project', 'hours') ) project_ids = [] - extra = 1 - # Check to see if we have a timecard from a previous reporting period. - # If we do, pull all of the project IDs from it and set our extra - # amount to be amount of existing project IDs + 1 to account for the - # form construction. + # Check to see if we have a timecard from a previous reporting period. If yes, grab projects which were tocked. if timecard: project_ids = timecard.timecardobjects.values_list( 'project_id', flat=True ) - extra = len(project_ids) + 1 reporting_period = ReportingPeriod.objects.prefetch_related( 'holiday_prefills__project' @@ -556,7 +551,7 @@ def prefilled_formset(self): # Check to see if there are hoilday prefills for the reporting period. # If there are, add them to the existing timecard prefill dictionary # we setup so that they are accounted for in addition to any other - # prefills we need to add to the timecard. + # prefills we'll add to the timecard. if reporting_period.holiday_prefills: for holiday_prefill in reporting_period.holiday_prefills.all(): timecard_prefills[holiday_prefill.project.id] =\ @@ -583,8 +578,12 @@ def prefilled_formset(self): for project_id, hours in timecard_prefills.items() ] - formset = timecard_formset_factory(extra=extra) - return formset(initial=init) + # Render all of our initial forms and an extra blank one + extra = len(init) + 1 + + FormSet = timecard_formset_factory(extra=extra) + return FormSet(initial=init) + def form_valid(self, form): context = self.get_context_data()