Skip to content

Commit

Permalink
Redéfinition de Profile.get_avatar_url()
Browse files Browse the repository at this point in the history
  • Loading branch information
Situphen committed Sep 30, 2024
1 parent a1c7460 commit adc324a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 25 deletions.
4 changes: 2 additions & 2 deletions zds/member/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ def test_detail_of_the_member(self):
self.assertEqual(profile.user.is_active, response.data.get("is_active"))
self.assertIsNotNone(response.data.get("date_joined"))
self.assertEqual(profile.site, response.data.get("site"))
self.assertEqual(profile.get_avatar_url(), response.data.get("avatar_url"))
self.assertEqual(profile.get_absolute_avatar_url(), response.data.get("avatar_url"))
self.assertEqual(profile.biography, response.data.get("biography"))
self.assertEqual(profile.sign, response.data.get("sign"))
self.assertFalse(response.data.get("show_email"))
Expand Down Expand Up @@ -442,7 +442,7 @@ def test_detail_of_a_member(self):
self.assertEqual(self.profile.user.is_active, response.data.get("is_active"))
self.assertIsNotNone(response.data.get("date_joined"))
self.assertEqual(self.profile.site, response.data.get("site"))
self.assertEqual(self.profile.get_avatar_url(), response.data.get("avatar_url"))
self.assertEqual(self.profile.get_absolute_avatar_url(), response.data.get("avatar_url"))
self.assertEqual(self.profile.biography, response.data.get("biography"))
self.assertEqual(self.profile.sign, response.data.get("sign"))
self.assertFalse(response.data.get("show_email"))
Expand Down
22 changes: 8 additions & 14 deletions zds/member/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,14 @@ def get_city(self):

return geo_location

def get_avatar_url(self, size=80):
"""Get the avatar URL for this profile.
If the user has defined a custom URL, use it.
If not, use Gravatar.
:return: The avatar URL for this profile
:rtype: str
"""
if self.avatar_url:
if self.avatar_url.startswith(settings.MEDIA_URL):
return "{}{}".format(settings.ZDS_APP["site"]["url"], self.avatar_url)
else:
return self.avatar_url
else:
return ""
def get_absolute_avatar_url(self):
"""Gets the avatar URL of this profile.
:return: The absolute URL of this profile's avatar
:rtype: str or None
"""
if self.avatar_url and self.avatar_url.startswith(settings.MEDIA_URL):
return settings.ZDS_APP["site"]["url"] + self.avatar_url
return self.avatar_url

def get_post_count(self):
"""
Expand Down
13 changes: 7 additions & 6 deletions zds/member/tests/tests_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,21 @@ def test_get_absolute_url_for_details_of_member(self):

def test_get_avatar_url(self):
# if no url was specified -> nothing !
self.assertEqual("", self.user1.get_avatar_url())
self.assertEqual(self.user1.get_absolute_avatar_url(), None)

# if an url is specified -> take it !
user2 = ProfileFactory()
testurl = "http://test.com/avatar.jpg"
user2.avatar_url = testurl
self.assertEqual(user2.get_avatar_url(), testurl)
self.assertEqual(user2.get_absolute_avatar_url(), testurl)

# if url is relative, send absolute url
gallerie_avtar = GalleryFactory()
image_avatar = ImageFactory(gallery=gallerie_avtar)
gallerie_avatar = GalleryFactory()
image_avatar = ImageFactory(gallery=gallerie_avatar)
user2.avatar_url = image_avatar.physical.url
self.assertNotEqual(user2.get_avatar_url(), image_avatar.physical.url)
self.assertIn("http", user2.get_avatar_url())
self.assertNotEqual(user2.get_absolute_avatar_url(), image_avatar.physical.url)
self.assertIn(image_avatar.physical.url, user2.get_absolute_avatar_url())
self.assertIn("http", user2.get_absolute_avatar_url())

def test_get_post_count(self):
# Start with 0
Expand Down
2 changes: 1 addition & 1 deletion zds/mp/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def test_expand_list_of_private_topics_for_author(self):
self.assertEqual(response.status_code, status.HTTP_200_OK)
author = response.data.get("results")[0].get("author")
self.assertEqual(author.get("username"), self.profile.user.username)
self.assertEqual(author.get("avatar_url"), self.profile.get_avatar_url())
self.assertEqual(author.get("avatar_url"), self.profile.get_absolute_avatar_url())

def test_create_private_topics_with_client_unauthenticated(self):
"""
Expand Down
3 changes: 1 addition & 2 deletions zds/utils/templatetags/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from django.core.cache import cache

from zds.member.models import Profile
from zds.utils.templatetags.remove_url_scheme import remove_url_scheme

register = template.Library()

Expand Down Expand Up @@ -77,7 +76,7 @@ def state(current_user):
def avatar(profile: Profile, size=80) -> dict:
if profile is not None:
return {
"avatar_url": remove_url_scheme(profile.get_avatar_url(size)),
"avatar_url": profile.avatar_url,
"avatar_size": size,
"username": profile.user.username,
}

0 comments on commit adc324a

Please sign in to comment.