From 59dc627e96a77cee6c0d039c14313234d324e63e Mon Sep 17 00:00:00 2001 From: Prometheus Date: Tue, 9 Jul 2024 14:05:55 +0100 Subject: [PATCH] Pagination limit and readme update (#40) * update readme with pagination query param * customize pagination --- README.md | 2 +- accounts/api.py | 3 ++- api/pagination.py | 6 ++++++ base/serializers.py | 2 +- base/settings.py | 3 +-- pots/serializers.py | 6 +++--- 6 files changed, 14 insertions(+), 8 deletions(-) create mode 100644 api/pagination.py diff --git a/README.md b/README.md index aeabe4f..7ab959d 100644 --- a/README.md +++ b/README.md @@ -108,7 +108,7 @@ Possible Error Codes: #### Pagination -Pagination available using `limit` and `offset` query params on endpoints that specify `paginated`. Default `limit` is 30. +Pagination available using `limit` and `page` as query param on endpoints that specify `paginated`. Default `limit` is 30. Endpoints that support pagination will return a success response containing the following: diff --git a/accounts/api.py b/accounts/api.py index 8d3b00b..3f2566a 100644 --- a/accounts/api.py +++ b/accounts/api.py @@ -41,6 +41,7 @@ AccountSerializer, PaginatedAccountsResponseSerializer, ) +from api.pagination import ResultPagination class DonorsAPI(APIView, PageNumberPagination): @@ -87,7 +88,7 @@ def get(self, request: Request, *args, **kwargs): return self.get_paginated_response(serializer.data) -class AccountsListAPI(APIView, PageNumberPagination): +class AccountsListAPI(APIView, ResultPagination): @extend_schema( responses={ diff --git a/api/pagination.py b/api/pagination.py new file mode 100644 index 0000000..21f4d65 --- /dev/null +++ b/api/pagination.py @@ -0,0 +1,6 @@ +from rest_framework.pagination import PageNumberPagination + +class ResultPagination(PageNumberPagination): + page_size = 30 + page_size_query_param = 'limit' + max_page_size = 200 diff --git a/base/serializers.py b/base/serializers.py index a47e77c..a27aa7e 100644 --- a/base/serializers.py +++ b/base/serializers.py @@ -1,7 +1,7 @@ from rest_framework import serializers -class TwoDecimalStringField(serializers.DecimalField): +class ResultPagination(serializers.DecimalField): def to_representation(self, value): if value is None: return value diff --git a/base/settings.py b/base/settings.py index 4c82261..b878872 100644 --- a/base/settings.py +++ b/base/settings.py @@ -107,7 +107,7 @@ DEFAULT_PAGE_SIZE = 30 REST_FRAMEWORK = { - "DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.PageNumberPagination", + "DEFAULT_PAGINATION_CLASS": "api.pagination.ResultPagination", "PAGE_SIZE": DEFAULT_PAGE_SIZE, "DEFAULT_THROTTLE_CLASSES": [ # "rest_framework.throttling.UserRateThrottle", @@ -166,7 +166,6 @@ "https://alpha.potlock.xyz", "https://alpha.potlock.app", # regex matching might not be advisable. "http://dev.local", - "https://dev.local", ] # REDIS / CACHE CONFIGS diff --git a/pots/serializers.py b/pots/serializers.py index b0c6581..901affd 100644 --- a/pots/serializers.py +++ b/pots/serializers.py @@ -2,15 +2,15 @@ from rest_framework.serializers import ModelSerializer, SerializerMethodField from accounts.serializers import SIMPLE_ACCOUNT_EXAMPLE, AccountSerializer -from base.serializers import TwoDecimalStringField +from base.serializers import ResultPagination from tokens.serializers import SIMPLE_TOKEN_EXAMPLE, TokenSerializer from .models import Pot, PotApplication, PotPayout class PotSerializer(ModelSerializer): - total_matching_pool_usd = TwoDecimalStringField(max_digits=20, decimal_places=2) - total_public_donations_usd = TwoDecimalStringField(max_digits=20, decimal_places=2) + total_matching_pool_usd = ResultPagination(max_digits=20, decimal_places=2) + total_public_donations_usd = ResultPagination(max_digits=20, decimal_places=2) class Meta: model = Pot