Skip to content

Commit

Permalink
fix rank
Browse files Browse the repository at this point in the history
  • Loading branch information
maelys-buhler committed May 2, 2024
1 parent 3a7e28e commit 3c89108
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion api/masteriqapp/views/RankView.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ def user(self, request, pk):

@action(detail=False, methods=["GET"])
def global_user(self, request):
data_with_score = get_user_model().objects.annotate(global_score=Avg('iq__iq'), row_number=Window(expression=RowNumber())).order_by('-global_score')
data_with_score = get_user_model().objects.annotate(global_score=Avg('iq__iq')).annotate(row_number=Window(expression=RowNumber(), order_by=F('global_score').desc()))
for info in data_with_score:
print(f"User: {info.username} IQ: {info.global_score} Rank: {info.row_number}")
# filter won't be efficient if more element, but not better way to do it found during the project. (Discussed in mail)
user_ranking = list(filter(lambda r: r.id == request.user.id, data_with_score))
data_to_send = {"user_rank": user_ranking[0].row_number, "user_iq": user_ranking[0].global_score}
Expand Down

0 comments on commit 3c89108

Please sign in to comment.