diff options
| -rw-r--r-- | pydis_site/apps/api/pagination.py | 49 | ||||
| -rw-r--r-- | pydis_site/apps/api/viewsets/bot/infraction.py | 5 | 
2 files changed, 54 insertions, 0 deletions
| diff --git a/pydis_site/apps/api/pagination.py b/pydis_site/apps/api/pagination.py new file mode 100644 index 00000000..2a325460 --- /dev/null +++ b/pydis_site/apps/api/pagination.py @@ -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": None, +    ...     "previous": None, +    ...     "results": [{ +    ...         "id": 6, +    ...         "inserted_at": "2021-01-26T21:13:35.477879Z", +    ...         "expires_at": None, +    ...         "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": None, +    ...     "active": False, +    ...     "user": 1, +    ...     "actor": 2, +    ...     "type": "warning", +    ...     "reason": None, +    ...     "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) diff --git a/pydis_site/apps/api/viewsets/bot/infraction.py b/pydis_site/apps/api/viewsets/bot/infraction.py index 423e806e..bd512ddd 100644 --- a/pydis_site/apps/api/viewsets/bot/infraction.py +++ b/pydis_site/apps/api/viewsets/bot/infraction.py @@ -13,6 +13,7 @@ from rest_framework.response import Response  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 @@ -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 100) +    - **offset** `int`: the initial index from which to return the results (default 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 @@ -46,6 +49,7 @@ class InfractionViewSet(      Invalid query parameters are ignored.      #### Response format +    Response is paginated but the result is returned without any pagination metadata.      >>> [      ...     {      ...         'id': 5, @@ -133,6 +137,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',) | 
