Skip to content

Commit

Permalink
Merge pull request #1101 from 18F/1098-submitted-only
Browse files Browse the repository at this point in the history
Test for submitted cards in utilization views
  • Loading branch information
Jkrzy committed Jun 15, 2020
2 parents 43a4ba7 + c6d4fd8 commit 918b107
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion tock/employees/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def _add_recent_tock_table(user, context):
for the last `settings.RECENT_TOCKS_TO_REPORT` time periods
"""
prefetch = Prefetch('timecardobjects', queryset=TimecardObject.objects.all().select_related('project'))
recent_tocks = user.timecards.select_related('reporting_period')\
recent_tocks = user.timecards.filter(submitted=True).select_related('reporting_period')\
.order_by('-reporting_period__start_date')\
.prefetch_related(prefetch)[:settings.RECENT_TOCKS_TO_REPORT]
recent_tocks = list(reversed(recent_tocks))
Expand Down
18 changes: 17 additions & 1 deletion tock/utilization/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,26 @@ def setUp(self):

self.old_timecard.save()

def test_unsubmitted_card(self):
"""
If our timecard is unsubmitted, we should have empty data.
"""
self.timecard.submitted = False
self.timecard.save()
response = self.app.get(
url=reverse('utilization:GroupUtilizationView'),
user=self.user
)
self.assertTrue(
len(response.context['object_list'][0]['utilization']['last_week_data']) == 0
)

def test_summary_rows(self):
"""
Row data w/ accurate total present in context
for user created in setup
for user created in setup.
We'll need to submit our previously created timecard first.
"""
response = self.app.get(
url=reverse('utilization:GroupUtilizationView'),
Expand Down
10 changes: 5 additions & 5 deletions tock/utilization/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def _build_utilization_query(users=None,recent_periods=None, fiscal_year=False,
filter_ = limit_to_fy()

if unit:
filter_ = Q(filter_, Q(timecards__unit=unit))
filter_ = Q(filter_, Q(timecards__submitted=True, timecards__unit=unit))

# Using Coalesce to set a default value of 0 if no data is available
billable = Coalesce(Sum('timecards__billable_hours', filter=filter_), 0)
Expand Down Expand Up @@ -67,18 +67,18 @@ def utilization_report(user_qs=None, recent_periods=1, fiscal_year=False, unit=N

def _limit_to_recent_periods(reporting_periods):
"""
Filter component to restrict timecards to those associated with
the provided reporting_periods
Filter component to restrict timecards to only those that have been submitted
and within the provided reporting_periods.
"""
return Q(timecards__reporting_period__in=reporting_periods)
return Q(timecards__submitted=True, timecards__reporting_period__in=reporting_periods)

def limit_to_fy():
"""
Filter component to Limit timecard aggregation to the current fiscal year
"""
current_fy = ReportingPeriod().get_fiscal_year_from_date(datetime.date.today())
fy_start_date = ReportingPeriod().get_fiscal_year_start_date(current_fy)
return Q(timecards__reporting_period__start_date__gte=fy_start_date)
return Q(timecards__submitted=True, timecards__reporting_period__start_date__gte=fy_start_date)

def _get_reporting_periods(count):
"""
Expand Down

0 comments on commit 918b107

Please sign in to comment.