Ad profile table (#110)

This commit is contained in:
Phuoc Anh Kha Le 2024-05-21 23:09:22 -05:00 committed by GitHub
parent ee17bc0778
commit 5335bc248f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 474 additions and 274 deletions

View file

@ -147,6 +147,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
rating_subquery = Rating.objects.filter(user=OuterRef("user"))
rating_sorted = rating_subquery.order_by("-contest__end_time")
@ -239,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])
RATING_LEVELS = [

61
judge/utils/users.py Normal file
View file

@ -0,0 +1,61 @@
from django.urls import reverse
from django.utils.formats import date_format
from django.utils.translation import gettext as _, gettext_lazy
from judge.caching import cache_wrapper
from judge.models import Profile, Rating, Submission, Friend, ProfileInfo
def get_rating_rank(profile):
rank = None
if profile.rating:
rank = (
Profile.objects.filter(
is_unlisted=False,
rating__gt=profile.rating,
).count()
+ 1
)
return rank
def get_points_rank(profile):
return (
Profile.objects.filter(
is_unlisted=False,
performance_points__gt=profile.performance_points,
).count()
+ 1
)
@cache_wrapper(prefix="gcr")
def get_contest_ratings(profile):
return (
profile.ratings.order_by("-contest__end_time")
.select_related("contest")
.defer("contest__description")
)
def get_awards(profile):
ratings = get_contest_ratings(profile)
sorted_ratings = sorted(
ratings, key=lambda x: (x.rank, -x.contest.end_time.timestamp())
)
result = [
{
"label": rating.contest.name,
"ranking": rating.rank,
"link": reverse("contest_ranking", args=(rating.contest.key,))
+ "#!"
+ profile.username,
"date": date_format(rating.contest.end_time, _("M j, Y")),
}
for rating in sorted_ratings
if rating.rank <= 3
]
return result

View file

@ -25,6 +25,7 @@ from judge.utils.cachedict import CacheDict
from judge.utils.diggpaginator import DiggPaginator
from judge.utils.tickets import filter_visible_tickets
from judge.utils.views import TitleMixin
from judge.utils.users import get_rating_rank, get_points_rank, get_awards
from judge.views.feed import FeedView
@ -81,6 +82,26 @@ class HomeFeedView(FeedView):
)
Profile.prefetch_profile_cache([p.id for p in context["top_rated"]])
Profile.prefetch_profile_cache([p.id for p in context["top_scorer"]])
if self.request.user.is_authenticated:
context["rating_rank"] = get_rating_rank(self.request.profile)
context["points_rank"] = get_points_rank(self.request.profile)
medals_list = get_awards(self.request.profile)
context["awards"] = {
"medals": medals_list,
"gold_count": 0,
"silver_count": 0,
"bronze_count": 0,
}
for medal in medals_list:
if medal["ranking"] == 1:
context["awards"]["gold_count"] += 1
elif medal["ranking"] == 2:
context["awards"]["silver_count"] += 1
elif medal["ranking"] == 3:
context["awards"]["bronze_count"] += 1
return context

View file

@ -43,6 +43,12 @@ from judge.tasks import import_users
from judge.utils.problems import contest_completed_ids, user_completed_ids
from judge.utils.ranker import ranker
from judge.utils.unicode import utf8text
from judge.utils.users import (
get_rating_rank,
get_points_rank,
get_awards,
get_contest_ratings,
)
from judge.utils.views import (
QueryStringSortMixin,
TitleMixin,
@ -142,22 +148,10 @@ class UserPage(TitleMixin, UserMixin, DetailView):
rating = self.object.ratings.order_by("-contest__end_time")[:1]
context["rating"] = rating[0] if rating else None
context["rank"] = (
Profile.objects.filter(
is_unlisted=False,
performance_points__gt=self.object.performance_points,
).count()
+ 1
)
context["points_rank"] = get_points_rank(self.object)
if rating:
context["rating_rank"] = (
Profile.objects.filter(
is_unlisted=False,
rating__gt=self.object.rating,
).count()
+ 1
)
context["rating_rank"] = get_rating_rank(self.object)
context["rated_users"] = Profile.objects.filter(
is_unlisted=False, rating__isnull=False
).count()
@ -185,42 +179,9 @@ EPOCH = datetime(1970, 1, 1, tzinfo=timezone.utc)
class UserAboutPage(UserPage):
template_name = "user/user-about.html"
def get_awards(self, ratings):
result = {}
sorted_ratings = sorted(
ratings, key=lambda x: (x.rank, -x.contest.end_time.timestamp())
)
result["medals"] = [
{
"label": rating.contest.name,
"ranking": rating.rank,
"link": reverse("contest_ranking", args=(rating.contest.key,))
+ "#!"
+ self.object.username,
"date": date_format(rating.contest.end_time, _("M j, Y")),
}
for rating in sorted_ratings
if rating.rank <= 3
]
num_awards = 0
for i in result:
num_awards += len(result[i])
if num_awards == 0:
result = None
return result
def get_context_data(self, **kwargs):
context = super(UserAboutPage, self).get_context_data(**kwargs)
ratings = context["ratings"] = (
self.object.ratings.order_by("-contest__end_time")
.select_related("contest")
.defer("contest__description")
)
ratings = context["ratings"] = get_contest_ratings(self.object)
context["rating_data"] = mark_safe(
json.dumps(
@ -244,7 +205,7 @@ class UserAboutPage(UserPage):
)
)
context["awards"] = self.get_awards(ratings)
context["awards"] = get_awards(self.object)
if ratings:
user_data = self.object.ratings.aggregate(Min("rating"), Max("rating"))