Use create_engine_url to add prefix to previous/next links (#553)

* Use create_engine_url to add prefix to previous/next links

* Remove override of get_paginated_response since it is unchanged from parent

* More concise override

* Make both overrides behave the same

* add test for public API alert groups pagination

Co-authored-by: Vadim Stepanov <vadimkerr@gmail.com>
This commit is contained in:
Michael Derynck 2022-09-23 03:45:28 -06:00 committed by GitHub
parent 4412ae52d2
commit accee4ebbe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 4 deletions

View file

@ -1,4 +1,5 @@
from unittest import mock
from unittest.mock import patch
import pytest
from django.urls import reverse
@ -186,6 +187,24 @@ def test_delete_incident_invalid_request(incident_public_api_setup):
assert response.status_code == status.HTTP_400_BAD_REQUEST
@pytest.mark.django_db
def test_pagination(settings, incident_public_api_setup):
settings.BASE_URL = "https://test.com/test/prefixed/urls"
token, incidents, _, _ = incident_public_api_setup
client = APIClient()
url = reverse("api-public:alert_groups-list")
with patch("common.api_helpers.paginators.PathPrefixedPagination.get_page_size", return_value=1):
response = client.get(url, HTTP_AUTHORIZATION=f"{token}")
assert response.status_code == status.HTTP_200_OK
result = response.json()
assert result["next"].startswith("https://test.com/test/prefixed/urls")
# This is test from old django-based tests
# TODO: uncomment with date checking in delete mode
# def test_delete_incident_invalid_date(self):

View file

@ -1,19 +1,33 @@
from rest_framework.pagination import CursorPagination, PageNumberPagination
from common.api_helpers.utils import create_engine_url
class HundredPageSizePaginator(PageNumberPagination):
class PathPrefixedPagination(PageNumberPagination):
def paginate_queryset(self, queryset, request, view=None):
request.build_absolute_uri = lambda: create_engine_url(request.get_full_path())
return super().paginate_queryset(queryset, request, view)
class PathPrefixedCursorPagination(CursorPagination):
def paginate_queryset(self, queryset, request, view=None):
request.build_absolute_uri = lambda: create_engine_url(request.get_full_path())
return super().paginate_queryset(queryset, request, view)
class HundredPageSizePaginator(PathPrefixedPagination):
page_size = 100
class FiftyPageSizePaginator(PageNumberPagination):
class FiftyPageSizePaginator(PathPrefixedPagination):
page_size = 50
class TwentyFivePageSizePaginator(PageNumberPagination):
class TwentyFivePageSizePaginator(PathPrefixedPagination):
page_size = 25
class TwentyFiveCursorPaginator(CursorPagination):
class TwentyFiveCursorPaginator(PathPrefixedCursorPagination):
page_size = 25
max_page_size = 100
page_size_query_param = "perpage"