diff options
| author | 2021-02-26 13:10:05 +0530 | |
|---|---|---|
| committer | 2021-02-26 13:10:05 +0530 | |
| commit | 4a5fa2d3c8e0aa2d71e13d15ca7b31ef46535001 (patch) | |
| tree | f3d38981884df9263dc82fb1ada4862f0a595a12 | |
| parent | Add custom paginator class to override default resonse (diff) | |
Update pagination docs and pagination class location
| -rw-r--r-- | pydis_site/apps/api/pagination.py | 49 | ||||
| -rw-r--r-- | pydis_site/apps/api/viewsets/bot/infraction.py | 11 | ||||
| -rw-r--r-- | pydis_site/apps/api/viewsets/bot/pagination.py | 12 | 
3 files changed, 55 insertions, 17 deletions
| diff --git a/pydis_site/apps/api/pagination.py b/pydis_site/apps/api/pagination.py new file mode 100644 index 00000000..bd8d963d --- /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": 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) diff --git a/pydis_site/apps/api/viewsets/bot/infraction.py b/pydis_site/apps/api/viewsets/bot/infraction.py index 2b0bbf3e..1bf89203 100644 --- a/pydis_site/apps/api/viewsets/bot/infraction.py +++ b/pydis_site/apps/api/viewsets/bot/infraction.py @@ -13,11 +13,11 @@ 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  ) -from pydis_site.apps.api.viewsets.bot.pagination import LimitSetPagination  class InfractionViewSet( @@ -39,8 +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 +    - **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 @@ -49,7 +49,8 @@ class InfractionViewSet(      Invalid query parameters are ignored.      #### Response format -    - Response are paginated but only the actual data is returned +    - Response are paginated but the result is returned without any pagination metadata. +      Below is the returned format.      >>> [      ...     {      ...         'id': 5, @@ -137,7 +138,7 @@ class InfractionViewSet(      serializer_class = InfractionSerializer      queryset = Infraction.objects.all() -    pagination_class = LimitSetPagination +    pagination_class = LimitOffsetPaginationExtended      filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter)      filter_fields = ('user__id', 'actor__id', 'active', 'hidden', 'type')      search_fields = ('$reason',) diff --git a/pydis_site/apps/api/viewsets/bot/pagination.py b/pydis_site/apps/api/viewsets/bot/pagination.py deleted file mode 100644 index f9a8ccae..00000000 --- a/pydis_site/apps/api/viewsets/bot/pagination.py +++ /dev/null @@ -1,12 +0,0 @@ -from rest_framework.pagination import LimitOffsetPagination -from rest_framework.response import Response - - -class LimitSetPagination(LimitOffsetPagination): -    """Extend LimitOffsetPagination.""" - -    default_limit = 100 - -    def get_paginated_response(self, data: list) -> Response: -        """Override default response.""" -        return Response(data) | 
