From 29ca87dc00aedc50f37437884ab9e0692838ea5e Mon Sep 17 00:00:00 2001 From: Situphen Date: Tue, 1 Oct 2024 00:23:59 +0200 Subject: [PATCH] =?UTF-8?q?Red=C3=A9finition=20de=20Profile.get=5Favatar?= =?UTF-8?q?=5Furl()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- zds/member/api/serializers.py | 6 +++--- zds/member/api/tests.py | 4 ++-- zds/member/models.py | 22 ++++++++-------------- zds/member/tests/tests_models.py | 15 ++++++++------- zds/mp/api/tests.py | 2 +- zds/utils/templatetags/profile.py | 3 +-- 6 files changed, 23 insertions(+), 29 deletions(-) diff --git a/zds/member/api/serializers.py b/zds/member/api/serializers.py index 0f8b614e35..8fa9d7d9cf 100644 --- a/zds/member/api/serializers.py +++ b/zds/member/api/serializers.py @@ -16,7 +16,7 @@ class UserListSerializer(serializers.ModelSerializer): serializers. """ - avatar_url = serializers.CharField(source="profile.get_avatar_url") + avatar_url = serializers.CharField(source="profile.get_absolute_avatar_url") html_url = serializers.CharField(source="get_absolute_url") class Meta: @@ -34,7 +34,7 @@ class ProfileListSerializer(serializers.ModelSerializer): html_url = serializers.CharField(source="user.get_absolute_url") is_active = serializers.BooleanField(source="user.is_active") date_joined = serializers.DateTimeField(source="user.date_joined") - avatar_url = serializers.CharField(source="get_avatar_url") + avatar_url = serializers.CharField(source="get_absolute_avatar_url") permissions = DRYPermissionsField(additional_actions=["ban"]) class Meta: @@ -78,7 +78,7 @@ class ProfileDetailSerializer(serializers.ModelSerializer): email = serializers.EmailField(source="user.email") is_active = serializers.BooleanField(source="user.is_active") date_joined = serializers.DateTimeField(source="user.date_joined") - avatar_url = serializers.CharField(source="get_avatar_url") + avatar_url = serializers.CharField(source="get_absolute_avatar_url") permissions = DRYPermissionsField(additional_actions=["ban"]) class Meta: diff --git a/zds/member/api/tests.py b/zds/member/api/tests.py index 9160869e7b..20103d34bc 100644 --- a/zds/member/api/tests.py +++ b/zds/member/api/tests.py @@ -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")) @@ -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")) diff --git a/zds/member/models.py b/zds/member/models.py index 78d76fa2b8..53fefa63d9 100644 --- a/zds/member/models.py +++ b/zds/member/models.py @@ -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): """ diff --git a/zds/member/tests/tests_models.py b/zds/member/tests/tests_models.py index de6ef2eb91..2a74b4e608 100644 --- a/zds/member/tests/tests_models.py +++ b/zds/member/tests/tests_models.py @@ -29,22 +29,23 @@ def setUp(self): def test_get_absolute_url_for_details_of_member(self): self.assertEqual(self.user1.get_absolute_url(), f"/@{self.user1.user.username}") - def test_get_avatar_url(self): + def test_get_absolute_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 diff --git a/zds/mp/api/tests.py b/zds/mp/api/tests.py index 25b065d0b3..7a0290dcf1 100644 --- a/zds/mp/api/tests.py +++ b/zds/mp/api/tests.py @@ -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): """ diff --git a/zds/utils/templatetags/profile.py b/zds/utils/templatetags/profile.py index 356ecfba10..87bd6f7283 100644 --- a/zds/utils/templatetags/profile.py +++ b/zds/utils/templatetags/profile.py @@ -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() @@ -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, }