Skip to content

Commit

Permalink
ReportingPeriodUserDetailView cleanup, utilization calculation update (
Browse files Browse the repository at this point in the history
…#1072)

* update utilization calculation in ReportingPeriodUserDetailView, clean up table rendering

* remove unused import
  • Loading branch information
timoballard authored Apr 28, 2020
1 parent 9554519 commit b2c9f8f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 43 deletions.
19 changes: 15 additions & 4 deletions tock/hours/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,27 +226,38 @@ def setUp(self):
self.billable_project = projects.models.Project.objects.filter(
accounting_code__billable=True
)[0]
self.excluded_project = projects.models.Project.objects.filter(
exclude_from_billability=True
)[0]
self.timecard_obj_0 = hours.models.TimecardObject.objects.create(
timecard=self.timecard,
project=self.nonbillable_project,
hours_spent=13
hours_spent=11
)
self.timecard_obj_1 = hours.models.TimecardObject.objects.create(
timecard=self.timecard,
project=self.billable_project,
hours_spent=27
)
self.timecard_obj_2 = hours.models.TimecardObject.objects.create(
timecard=self.timecard,
project=self.excluded_project,
hours_spent=2
)
self.timecard.save()

def test_user_reporting_period_report(self):
self.client.force_login(self.user)
response = self.client.get(reverse(
'reports:ReportingPeriodUserDetailView',
kwargs={'reporting_period':'1999-12-31', 'username':'aaron.snow'}
))
self.assertEqual(response.context['user_utilization'], '67.5%')
self.assertEqual(response.context['user_all_hours'], 40.00)
self.assertEqual(response.context['user_utilization'], Decimal('0.90'))
self.assertEqual(response.context['user_target_hours'], 30.00)
self.assertEqual(response.context['user_billable_hours'], 27)
self.assertContains(response, '67.5%')
self.assertEqual(response.context['user_non_billable_hours'], 11)
self.assertEqual(response.context['user_excluded_hours'], 2)
self.assertContains(response, '90%')

@override_settings(STARTING_FY_FOR_REPORTS_PAGE=2015)
class ReportTests(WebTest):
Expand Down
36 changes: 11 additions & 25 deletions tock/hours/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from rest_framework.permissions import IsAuthenticated
from tock.remote_user_auth import email_to_username
from tock.utils import IsSuperUserOrSelf, PermissionMixin
from utilization.utils import calculate_utilization

from .forms import (ReportingPeriodForm, ReportingPeriodImportForm,
TimecardForm, TimecardFormSet, projects_as_choices,
Expand Down Expand Up @@ -783,31 +782,18 @@ def get_object(self):
def get_context_data(self, **kwargs):
rp_period = self.kwargs['reporting_period']
username = self.kwargs['username']
user_billable_hours = TimecardObject.objects.filter(
timecard__reporting_period__start_date=rp_period,
timecard__user__username=username,
project__accounting_code__billable=True
).aggregate(
(
Sum('hours_spent')
)
)['hours_spent__sum']

user_all_hours = TimecardObject.objects.filter(
timecard__reporting_period__start_date=rp_period,
timecard__user__username=username,
).aggregate(
(
Sum('hours_spent')
)
)['hours_spent__sum']

timecard = Timecard.objects.get(
reporting_period__start_date=rp_period,
user__username=username
)

context = super(
ReportingPeriodUserDetailView, self).get_context_data(**kwargs)
context['user_billable_hours'] = user_billable_hours
context['user_all_hours'] = user_all_hours
context['user_utilization'] = calculate_utilization(
context['user_billable_hours'],
context['user_all_hours']
)
context['user_billable_hours'] = timecard.billable_hours
context['user_non_billable_hours'] = timecard.non_billable_hours
context['user_excluded_hours'] = timecard.excluded_hours
context['user_target_hours'] = timecard.target_hours
context['user_utilization'] = timecard.utilization

return context
3 changes: 2 additions & 1 deletion tock/projects/fixtures/projects.json
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@
"fields":{
"name":"Out Of Office",
"description":"",
"accounting_code":1
"accounting_code":1,
"exclude_from_billability":true
},
"pk":1
},
Expand Down
26 changes: 13 additions & 13 deletions tock/tock/templates/hours/reporting_period_user_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

{% block content %}

<table class="table-responsive-reflow">
<table class="usa-table table-responsive-reflow">
<caption>
<h1>{{ object.user }}'s time from {{ object.reporting_period.start_date }} to {{ object.reporting_period.end_date }}</h1>
</caption>
Expand All @@ -14,6 +14,7 @@ <h1>{{ object.user }}'s time from {{ object.reporting_period.start_date }} to {{
<th>Project</th>
<th>Billable</th>
<th>Hours spent</th>
<th>Notes</th>
</tr>
</thead>
{% for entry in object.timecardobjects.all %}
Expand All @@ -28,39 +29,38 @@ <h1>{{ object.user }}'s time from {{ object.reporting_period.start_date }} to {{
{% endif %}
</td>
<td data-title="Hours spent">{{ entry.hours_spent }}</td>
</tr>
{% if entry.notes %}
<tr>
<td colspan="3">
<strong>Notes entered for {{ entry.project }}</strong>
<td data-title="Notes">
{% for note in entry.notes_list %}
<p>{{ note }}</p>
<p>{{ note }}</p>
{% endfor %}
</td>
</tr>
</tbody>
{% endif %}
{% endfor %}
</table>
<div>First Submitted: {{ object.created }}</div>
Last Changed: {{ object.modified }}

<table class="table-responsive-reflow">
<table class="usa-table table-responsive-reflow">
<caption>
<h3>Reporting Period Summary</h3>
</caption>
<thead>
<tr>
<th>Billable Hours</th>
<th>Total Hours</th>
<th>Billable %<br><span class="table-subtext">(billable hrs / total hrs)</span></th>
<th>Non-Billable Hours</th>
<th>Excluded Hours</th>
<th>Target Hours</th>
<th>Utilization</th>
</tr>
</thead>
<tbody>
<tr>
<td data-title="Billable hours"> {{ user_billable_hours }} </td>
<td data-title="Total hours"> {{ user_all_hours }} </td>
<td data-title="Billable %"> {{ user_utilization }} </td>
<td data-title="Non-Billable hours"> {{ user_non_billable_hours }} </td>
<td data-title="Excluded hours"> {{ user_excluded_hours }} </td>
<td data-title="Target hours"> {{ user_target_hours }} </td>
<td data-title="Utilization"> {% widthratio user_utilization 1 100 %}% </td>
</tr>
</tbody>
</table>
Expand Down

0 comments on commit b2c9f8f

Please sign in to comment.