Skip to content

Commit

Permalink
Merge pull request #1258 from 18F/jk/tc-prefills
Browse files Browse the repository at this point in the history
Update timecard prefilling
  • Loading branch information
Jkrzy authored Mar 26, 2021
2 parents 70c94e5 + 5638e8a commit 623554b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
14 changes: 10 additions & 4 deletions tock/hours/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand Down
17 changes: 8 additions & 9 deletions tock/hours/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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] =\
Expand All @@ -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()
Expand Down

0 comments on commit 623554b

Please sign in to comment.