Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: clamp page if it exceeds the maximum page #814

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions sqladmin/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,12 +442,13 @@ async def list(self, request: Request) -> Response:
pagination = await model_view.list(request)
pagination.add_pagination_urls(request.url)

if (
pagination.page * pagination.page_size
> pagination.count + pagination.page_size
):
raise HTTPException(
status_code=400, detail="Invalid page or pageSize parameter"
request_page = model_view.validate_page_number(
request.query_params.get("page"), 1
)

if request_page > pagination.page:
return RedirectResponse(
request.url.include_query_params(page=pagination.page), status_code=302
)

context = {"model_view": model_view, "pagination": pagination}
Expand Down
4 changes: 4 additions & 0 deletions sqladmin/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ def next_page(self) -> PageControl:

raise RuntimeError("Next page not found.")

def __post_init__(self) -> None:
# Clamp page
self.page = min(self.page, max(1, self.count // self.page_size + 1))

def resize(self, page_size: int) -> Pagination:
self.page = (self.page - 1) * self.page_size // page_size + 1
self.page_size = page_size
Expand Down
2 changes: 1 addition & 1 deletion tests/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class UserAdmin(ModelView, model=User):
client = TestClient(app)

response = client.get("/admin/user/list?page=10000")
assert response.status_code == 400
assert response.status_code == 200

response = client.get("/admin/user/list?page=aaaa")
assert response.status_code == 400
Loading