-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dbd2c07
commit a9e1cde
Showing
2 changed files
with
15 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from lego.utils.pagination import CursorPagination | ||
|
||
|
||
class AchievementLeaderboardPagination(CursorPagination): | ||
page_size = 25 | ||
max_page_size = 25 | ||
ordering = "-achievements_score" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,28 @@ | ||
from rest_framework import mixins, permissions, viewsets | ||
from rest_framework.response import Response | ||
|
||
from lego.apps.achievements.pagination import AchievementLeaderboardPagination | ||
from lego.apps.users.models import User | ||
from lego.apps.users.serializers.users import PublicUserWithGroupsSerializer | ||
|
||
|
||
class LeaderBoardViewSet(mixins.ListModelMixin, viewsets.GenericViewSet): | ||
serializer_class = PublicUserWithGroupsSerializer | ||
permission_classes = [permissions.IsAuthenticated] | ||
pagination_class = AchievementLeaderboardPagination | ||
|
||
def get_queryset(self): | ||
return ( | ||
User.objects.filter(achievements__isnull=False) | ||
.order_by("-achievements_score") | ||
.distinct()[:50] | ||
.distinct() | ||
) | ||
|
||
def list(self, request, *args, **kwargs): | ||
queryset = self.get_queryset() | ||
page = self.paginate_queryset(queryset) | ||
if page is not None: | ||
serializer = self.get_serializer(page, many=True) | ||
return self.get_paginated_response(serializer.data) | ||
serializer = self.get_serializer(queryset, many=True) | ||
return Response(serializer.data) | ||
return Response(serializer.data) |