Use user first and last name, when available, in UI

This commit is contained in:
Christopher C. Wells 2020-02-18 10:36:59 -08:00 committed by Christopher Charbonneau Wells
parent 192d8d9b09
commit d0b33c2b1b
6 changed files with 26 additions and 9 deletions

View File

@ -232,7 +232,9 @@
href="#"
data-toggle="dropdown"
aria-haspopup="true"
aria-expanded="false"><i class="icon icon-user" aria-hidden="true"></i> {{ request.user }}
aria-expanded="false">
<i class="icon icon-user" aria-hidden="true"></i>
{% firstof user.get_full_name user.get_username %}
</a>
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="nav-user-menu-link">
<h6 class="dropdown-header">{% trans "User" %}</h6>

View File

@ -440,6 +440,13 @@ class Timer(models.Model):
child=self.child)
return title
@property
def user_username(self):
""" Get Timer user's name with a preference for the full name. """
if self.user.get_full_name():
return self.user.get_full_name()
return self.user.get_username()
@classmethod
def from_db(cls, db, field_names, values):
instance = super(Timer, cls).from_db(db, field_names, values)

View File

@ -31,7 +31,7 @@
{% endif %}
</p>
<p class="text-muted">
{% blocktrans trimmed with user=object.user %}
{% blocktrans trimmed with user=object.user_username %}
{{ timer }} created by {{ user }}
{% endblocktrans %}
</p>

View File

@ -45,7 +45,7 @@
<td>{{ timer.duration|duration_string }}</td>
<td>{{ timer.end }}</td>
<td>{{ timer.active|bool_icon }}</td>
<td>{{ timer.user }}</td>
<td>{{ timer.user_username }}</td>
</tr>
{% empty %}
<tr>

View File

@ -171,15 +171,16 @@ class TimerTestCase(TestCase):
last_name='Last',
birth_date=timezone.localdate()
)
self.user = User.objects.first()
self.named = models.Timer.objects.create(
name='Named',
end=timezone.localtime(),
user=User.objects.first(),
user=self.user,
child=child
)
self.unnamed = models.Timer.objects.create(
end=timezone.localtime(),
user=User.objects.first()
user=self.user
)
def test_timer_create(self):
@ -202,6 +203,13 @@ class TimerTestCase(TestCase):
'{} ({})'.format(str(self.named), str(self.named.child))
)
def test_timer_user_username(self):
self.assertEqual(self.named.user_username, self.user.get_username())
self.user.first_name = 'User'
self.user.last_name = 'Name'
self.user.save()
self.assertEqual(self.named.user_username, self.user.get_full_name())
def test_timer_restart(self):
self.named.restart()
self.assertIsNone(self.named.end)

View File

@ -13,12 +13,12 @@
{% block listgroup %}
<ul class="list-group list-group-flush">
{% for instance in instances %}
<a href="{% url 'core:timer-detail' instance.id %}"
{% for timer in instances %}
<a href="{% url 'core:timer-detail' timer.id %}"
class="list-group-item list-group-item-action">
<strong>{{ instance.title_with_child }}</strong>
<strong>{{ timer.title_with_child }}</strong>
<p class="text-muted small m-0">
{% blocktrans trimmed with start=instance.start|time user=instance.user %}
{% blocktrans trimmed with start=timer.start|time user=timer.user_username %}
Started by {{ user }} at {{ start }}
{% endblocktrans %}
</p>