Skip to content

Commit

Permalink
Merge eae89ec into f17075f
Browse files Browse the repository at this point in the history
  • Loading branch information
kunal097 authored Mar 7, 2021
2 parents f17075f + eae89ec commit 66224d9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
49 changes: 49 additions & 0 deletions pydis_site/apps/api/pagination.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import typing

from rest_framework.pagination import LimitOffsetPagination
from rest_framework.response import Response


class LimitOffsetPaginationExtended(LimitOffsetPagination):
"""
Extend LimitOffsetPagination to customise the default response.
For example:
## Default response
{
"count": 1,
"next": null,
"previous": null,
"results": [{
"id": 6,
"inserted_at": "2021-01-26T21:13:35.477879Z",
"expires_at": null,
"active": false,
"user": 1,
"actor": 2,
"type": "warning",
"reason": null,
"hidden": false
}]
}
## Required response
[{
"id": 6,
"inserted_at": "2021-01-26T21:13:35.477879Z",
"expires_at": null,
"active": false,
"user": 1,
"actor": 2,
"type": "warning",
"reason": null,
"hidden": false
}]
"""

default_limit = 100

def get_paginated_response(self, data: typing.Any) -> Response:
"""Override to skip metadata i.e. `count`, `next`, and `previous`."""
return Response(data)
6 changes: 6 additions & 0 deletions pydis_site/apps/api/viewsets/bot/infraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from rest_framework.viewsets import GenericViewSet

from pydis_site.apps.api.models.bot.infraction import Infraction
from pydis_site.apps.api.pagination import LimitOffsetPaginationExtended
from pydis_site.apps.api.serializers import (
ExpandedInfractionSerializer,
InfractionSerializer
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`: number of results return per page. default limit is 100
- **offset** `int`: the initial index from which to return the results, default is 0
- **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,8 @@ class InfractionViewSet(
Invalid query parameters are ignored.
#### Response format
- Response are paginated but the result is returned without any pagination metadata.
Below is the returned format.
>>> [
... {
... 'id': 5,
Expand Down Expand Up @@ -133,6 +138,7 @@ class InfractionViewSet(

serializer_class = InfractionSerializer
queryset = Infraction.objects.all()
pagination_class = LimitOffsetPaginationExtended
filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter)
filter_fields = ('user__id', 'actor__id', 'active', 'hidden', 'type')
search_fields = ('$reason',)
Expand Down

0 comments on commit 66224d9

Please sign in to comment.