From e647984e4a9c56fc50c2fedde5637e352568bd2d Mon Sep 17 00:00:00 2001 From: Michael Hall Date: Tue, 4 Sep 2018 21:36:15 -0400 Subject: [PATCH] Fix error when trying to display a speaker bio without an avatar on the speaker or on the user profile --- events/models/profiles.py | 2 +- events/models/speakers.py | 7 +++++++ .../templates/get_together/speakers/show_speaker.html | 2 +- get_together/tests/speakers.py | 8 ++++++++ 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/events/models/profiles.py b/events/models/profiles.py index 51720c5..6c283fc 100644 --- a/events/models/profiles.py +++ b/events/models/profiles.py @@ -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 diff --git a/events/models/speakers.py b/events/models/speakers.py index bbd4d72..1b89944 100644 --- a/events/models/speakers.py +++ b/events/models/speakers.py @@ -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 diff --git a/get_together/templates/get_together/speakers/show_speaker.html b/get_together/templates/get_together/speakers/show_speaker.html index 541fdd9..1aecc2c 100644 --- a/get_together/templates/get_together/speakers/show_speaker.html +++ b/get_together/templates/get_together/speakers/show_speaker.html @@ -10,7 +10,7 @@
-
+

{{ speaker.user }}

{{ speaker.title }}

diff --git a/get_together/tests/speakers.py b/get_together/tests/speakers.py index 9ca69fd..727315c 100644 --- a/get_together/tests/speakers.py +++ b/get_together/tests/speakers.py @@ -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') \ No newline at end of file