From 46c950dc37b28c73e0525a3a824f194f66fd828b Mon Sep 17 00:00:00 2001 From: Phuoc Anh Kha Le <76896393+anhkha2003@users.noreply.github.com> Date: Tue, 4 Jun 2024 19:40:20 -0500 Subject: [PATCH] Edit displaying rating and points rank for unlisted (#115) * Edit displaying rating and points rank for unlisted * Edit rating/points rank of unlisted users and cache get_rank functions --- judge/caching.py | 6 ++++-- judge/ratings.py | 4 ++-- judge/signals.py | 2 ++ judge/utils/users.py | 6 ++++++ templates/profile-table.html | 12 +++++------- templates/user/user-about.html | 14 ++++++-------- 6 files changed, 25 insertions(+), 19 deletions(-) diff --git a/judge/caching.py b/judge/caching.py index d86cc30..f40adb1 100644 --- a/judge/caching.py +++ b/judge/caching.py @@ -79,8 +79,10 @@ def cache_wrapper(prefix, timeout=None, expected_type=None): return result result = func(*args, **kwargs) if result is None: - result = NONE_RESULT - _set(cache_key, result, timeout) + cache_result = NONE_RESULT + else: + cache_result = result + _set(cache_key, cache_result, timeout) return result def dirty(*args, **kwargs): diff --git a/judge/ratings.py b/judge/ratings.py index bfcb828..854bdcb 100644 --- a/judge/ratings.py +++ b/judge/ratings.py @@ -7,7 +7,6 @@ from django.db.models import Count, OuterRef, Subquery from django.db.models.functions import Coalesce from django.utils import timezone - BETA2 = 328.33**2 RATING_INIT = 1200 # Newcomer's rating when applying the rating floor/ceiling MEAN_INIT = 1400.0 @@ -147,7 +146,7 @@ def recalculate_ratings(ranking, old_mean, times_ranked, historical_p): def rate_contest(contest): from judge.models import Rating, Profile from judge.models.profile import _get_basic_info - from judge.utils.users import get_contest_ratings + from judge.utils.users import get_contest_ratings, get_rating_rank rating_subquery = Rating.objects.filter(user=OuterRef("user")) rating_sorted = rating_subquery.order_by("-contest__end_time") @@ -241,6 +240,7 @@ def rate_contest(contest): _get_basic_info.dirty_multi([(uid,) for uid in user_ids]) get_contest_ratings.dirty_multi([(uid,) for uid in user_ids]) + get_rating_rank.dirty_multi([(uid,) for uid in user_ids]) RATING_LEVELS = [ diff --git a/judge/signals.py b/judge/signals.py index 614a21b..c3029bb 100644 --- a/judge/signals.py +++ b/judge/signals.py @@ -72,6 +72,8 @@ def problem_update(sender, instance, **kwargs): @receiver(post_save, sender=Profile) def profile_update(sender, instance, **kwargs): + judge.utils.users.get_points_rank.dirty(instance.id) + judge.utils.users.get_rating_rank.dirty(instance.id) if hasattr(instance, "_updating_stats_only"): return diff --git a/judge/utils/users.py b/judge/utils/users.py index b9eb0b6..9ac071c 100644 --- a/judge/utils/users.py +++ b/judge/utils/users.py @@ -6,7 +6,10 @@ from judge.caching import cache_wrapper from judge.models import Profile, Rating, Submission, Friend, ProfileInfo +@cache_wrapper(prefix="grr") def get_rating_rank(profile): + if profile.is_unlisted: + return None rank = None if profile.rating: rank = ( @@ -19,7 +22,10 @@ def get_rating_rank(profile): return rank +@cache_wrapper(prefix="gpr") def get_points_rank(profile): + if profile.is_unlisted: + return None return ( Profile.objects.filter( is_unlisted=False, diff --git a/templates/profile-table.html b/templates/profile-table.html index de1ad33..17a05ad 100644 --- a/templates/profile-table.html +++ b/templates/profile-table.html @@ -33,15 +33,13 @@ {{ request.profile.performance_points|floatformat(0) }} - {% if not request.profile.is_unlisted %} -
-
{{_('Rating')}} #
-
{{rating_rank if rating_rank else '-'}}
-
- {% endif %} +
+
{{_('Rating')}} #
+
{{rating_rank if rating_rank else '-'}}
+
{{_('Points')}} #
-
{{points_rank}}
+
{{points_rank if points_rank else '-'}}
{% if awards.medals %} diff --git a/templates/user/user-about.html b/templates/user/user-about.html index 69ff3bb..811e8c6 100644 --- a/templates/user/user-about.html +++ b/templates/user/user-about.html @@ -39,18 +39,16 @@ - {% if not user.is_unlisted %} -
-
- - -
+
+ - {% endif %} +