Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply LimitOffsetPagination in GET Infraction with default Page Size 100 #443

Merged
merged 13 commits into from
Apr 24, 2021
Merged
5 changes: 5 additions & 0 deletions pydis_site/apps/api/viewsets/bot/infraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
ExpandedInfractionSerializer,
InfractionSerializer
)
from pydis_site.apps.api.viewsets.bot.pagination import LimitSetPagination


class InfractionViewSet(
Expand All @@ -38,6 +39,8 @@ class InfractionViewSet(
- **active** `bool`: whether the infraction is still active
- **actor__id** `int`: snowflake of the user which applied the infraction
- **hidden** `bool`: whether the infraction is a shadow infraction
- **limit** `int`: default limit is 100
- **offset** `int`: default is 0
MarkKoz marked this conversation as resolved.
Show resolved Hide resolved
- **search** `str`: regular expression applied to the infraction's reason
- **type** `str`: the type of the infraction
- **user__id** `int`: snowflake of the user to which the infraction was applied
Expand All @@ -46,6 +49,7 @@ class InfractionViewSet(
Invalid query parameters are ignored.

#### Response format
- Response are paginated but only the actual data is returned
MarkKoz marked this conversation as resolved.
Show resolved Hide resolved
>>> [
... {
... 'id': 5,
Expand Down Expand Up @@ -133,6 +137,7 @@ class InfractionViewSet(

serializer_class = InfractionSerializer
queryset = Infraction.objects.all()
pagination_class = LimitSetPagination
filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter)
filter_fields = ('user__id', 'actor__id', 'active', 'hidden', 'type')
search_fields = ('$reason',)
Expand Down
12 changes: 12 additions & 0 deletions pydis_site/apps/api/viewsets/bot/pagination.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from rest_framework.pagination import LimitOffsetPagination
MarkKoz marked this conversation as resolved.
Show resolved Hide resolved
from rest_framework.response import Response


class LimitSetPagination(LimitOffsetPagination):
MarkKoz marked this conversation as resolved.
Show resolved Hide resolved
"""Extend LimitOffsetPagination."""
MarkKoz marked this conversation as resolved.
Show resolved Hide resolved

default_limit = 100

def get_paginated_response(self, data: list) -> Response:
MarkKoz marked this conversation as resolved.
Show resolved Hide resolved
"""Override default response."""
MarkKoz marked this conversation as resolved.
Show resolved Hide resolved
return Response(data)