Skip to content

Commit

Permalink
fix: clamp page if it exceeds the maximum page
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-lambdaloopers committed Sep 9, 2024
1 parent e5e7d50 commit b67054c
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
3 changes: 3 additions & 0 deletions sqladmin/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,9 @@ async def list(self, request: Request) -> Pagination:
else:
count = await self.count(request)

# Clamp page
page = min(page, max(1, count // page_size + 1))

stmt = stmt.limit(page_size).offset((page - 1) * page_size)
rows = await self._run_query(stmt)

Expand Down
3 changes: 3 additions & 0 deletions sqladmin/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ def resize(self, page_size: int) -> Pagination:
return self

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

# Previous pages
for p in range(self.page - min(self.max_page_controls, 3), self.page):
if p > 0:
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

0 comments on commit b67054c

Please sign in to comment.