Skip to content

Commit

Permalink
Furthur optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonLovesDoggo committed Apr 2, 2024
1 parent 61de749 commit a65c804
Show file tree
Hide file tree
Showing 9 changed files with 245 additions and 205 deletions.
51 changes: 44 additions & 7 deletions gameserver/models/cache.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import TYPE_CHECKING, Optional, Self

from typing import TYPE_CHECKING, Optional, Self, Protocol
from django.http import HttpRequest
from django.apps import apps
from django.db import models, transaction
from django.db.models import (
Expand All @@ -8,28 +8,59 @@
Count,
F,
OuterRef,
Q,
Subquery,
Sum,
Value,
When,
Window,
)
from django.db.models.functions import Coalesce, DenseRank, Rank, RowNumber
from django.db.models.functions import Coalesce, Rank, RowNumber

from .contest import Contest, ContestParticipation, ContestSubmission

if TYPE_CHECKING:
from .profile import User
from .contest import Contest, ContestParticipation, ContestSubmission


class ResetableCache(Protocol):
def can_reset(cls, request: HttpRequest) -> None: ...


class CacheMeta(models.Model):

class Meta:
abstract = True
permissions = [
(
"can_reset_cache",
"Designates if the user has permission to reset the scoring caches or not.",
)
]

@classmethod
def _can_reset(cls, request: HttpRequest):
return all(
[
request.user.is_authenticated,
request.user.is_staff,
request.GET.get("reset", "") == "true",
]
)


class UserScore(models.Model):
class UserScore(CacheMeta):
user = models.OneToOneField("User", on_delete=models.CASCADE, db_index=True)
points = models.PositiveIntegerField(help_text="The amount of points.", default=0)
flag_count = models.PositiveIntegerField(
help_text="The amount of flags the user/team has.", default=0
)

@classmethod
def can_reset(cls, request: HttpRequest):
return cls._can_reset(request) and request.user.has_perm(
"gameserver.can_reset_cache_user_score"
)

def __str__(self) -> str:
return self.user.username

Expand Down Expand Up @@ -97,7 +128,7 @@ def reset_data(cls):
cls.objects.bulk_create(scores_to_create)


class ContestScore(models.Model):
class ContestScore(CacheMeta):
participation = models.OneToOneField(
"ContestParticipation", on_delete=models.CASCADE, db_index=True
)
Expand All @@ -106,6 +137,12 @@ class ContestScore(models.Model):
help_text="The amount of flags the user/team has.", default=0
)

@classmethod
def can_reset(cls, request: HttpRequest):
return cls._can_reset(request) and request.user.has_perm(
"gameserver.can_reset_cache_user_score"
)

def get_absolute_url(self):
return self.participation.get_absolute_url()

Expand Down
3 changes: 3 additions & 0 deletions gameserver/models/contest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from django.urls import reverse
from django.utils import timezone
from django.utils.functional import cached_property
from gameserver.models.cache import ContestScore

from ..templatetags.common_tags import strfdelta
from . import abstract
Expand Down Expand Up @@ -227,6 +228,8 @@ def get_editable_contests(cls, user):


class ContestParticipation(models.Model):
cache = ContestScore

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.ContestScore = apps.get_model("gameserver", "ContestScore", require_ready=True)
Expand Down
Loading

0 comments on commit a65c804

Please sign in to comment.