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

feat: Addition of pagination proxy and Flag functionality (#4) #5

Merged
merged 3 commits into from
Oct 24, 2024
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
8 changes: 7 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@
"**/db.sqlite3": true,
"**/.DS_Store": true,
"**/*.pyc": true,
"**/__pycache__/": true
"**/__pycache__/": true,
"**/build": true,
"**/dist": true,
"**/*.egg-info": true,
},
"search.exclude": {
"**/.git": true,
"**/build": true,
"**/*.egg-info": true,
"**/dist": true,
"**/.venv": true,
"**/tmp": true,
"htmlcov/*": true,
Expand Down
57 changes: 55 additions & 2 deletions creyPY/fastapi/pagination.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
from math import ceil
from typing import Any, Generic, Optional, Self, Sequence, TypeVar
from typing import Any, Generic, Optional, Self, Sequence, TypeVar, Union

from fastapi_pagination import Params
from fastapi_pagination.bases import AbstractPage, AbstractParams
from fastapi_pagination.types import GreaterEqualOne, GreaterEqualZero
from fastapi_pagination.types import (
GreaterEqualOne,
GreaterEqualZero,
AdditionalData,
SyncItemsTransformer,
)
from fastapi_pagination.api import create_page, apply_items_transformer
from fastapi_pagination.utils import verify_params
from fastapi_pagination.ext.sqlalchemy import create_paginate_query
from pydantic.json_schema import SkipJsonSchema
from sqlalchemy.sql.selectable import Select
from sqlalchemy.orm.session import Session
from sqlalchemy import select, func

T = TypeVar("T")

Expand Down Expand Up @@ -70,3 +81,45 @@ def parse_page(response, page: int, size: int) -> Page:
has_next=response.has_next,
has_prev=response.has_prev,
)


def create_count_query(query: Select) -> Select:
return select(func.count()).select_from(query.subquery())


def unwrap_scalars(
items: Sequence[Sequence[T]],
force_unwrap: bool = True,
) -> Union[Sequence[T], Sequence[Sequence[T]]]:
return [item[0] if force_unwrap else item for item in items]


def paginate(
connection: Session,
query: Select,
paginationFlag: bool = True,
params: Optional[AbstractParams] = None,
transformer: Optional[SyncItemsTransformer] = None,
additional_data: Optional[AdditionalData] = None,
):

params, raw_params = verify_params(params, "limit-offset", "cursor")

count_query = create_count_query(query)
total = connection.scalar(count_query)

if paginationFlag is False:
params = Params(page=1, size=total)

query = create_paginate_query(query, params)
items = connection.execute(query).all()

items = unwrap_scalars(items)
t_items = apply_items_transformer(items, transformer)

return create_page(
t_items,
params=params,
total=total,
**(additional_data or {}),
)