Skip to content

Commit

Permalink
Added user list and contest lb filtering (without templates)
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonLovesDoggo committed Apr 6, 2024
1 parent 7e31cfc commit c92e9c0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
5 changes: 3 additions & 2 deletions gameserver/models/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
13 changes: 12 additions & 1 deletion gameserver/views/contest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
12 changes: 6 additions & 6 deletions gameserver/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit c92e9c0

Please sign in to comment.