From a62f913a86288999abbf3c68f25b42e8d235d384 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jochen=20Wersd=C3=B6rfer?= Date: Wed, 2 Aug 2023 17:11:08 +0200 Subject: [PATCH] #98 fixed the "land on last page by default" bug + test --- cast/views/audio.py | 18 +----------------- cast/views/video.py | 16 +--------------- cast/views/wagtail_pagination.py | 23 +++++++++++++++++++++++ tests/pagination_test.py | 14 ++++++++++++++ 4 files changed, 39 insertions(+), 32 deletions(-) create mode 100644 cast/views/wagtail_pagination.py diff --git a/cast/views/audio.py b/cast/views/audio.py index 06a2d5e8..91a001e8 100644 --- a/cast/views/audio.py +++ b/cast/views/audio.py @@ -1,7 +1,5 @@ from typing import Any -from django.core.paginator import Page, Paginator -from django.db.models import QuerySet from django.http import HttpRequest, HttpResponse from django.shortcuts import get_object_or_404, redirect, render from django.urls import reverse @@ -16,21 +14,7 @@ from ..forms import AudioForm, NonEmptySearchForm from ..models import Audio from . import AuthenticatedHttpRequest - -DEFAULT_PAGE_KEY = "p" - -pagination_template = "wagtailadmin/shared/ajax_pagination_nav.html" - - -def paginate( - request: HttpRequest, - items: QuerySet[Audio], - page_key: str = DEFAULT_PAGE_KEY, - per_page: int = MENU_ITEM_PAGINATION, -) -> tuple[Paginator, Page]: - paginator: Paginator = Paginator(items, per_page) - page = paginator.get_page(request.GET.get(page_key)) - return paginator, page +from .wagtail_pagination import paginate, pagination_template @vary_on_headers("X-Requested-With") diff --git a/cast/views/video.py b/cast/views/video.py index 10adf2c8..c0709be3 100644 --- a/cast/views/video.py +++ b/cast/views/video.py @@ -1,7 +1,5 @@ from typing import Any -from django.core.paginator import Page, Paginator -from django.db.models import QuerySet from django.http import HttpRequest, HttpResponse from django.shortcuts import get_object_or_404, redirect, render from django.urls import reverse @@ -16,19 +14,7 @@ from ..forms import NonEmptySearchForm, get_video_form from ..models import Video from . import AuthenticatedHttpRequest - -DEFAULT_PAGE_KEY = "p" - - -pagination_template = "wagtailadmin/shared/ajax_pagination_nav.html" - - -def paginate( - request: HttpRequest, items: QuerySet[Video], page_key: str = DEFAULT_PAGE_KEY, per_page: int = 20 -) -> tuple[Paginator, Page]: - paginator = Paginator(items, per_page) - page = paginator.get_page(request.GET.get(page_key, 0)) - return paginator, page +from .wagtail_pagination import paginate, pagination_template @vary_on_headers("X-Requested-With") diff --git a/cast/views/wagtail_pagination.py b/cast/views/wagtail_pagination.py new file mode 100644 index 00000000..cfda79c3 --- /dev/null +++ b/cast/views/wagtail_pagination.py @@ -0,0 +1,23 @@ +from typing import Union + +from django.core.paginator import Page, Paginator +from django.db.models import QuerySet +from django.http import HttpRequest + +from ..appsettings import MENU_ITEM_PAGINATION +from ..models import Audio, Video + +DEFAULT_PAGE_KEY = "p" + +pagination_template = "wagtailadmin/shared/ajax_pagination_nav.html" + + +def paginate( + request: HttpRequest, + items: Union[QuerySet[Audio], QuerySet[Video]], + page_key: str = DEFAULT_PAGE_KEY, + per_page: int = MENU_ITEM_PAGINATION, +) -> tuple[Paginator, Page]: + paginator: Paginator = Paginator(items, per_page) + page = paginator.get_page(request.GET.get(page_key)) + return paginator, page diff --git a/tests/pagination_test.py b/tests/pagination_test.py index 209ba70f..2f5a81f5 100644 --- a/tests/pagination_test.py +++ b/tests/pagination_test.py @@ -4,6 +4,7 @@ from django.shortcuts import render from cast.models import Blog +from cast.views.wagtail_pagination import paginate def test_pagination_template_is_not_paginated(simple_request): @@ -50,3 +51,16 @@ def test_pagination_template_is_paginated_long(simple_request): ) def test_get_other_get_params(query_string, expected_other_get_params): assert Blog.get_other_get_params(QueryDict(query_string)) == expected_other_get_params + + +def test_wagtail_admin_pagination_starts_with_first_page(simple_request): + """ + Test that the pagination starts with the first page. There was a bug + when the page was fetched like this: + page = paginator.get_page(request.GET.get(page_key, 0)) + + This would return the last page if there was more than one page. Make + sure this does not happen again. + """ + paginator, page = paginate(simple_request, range(100)) # noqa + assert page.number == 1