diff --git a/gameserver/models/cache.py b/gameserver/models/cache.py index 0a38599..833e02a 100644 --- a/gameserver/models/cache.py +++ b/gameserver/models/cache.py @@ -120,14 +120,14 @@ def update_or_create(cls, change_in_score: int, user: "User", update_flags: bool def get_rank(cls, user: "User") -> int: """ Get the rank of a user - + There are a couple issues with implementing this function as cls.ranks().get(user=user).rank - The biggest is django is lazy and the user's rank will always be 1 The only way I see to implement this would be to use raw SQL (see cls.ranks().query) """ raise NotImplementedError - + @classmethod def reset_data(cls, users: Optional[QuerySet["User"]] = None): from django.contrib.auth import get_user_model @@ -290,6 +290,7 @@ def update_or_create( queryset.update( points=F("points") + change_in_score, last_correct_submission=timezone.now() ) + @classmethod def reset_data(cls, contest: Optional["Contest"] = None, all: bool = False): assert contest is not None or all, "Either contest or all must be set to True" diff --git a/gameserver/views/contest.py b/gameserver/views/contest.py index 186805a..a3f0cb6 100644 --- a/gameserver/views/contest.py +++ b/gameserver/views/contest.py @@ -199,10 +199,21 @@ def get_title(self): def get_queryset(self): if self.model.cache.should_reset(self.request): ContestScore.reset_data(contest=self.object) - return ContestScore.ranks(contest=self.object).select_related( + + ranks = ContestScore.ranks(contest=self.object).select_related( "participation__team" ) # select related reduces queries from around 54 to 17ish so 8ms to 5ms + query = self.request.GET.get("q") + + if query: + ranks = ranks.filter( + Q(participation__team__name__icontains=query) + | Q(participation__team__members__username__icontains=query) + | Q(participation__team__members__full_name__icontains=query) + ).distinct() + return ranks + def _get_contest(self, slug): return get_object_or_404(models.Contest, slug=slug) diff --git a/gameserver/views/user.py b/gameserver/views/user.py index 526128d..bd58e87 100644 --- a/gameserver/views/user.py +++ b/gameserver/views/user.py @@ -31,14 +31,14 @@ class UserList(ListView, mixin.MetaMixin): def get_queryset(self) -> models.QuerySet: if self.model.cache.should_reset(self.request): UserScore.reset_data() - return UserScore.ranks() - - + + query = self.request.GET.get("q") + ranks = UserScore.ranks() - - return ranks + if query: + ranks = ranks.filter(user__username__icontains=query) - + return ranks def get(self, request, *args, **kwargs): if request.in_contest: