From 3a781e07756c49683018ca8ebffca4b8803dec97 Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 11 Mar 2024 02:15:24 -0400 Subject: [PATCH] Fixed rank being broken. --- gameserver/models/cache.py | 16 ++++++++++++++-- gameserver/models/contest.py | 16 ++-------------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/gameserver/models/cache.py b/gameserver/models/cache.py index 4a1ee6e..a6ca1e2 100644 --- a/gameserver/models/cache.py +++ b/gameserver/models/cache.py @@ -1,8 +1,10 @@ from typing import TYPE_CHECKING, Self, Optional from django.apps import apps +from django.contrib.auth.models import Permission +from django.contrib.contenttypes.models import ContentType from django.db import models, transaction -from django.db.models import Count, F, Sum, Window, Value, When, BooleanField, Case +from django.db.models import Count, F, Sum, Window, Value, When, BooleanField, Case, Q from django.db.models.functions import Coalesce, Rank if TYPE_CHECKING: @@ -105,7 +107,17 @@ def contest(self): @classmethod def ranks(cls, contest: "Contest", queryset: Optional[models.QuerySet] = None, participation: Optional["ContestParticipation"] = None) -> models.QuerySet: assert queryset is None or participation is None, "Only one of queryset or participation can be set" - query = cls.objects.filter(participation__contest=contest).prefetch_related("participation") + # contest_content_type = ContentType.objects.get_for_model(apps.get_model("gameserver", "Contest")) + # perm_edit_all_contests = Permission.objects.get( + # codename="edit_all_contests", content_type=contest_content_type + # ) + query = cls.objects.filter(participation__contest=contest).prefetch_related("participation")#.exclude( + # Q(participants__is_superuser=True) + # | Q(participants__groups__permissions=perm_edit_all_contests) + # | Q(participants__user_permissions=perm_edit_all_contests) + # | Q(participants__in=contest.organizers.all()) + # | Q(participants__in=contest.curators.all()) + # ) data = query.annotate(is_solo=Case( When(participation__team_id=None, then=Value(False)), default=Value(True), diff --git a/gameserver/models/contest.py b/gameserver/models/contest.py index d9a5f03..db99e86 100644 --- a/gameserver/models/contest.py +++ b/gameserver/models/contest.py @@ -300,23 +300,11 @@ def time_taken(self) -> str: return strfdelta(timedelta(seconds=round((solve_time - self.contest.start_time).total_seconds()))) def rank(self): - return self.contest.ranks().filter( - participation=self) - - if isinstance(self.points, int): - points = self.points - else: - points = self.points() - return ( - self.contest.ranks() - .annotate(num_participants=Count("participants")) - .filter( - Q(points__gt=points) - | Q(points=points, most_recent_solve_time__lt=self.last_solve_time) + self.contest.ranks().filter( + Q(points__gte=self.points()) ) .count() - + 1 ) def has_attempted(self, problem):