From 15062b3cda5985bed5b779e11550eb9eb8c18c0c Mon Sep 17 00:00:00 2001 From: Oliver Falk Date: Fri, 26 Sep 2025 09:21:00 +0200 Subject: [PATCH] Return full avatar URLs instead of digests in stats - Replace digest_sha256 with avatar_url in top_viewed_avatars - Replace digest_sha256 with avatar_url in top_queried_emails - Replace digest_sha256 with avatar_url in top_queried_openids - All avatar URLs now use https://libravatar.org/avatar/{digest} format - Update tests to verify avatar_url presence and correct format - All 5 stats tests pass successfully This makes the stats API more user-friendly by providing complete avatar URLs that can be directly used in applications instead of requiring clients to construct the URLs themselves. --- ivatar/test_views_stats.py | 24 ++++++++++++++++++++++++ ivatar/views.py | 8 ++++---- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/ivatar/test_views_stats.py b/ivatar/test_views_stats.py index 309db08..4a951d1 100644 --- a/ivatar/test_views_stats.py +++ b/ivatar/test_views_stats.py @@ -160,6 +160,14 @@ class StatsTester(TestCase): 100, "top avatar access count incorrect", ) + # Check that avatar_url is present and starts with the correct base URL + self.assertIn("avatar_url", j["top_viewed_avatars"][0], "avatar_url missing") + self.assertTrue( + j["top_viewed_avatars"][0]["avatar_url"].startswith( + "https://libravatar.org/avatar/" + ), + "avatar_url should start with https://libravatar.org/avatar/", + ) # Test top queried emails self.assertIn("top_queried_emails", j, "top_queried_emails missing") @@ -171,6 +179,14 @@ class StatsTester(TestCase): 100, "top email access count incorrect", ) + # Check that avatar_url is present and starts with the correct base URL + self.assertIn("avatar_url", j["top_queried_emails"][0], "avatar_url missing") + self.assertTrue( + j["top_queried_emails"][0]["avatar_url"].startswith( + "https://libravatar.org/avatar/" + ), + "avatar_url should start with https://libravatar.org/avatar/", + ) # Test top queried openids self.assertIn("top_queried_openids", j, "top_queried_openids missing") @@ -182,6 +198,14 @@ class StatsTester(TestCase): 75, "top openid access count incorrect", ) + # Check that avatar_url is present and starts with the correct base URL + self.assertIn("avatar_url", j["top_queried_openids"][0], "avatar_url missing") + self.assertTrue( + j["top_queried_openids"][0]["avatar_url"].startswith( + "https://libravatar.org/avatar/" + ), + "avatar_url should start with https://libravatar.org/avatar/", + ) # Test photo format distribution self.assertIn( diff --git a/ivatar/views.py b/ivatar/views.py index 25cc3b3..a0d43d9 100644 --- a/ivatar/views.py +++ b/ivatar/views.py @@ -589,7 +589,7 @@ class StatsView(TemplateView, JsonResponse): top_photos_data.append( { "access_count": top_associated.access_count, - "digest_sha256": top_associated.digest_sha256, + "avatar_url": f"https://libravatar.org/avatar/{top_associated.digest_sha256}", } ) else: @@ -597,7 +597,7 @@ class StatsView(TemplateView, JsonResponse): top_photos_data.append( { "access_count": top_associated.access_count, - "digest_sha256": top_associated.digest, + "avatar_url": f"https://libravatar.org/avatar/{top_associated.digest}", } ) @@ -610,7 +610,7 @@ class StatsView(TemplateView, JsonResponse): top_emails_data.append( { "access_count": email.access_count, - "digest_sha256": email.digest_sha256, + "avatar_url": f"https://libravatar.org/avatar/{email.digest_sha256}", } ) @@ -623,7 +623,7 @@ class StatsView(TemplateView, JsonResponse): top_openids_data.append( { "access_count": openid.access_count, - "digest_sha256": openid.digest, + "avatar_url": f"https://libravatar.org/avatar/{openid.digest}", } )