Skip to content

Commit

Permalink
#98 fixed the "land on last page by default" bug + test
Browse files Browse the repository at this point in the history
  • Loading branch information
ephes committed Aug 2, 2023
1 parent 41413b2 commit a62f913
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 32 deletions.
18 changes: 1 addition & 17 deletions cast/views/audio.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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")
Expand Down
16 changes: 1 addition & 15 deletions cast/views/video.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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")
Expand Down
23 changes: 23 additions & 0 deletions cast/views/wagtail_pagination.py
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions tests/pagination_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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

0 comments on commit a62f913

Please sign in to comment.