aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps/api/pagination.py
blob: 2a325460d85fe924cd6bcd6d6792003b59e5ef17 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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)