Skip to content

Commit c92e9c0

Browse files
Added user list and contest lb filtering (without templates)
1 parent 7e31cfc commit c92e9c0

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

gameserver/models/cache.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,14 @@ def update_or_create(cls, change_in_score: int, user: "User", update_flags: bool
120120
def get_rank(cls, user: "User") -> int:
121121
"""
122122
Get the rank of a user
123-
123+
124124
There are a couple issues with implementing this function as
125125
cls.ranks().get(user=user).rank
126126
- The biggest is django is lazy and the user's rank will always be 1
127127
The only way I see to implement this would be to use raw SQL (see cls.ranks().query)
128128
"""
129129
raise NotImplementedError
130-
130+
131131
@classmethod
132132
def reset_data(cls, users: Optional[QuerySet["User"]] = None):
133133
from django.contrib.auth import get_user_model
@@ -290,6 +290,7 @@ def update_or_create(
290290
queryset.update(
291291
points=F("points") + change_in_score, last_correct_submission=timezone.now()
292292
)
293+
293294
@classmethod
294295
def reset_data(cls, contest: Optional["Contest"] = None, all: bool = False):
295296
assert contest is not None or all, "Either contest or all must be set to True"

gameserver/views/contest.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,21 @@ def get_title(self):
199199
def get_queryset(self):
200200
if self.model.cache.should_reset(self.request):
201201
ContestScore.reset_data(contest=self.object)
202-
return ContestScore.ranks(contest=self.object).select_related(
202+
203+
ranks = ContestScore.ranks(contest=self.object).select_related(
203204
"participation__team"
204205
) # select related reduces queries from around 54 to 17ish so 8ms to 5ms
205206

207+
query = self.request.GET.get("q")
208+
209+
if query:
210+
ranks = ranks.filter(
211+
Q(participation__team__name__icontains=query)
212+
| Q(participation__team__members__username__icontains=query)
213+
| Q(participation__team__members__full_name__icontains=query)
214+
).distinct()
215+
return ranks
216+
206217
def _get_contest(self, slug):
207218
return get_object_or_404(models.Contest, slug=slug)
208219

gameserver/views/user.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ class UserList(ListView, mixin.MetaMixin):
3131
def get_queryset(self) -> models.QuerySet:
3232
if self.model.cache.should_reset(self.request):
3333
UserScore.reset_data()
34-
return UserScore.ranks()
35-
36-
34+
35+
query = self.request.GET.get("q")
36+
3737
ranks = UserScore.ranks()
38-
39-
return ranks
38+
if query:
39+
ranks = ranks.filter(user__username__icontains=query)
4040

41-
41+
return ranks
4242

4343
def get(self, request, *args, **kwargs):
4444
if request.in_contest:

0 commit comments

Comments
 (0)