Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Commit

Permalink
Simplify creation of rank list from leaderboard query
Browse files Browse the repository at this point in the history
  • Loading branch information
TimJentzsch committed Nov 19, 2021
1 parent db88f4a commit 1295076
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .isort.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
line_length = 88
multi_line_output = 3
include_trailing_comma = True
known_third_party = beeline,better_exceptions,blossom_wrapper,bugsnag,dateparser,django,django_cte,django_filters,dotenv,dpath,drf_yasg,mimesis,praw,prawcore,psaw,pytest,pytest_django,pytz,requests,rest_framework,rest_framework_api_key,slack,social_core,stripe,toml
known_third_party = beeline,better_exceptions,blossom_wrapper,bugsnag,dateparser,django,django_filters,dotenv,dpath,drf_yasg,mimesis,praw,prawcore,psaw,pytest,pytest_django,pytz,requests,rest_framework,rest_framework_api_key,slack,social_core,stripe,toml
skip=venv,.venv,env,migrations
13 changes: 5 additions & 8 deletions api/views/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,9 +819,7 @@ def leaderboard(self, request: Request,) -> Response:
above_count = int(request.GET.get("above_count", 5))
below_count = int(request.GET.get("below_count", 5))

above_data = None
user_data = None
below_data = None
above_data = user_data = below_data = None

rank_query = (
# Apply the provided submission filters
Expand All @@ -843,15 +841,14 @@ def leaderboard(self, request: Request,) -> Response:
.values("id", "username", "gamma", "date_joined")
.order_by(F("gamma").desc(), F("date_joined").desc())
)
# We can't filter the query further, because we'd lose the rank annotations
# We convert the query into a list to extract the necessary entries
# TODO: This is very inefficient, maybe there's a better way to do this?
query_list = list(rank_query)

# Originally we used window expressions to annotate the ranks directly
# https://stackoverflow.com/questions/54595867/django-model-how-to-add-order-index-annotation
# Unfortunately that is not supported on all backends
rank_list = [{**entry, "rank": i + 1} for i, entry in enumerate(query_list)]
# Instead, we convert the query into a list and also add the ranks manually
rank_list = rank_list = [
{**entry, "rank": i + 1} for i, entry in enumerate(rank_query)
]

# Find the top users
top_data = rank_list[:top_count]
Expand Down

0 comments on commit 1295076

Please sign in to comment.