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

Use preferred names for templates in Tock #1167

Merged
merged 9 commits into from
Jan 29, 2021
Merged
6 changes: 5 additions & 1 deletion tock/employees/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ def save(self, *args, **kwargs):
)
super(EmployeeGrade, self).save(*args, **kwargs)


class UserData(models.Model):
user = models.OneToOneField(User, related_name='user_data', verbose_name='Tock username', on_delete=models.CASCADE)
start_date = models.DateField(blank=True, null=True, verbose_name='Employee start date')
Expand Down Expand Up @@ -180,6 +179,11 @@ def is_late(self):
else:
return False

@cached_property
tbaxter-18f marked this conversation as resolved.
Show resolved Hide resolved
def display_name(self):
Sgtpluck marked this conversation as resolved.
Show resolved Hide resolved
"""If we have First/Last use those, otherwise fallback to username"""
return self.user.get_full_name() or self.user.username

Sgtpluck marked this conversation as resolved.
Show resolved Hide resolved
def save(self, *args, **kwargs):
"""Aligns User model and UserData model attributes on save."""
user = User.objects.get(username=self.user)
Expand Down
12 changes: 11 additions & 1 deletion tock/employees/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,14 @@ def test_is_18f_employee_true_if_18f(self):
def test_billable_expectation(self):
self.regular_user_userdata.expected_billable_hours = 30
expected = 30 / settings.HOURS_IN_A_REGULAR_WORK_WEEK
self.assertEqual(self.regular_user_userdata.billable_expectation, expected)
self.assertEqual(self.regular_user_userdata.billable_expectation, expected)

def test_display_name_if_no_full_name(self):
expected = self.regular_user.username
self.assertEqual(self.regular_user_userdata.display_name, expected)

def test_display_name_if_full_name(self):
self.regular_user.first_name = 'Hank'
self.regular_user.last_name = 'Venture'
expected = self.regular_user.get_full_name()
self.assertEqual(self.regular_user_userdata.display_name, expected)
4 changes: 2 additions & 2 deletions tock/employees/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ class UserFormView(PermissionMixin, FormView):
permission_classes = (IsSuperUserOrSelf, )

def get_context_data(self, **kwargs):
kwargs['username'] = self.kwargs['username']
user = User.objects.get(username=kwargs['username'])
user = User.objects.get(username=self.kwargs['username'])
kwargs['display_name'] = user.user_data.display_name
context = super(UserFormView, self).get_context_data(**kwargs)
context.update(user_billing_context(user))
return _add_recent_tock_table(user, context)
Expand Down
8 changes: 0 additions & 8 deletions tock/hours/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,6 @@ def test_admin_bulk_timecards(self):
for row in rows:
self.assertEqual(set(row.keys()), expected_fields)

class ProjectTimelineTests(WebTest):
fixtures = FIXTURES

def test_project_timeline(self):
res = client().get(reverse('reports:UserTimelineView'))
self.assertIn(
'aaron.snow,,2015-06-01,2015-06-08,False,20.00', str(res.content))

class UtilTests(TestCase):

def test_number_of_hours_util(self):
Expand Down
2 changes: 1 addition & 1 deletion tock/projects/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ def test_project_timeline(self):
self.assertEqual(
res['groups'],
{
self.user: {
self.user.user_data.display_name: {
Sgtpluck marked this conversation as resolved.
Show resolved Hide resolved
obj.timecard.reporting_period.start_date: obj.hours_spent
for obj in self.objs_recent
}
Expand Down
5 changes: 2 additions & 3 deletions tock/projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from hours.models import TimecardObject
from .models import Project


class ProjectListView(LoginRequiredMixin, ListView):
""" View for listing all of the projects, sort projects by name """
model = Project
Expand Down Expand Up @@ -53,7 +52,7 @@ def project_timeline(project, period_limit=5):
).order_by(
'-timecard__reporting_period__start_date'
).select_related(
'timecard__user',
'timecard__user__user_data',
'timecard__reporting_period',
)

Expand All @@ -72,7 +71,7 @@ def project_timeline(project, period_limit=5):
break
periods.append(report_date)

groups[tc.user][report_date] = float(t.hours_spent)
groups[tc.user.user_data.display_name][report_date] = float(t.hours_spent)
Sgtpluck marked this conversation as resolved.
Show resolved Hide resolved

return {
'groups': dict(groups),
Expand Down
2 changes: 1 addition & 1 deletion tock/tock/templates/employees/user_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{% endblock %}

{% block content %}
<h1>Let's Tock about {{ object.user.first_name }} {{ object.user.last_name }}!</h1>
<h1>Let's Tock about {{ object.display_name }}!</h1>
<table id="dataTable" class="striped usa-table">
<tr>
<td><b>First Name:</b></td>
Expand Down
4 changes: 2 additions & 2 deletions tock/tock/templates/employees/user_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{% endblock %}

{% block content %}
<h1>Let's Tock about {{ username }}!</h1>
<h1>Let's Tock about {{ display_name }}!</h1>
Sgtpluck marked this conversation as resolved.
Show resolved Hide resolved
{% include 'employees/user_utilization.html' %}
{% include 'employees/user_recent_tocks.html' %}
{% endblock %}
{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
{% for employee in unit.utilization.last_week_data %}
<tr>
<td>
<a href="{% url 'employees:UserDetailView' employee.username %}">{{employee.username}}</a>
<a href="{% url 'employees:UserDetailView' employee.display_name %}">{{employee.display_name}}</a>
</td>
{% with unit.utilization.last_week_data|index:forloop.counter0 as data %}
{% include 'utilization/includes/unit_utilization_cell.html' %}
Expand Down
4 changes: 2 additions & 2 deletions tock/utilization/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ def test_excludes_user_with_no_recent_hours(self):

utilization_data = response.context['object_list'][0]['utilization']

self.assertEqual(0, len([u for u in utilization_data['last_week_data'] if u['username'] == self.user_with_no_hours.username]))
self.assertEqual(0, len([u for u in utilization_data['last_month_data'] if u['username'] == self.user_with_no_hours.username]))
self.assertEqual(0, len([u for u in utilization_data['last_week_data'] if u['display_name'] == self.user_with_no_hours.user_data.display_name]))
self.assertEqual(0, len([u for u in utilization_data['last_month_data'] if u['display_name'] == self.user_with_no_hours.user_data.display_name]))

def test_includes_user_no_longer_with_unit(self):
response = self.app.get(
Expand Down
4 changes: 2 additions & 2 deletions tock/utilization/unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ def _get_unit_billing_data(users, unit, recent_periods=None, fiscal_year=False):
start, end, utilization = utilization_report(users, unit=unit)

if utilization:
data = utilization.values('username', 'billable', 'target')
data = utilization.values('billable', 'first_name', 'last_name', 'target', 'username')

# Build context for each employee
output_data = [{
'username': employee['username'],
'display_name': f"{employee['first_name']} {employee['last_name']}".strip() or employee['username'],
'denominator': employee['target'],
'billable': employee['billable'],
'utilization': calculate_utilization(employee['billable'], employee['target'])
Expand Down