feat: add events app + "multi-tenants" aka multi-events
This commit is contained in:
@@ -22,10 +22,12 @@ from opus_magnum.models import PuzzleResponse, SteamCollectionItem, SteamCollect
|
||||
|
||||
router = Router()
|
||||
|
||||
PREFIX = "/events/{event_path}/results"
|
||||
|
||||
@router.get("results", response=RankingSchema)
|
||||
def results(request: HttpRequest) -> dict:
|
||||
cache_key = "api:results:results"
|
||||
|
||||
@router.get(f"{PREFIX}/results", response=RankingSchema)
|
||||
def results(request: HttpRequest, event_path: str) -> dict:
|
||||
cache_key = f"api:{event_path}:results:results"
|
||||
cached_data = cache.get(cache_key)
|
||||
|
||||
if cached_data is not None:
|
||||
@@ -35,7 +37,9 @@ def results(request: HttpRequest) -> dict:
|
||||
responses_by_puzzleid = defaultdict(list)
|
||||
|
||||
for response in list(
|
||||
PuzzleResponse.objects.filter(needs_manual_validation=False)
|
||||
PuzzleResponse.objects.filter(
|
||||
needs_manual_validation=False, submission__event=request.event
|
||||
)
|
||||
.filter_user_best_response()
|
||||
.prefetch_related("submission__user")
|
||||
):
|
||||
@@ -51,25 +55,27 @@ def results(request: HttpRequest) -> dict:
|
||||
|
||||
data = {
|
||||
"users": list(CustomUser.objects.filter(pk__in=responses_by_userid.keys())),
|
||||
"puzzles": list(SteamCollectionItem.objects.all()),
|
||||
"puzzles": list(
|
||||
SteamCollectionItem.objects.filter(collection__event=request.event)
|
||||
),
|
||||
"responses_by_userid": dict(responses_by_userid),
|
||||
"ranking_by_puzzle": ranking,
|
||||
}
|
||||
|
||||
cache.set("api:results:results", data, 300)
|
||||
cache.set(cache_key, data, 300)
|
||||
return data
|
||||
|
||||
|
||||
@router.get("top-submissions", response=TournamentSubmissionsOut)
|
||||
def top_submissions(request: HttpRequest, limit: int = 5) -> dict:
|
||||
@router.get(f"{PREFIX}/top-submissions", response=TournamentSubmissionsOut)
|
||||
def top_submissions(request: HttpRequest, event_path: str, limit: int = 5) -> dict:
|
||||
"""Get tournament top submissions for each puzzle. Only available when tournament is closed."""
|
||||
cache_key = f"api:results:top_submissions:{limit}"
|
||||
cache_key = f"api:{event_path}:results:top_submissions:{limit}"
|
||||
cached_data = cache.get(cache_key)
|
||||
|
||||
if cached_data is not None:
|
||||
return cached_data
|
||||
|
||||
collection = get_object_or_404(SteamCollection, is_active=True)
|
||||
collection = get_object_or_404(SteamCollection, is_active=True, event=request.event)
|
||||
|
||||
# Only allow access when tournament is closed
|
||||
if collection.accepting_submissions:
|
||||
@@ -141,20 +147,20 @@ def top_submissions(request: HttpRequest, limit: int = 5) -> dict:
|
||||
)
|
||||
|
||||
data = {"submissions": submissions_list}
|
||||
cache.set(f"api:results:top_submissions:{limit}", data, 300)
|
||||
cache.set(cache_key, data, 300)
|
||||
return data
|
||||
|
||||
|
||||
@router.get("puzzle-results", response=TournamentPuzzleResultsOut)
|
||||
def puzzle_results(request: HttpRequest, limit: int = 5) -> dict:
|
||||
@router.get(f"{PREFIX}/puzzle-results", response=TournamentPuzzleResultsOut)
|
||||
def puzzle_results(request: HttpRequest, event_path: str, limit: int = 5) -> dict:
|
||||
"""Get tournament results organized by puzzle with coefficients. Only available when tournament is closed."""
|
||||
cache_key = f"api:results:puzzle_results:{limit}"
|
||||
cache_key = f"api:{event_path}:results:puzzle_results:{limit}"
|
||||
cached_data = cache.get(cache_key)
|
||||
|
||||
if cached_data is not None:
|
||||
return cached_data
|
||||
|
||||
collection = get_object_or_404(SteamCollection, is_active=True)
|
||||
collection = get_object_or_404(SteamCollection, is_active=True, event=request.event)
|
||||
|
||||
# Only allow access when tournament is closed
|
||||
if collection.accepting_submissions:
|
||||
@@ -237,5 +243,5 @@ def puzzle_results(request: HttpRequest, limit: int = 5) -> dict:
|
||||
)
|
||||
|
||||
data = {"results": results_list}
|
||||
cache.set(f"api:results:puzzle_results:{limit}", data, 300)
|
||||
cache.set(cache_key, data, 300)
|
||||
return data
|
||||
|
||||
Reference in New Issue
Block a user