Skip to content

Commit

Permalink
Merge pull request #184 from HE-Arc/fix-rank
Browse files Browse the repository at this point in the history
fix rank
  • Loading branch information
maelys-buhler authored May 2, 2024
2 parents 6ec61e0 + 3c89108 commit 48050f4
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 48050f4

Please sign in to comment.