Skip to content

Commit 87d0759

Browse files
author
julioest
committed
refactor: Move avatar logic to User model
Move _get_author_avatar from demo view to User.get_avatar_url() with walrus operator.
1 parent 06e37aa commit 87d0759

2 files changed

Lines changed: 17 additions & 18 deletions

File tree

core/views.py

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,23 +1071,6 @@ class V3ComponentDemoView(TemplateView):
10711071

10721072
template_name = "base.html"
10731073

1074-
@staticmethod
1075-
def _get_author_avatar(author):
1076-
"""Return the best available avatar URL for a library author (User).
1077-
1078-
Returns empty string when no image is available so the avatar
1079-
template falls back to a colored initials circle.
1080-
"""
1081-
if not author:
1082-
return ""
1083-
url = author.get_thumbnail_url()
1084-
if url:
1085-
return url
1086-
ca = getattr(author, "commitauthor", None)
1087-
if ca and getattr(ca, "avatar_url", None):
1088-
return ca.avatar_url
1089-
return ""
1090-
10911074
def get_context_data(self, **kwargs):
10921075
from django.urls import reverse
10931076
from libraries.models import Library, LibraryVersion
@@ -1836,7 +1819,7 @@ def get_context_data(self, **kwargs):
18361819
"author": {
18371820
"name": author.display_name if author else "Unknown",
18381821
"role": "Contributor",
1839-
"avatar_url": self._get_author_avatar(author),
1822+
"avatar_url": author.get_avatar_url() if author else "",
18401823
"badge_url": f"{badge_img}/badge-first-place.png",
18411824
},
18421825
"doc_url": reverse(

users/models.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,22 @@ def get_thumbnail_url(self):
289289
with suppress(AttributeError, MissingSource):
290290
return getattr(self.image_thumbnail, "url", None)
291291

292+
def get_avatar_url(self):
293+
"""Return the best available avatar URL.
294+
295+
Tries the profile image thumbnail first, then falls back to
296+
the linked CommitAuthor avatar. Returns empty string when no
297+
image is available so the avatar template falls back to a
298+
colored initials circle.
299+
"""
300+
if url := self.get_thumbnail_url():
301+
return url
302+
if (ca := getattr(self, "commitauthor", None)) and getattr(
303+
ca, "avatar_url", None
304+
):
305+
return ca.avatar_url
306+
return ""
307+
292308
def get_hq_image_url(self):
293309
# convenience method for templates
294310
if self.hq_image and self.hq_image_render:

0 commit comments

Comments
 (0)