-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from cloudblue/feature/LITE-28409-Add-paginati…
…on-to-endpoints LITE-28409: Adding pagination capability
- Loading branch information
Showing
10 changed files
with
430 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from fastapi import Query | ||
from fastapi_pagination import LimitOffsetPage, LimitOffsetParams, set_page | ||
from fastapi_pagination.api import resolve_params | ||
from fastapi_pagination.ext.sqlalchemy import paginate | ||
|
||
|
||
set_page(LimitOffsetPage) | ||
|
||
|
||
class PaginationParams(LimitOffsetParams): | ||
"""Here we can redefine default size value""" | ||
limit: int = Query(1000, ge=1, le=1000, description="Page size") | ||
|
||
|
||
def apply_pagination(query, db, params, response): | ||
"""Apply pagination for the query | ||
* Paginate query according to applied parameters (size and page) | ||
* Add pagination headers to the response | ||
Don't filter or remove elements from query after the pagination | ||
""" | ||
paginated = paginate(db, query, params) | ||
resolve_params(params) | ||
init = paginated.offset | ||
# If we are selecting a page that it's out of range, we return the same start and end for on | ||
# header range, copying the same behavior as connect. | ||
end = init | ||
if paginated.items: | ||
end += len(paginated.items) - 1 | ||
|
||
response.headers['Content-Range'] = f'items {init}-{end}/{paginated.total}' | ||
return paginated.items |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.