aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Matteo Bertucci <[email protected]>2021-04-24 17:02:28 +0200
committerGravatar GitHub <[email protected]>2021-04-24 17:02:28 +0200
commit478dfb2dd0f00ecbecadffebd1600ca9ca53017a (patch)
tree098b7572e2b380b7f57ae7eedb180fa0b70edc8e
parentMerge pull request #481 from dawnofmidnight/patch-1 (diff)
parentMerge branch 'main' into limit-infraction-result (diff)
Merge pull request #443 from kunal097/limit-infraction-result
Apply LimitOffsetPagination in GET Infraction with default Page Size 100
-rw-r--r--pydis_site/apps/api/pagination.py49
-rw-r--r--pydis_site/apps/api/viewsets/bot/infraction.py5
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',)