Fix error when trying to display a speaker bio without an avatar on the speaker or on the user profile

This commit is contained in:
Michael Hall 2018-09-04 21:36:15 -04:00
parent 93dc62c322
commit e647984e4a
4 changed files with 17 additions and 2 deletions

View file

@ -67,7 +67,7 @@ class UserProfile(models.Model):
def avatar_url(self):
try:
if self.avatar is None or self.avatar.name is None:
if self.avatar is None or self.avatar.name is None or self.avatar.name == '':
return settings.STATIC_URL + 'img/avatar_placeholder.png'
elif self.avatar.name.startswith('http'):
return self.avatar.name

View file

@ -34,6 +34,13 @@ class Speaker(models.Model):
else:
return self.user.avatar
def headshot_url(self):
if self.avatar is not None and self.avatar.name is not None and self.avatar.name != '':
return self.avatar.url
else:
return self.user.avatar_url()
def __str__(self):
if self.title:
return self.title

View file

@ -10,7 +10,7 @@
<div class="container">
<div class="row">
<div class="col-md-9">
<img src="{{speaker.headshot.url}}"/><hr/>
<img src="{{speaker.headshot_url}}"/><hr/>
<h3>{{ speaker.user }}</h3>
<h5 class="text-muted">{{ speaker.title }}</h5>
<p>

View file

@ -42,3 +42,11 @@ class TalkCreationTests(TestCase):
assert(response.status_code == 302)
assert(response.url == resolve_url('user-talks'))
def test_show_speaker_without_avatar(self):
user = User.objects.create(username='testuser', password='12345', is_active=True)
speaker = Speaker.objects.create(user=user.profile)
c = Client()
response = c.get(resolve_url('show-speaker', speaker.id))
assert(response.status_code == 200)
self.assertContains(response, 'avatar_placeholder.png')