Adding pagination to scopes page

This commit is contained in:
Ian Roddis committed 2026-08-11 11:33:56 -03:00
1 parent 7a15d9f7c0
commit 6d7c5c5604
6 files changed
+160 -51

No files matched your search

+42 -6
View File
@@ -19,10 +19,15 @@ from app.crud import sub_part_type as sub_part_type_crud
from app.models.user import User
from app.services import scope_guide as scope_guide_service
from app.web.admin import web_require
from app.web.pagination import paginate
from app.web.templates import templates
router = APIRouter(include_in_schema=False)
_PAGE_SIZE = 50
_TABLE_TARGET = "#scopes-table-card"
_FILTER_PATH = "/scopes/filter"
def _to_int(value: str) -> int | None:
return int(value) if value.strip() else None
@@ -33,7 +38,11 @@ def _can_write_scopes(user: User) -> bool:
async def _build(db: AsyncSession, filters: dict) -> dict:
"""Run the guide query for the given raw (string) filter values."""
"""Run the guide query for the given raw (string) filter values.
Always returns the full matching row set — pagination is applied by the
caller by slicing ``rows`` (see ``_paginated_guide``).
"""
return await scope_guide_service.build(
db,
description=filters["description"],
@@ -44,6 +53,22 @@ async def _build(db: AsyncSession, filters: dict) -> dict:
)
async def _paginated_guide(
db: AsyncSession, filters: dict, page: int, page_size: int
) -> tuple[dict, dict]:
"""Run the guide query, then slice its rows to the requested page.
Returns ``(guide, pagination)`` where ``guide["rows"]`` is already sliced.
"""
guide = await _build(db, filters)
all_rows = guide["rows"]
pagination = paginate(
_FILTER_PATH, filters, page, page_size, len(all_rows), _TABLE_TARGET
)
guide["rows"] = all_rows[pagination["skip"] : pagination["skip"] + page_size]
return guide, pagination
async def _filter_options(db: AsyncSession) -> dict:
"""Dropdown option lists for the filter bar."""
scope_type = await part_type_crud.get_by_name(
@@ -79,7 +104,7 @@ async def scopes_page(
"sub_part_type_id": sub_part_type_id,
"fits_part_type_id": fits_part_type_id,
}
guide = await _build(db, filters)
guide, pagination = await _paginated_guide(db, filters, 1, _PAGE_SIZE)
return templates.TemplateResponse(
request,
"scopes.html",
@@ -87,6 +112,7 @@ async def scopes_page(
"user": user,
"filters": filters,
"can_write_scopes": _can_write_scopes(user),
"pagination": pagination,
**guide,
**(await _filter_options(db)),
},
@@ -101,6 +127,8 @@ async def scopes_filter(
scope_model_id: str = "",
sub_part_type_id: str = "",
fits_part_type_id: str = "",
page: int = 1,
page_size: int = _PAGE_SIZE,
db: AsyncSession = Depends(get_db),
user: User = Depends(web_require("augmented_parts:read", "fits:read")),
):
@@ -111,11 +139,15 @@ async def scopes_filter(
"sub_part_type_id": sub_part_type_id,
"fits_part_type_id": fits_part_type_id,
}
guide = await _build(db, filters)
guide, pagination = await _paginated_guide(db, filters, page, page_size)
return templates.TemplateResponse(
request,
"_scopes_table_card.html",
{**guide, "can_write_scopes": _can_write_scopes(user)},
{
**guide,
"pagination": pagination,
"can_write_scopes": _can_write_scopes(user),
},
)
@@ -154,9 +186,13 @@ async def bulk_delete_scopes(
"sub_part_type_id": sub_part_type_id,
"fits_part_type_id": fits_part_type_id,
}
guide = await _build(db, filters)
guide, pagination = await _paginated_guide(db, filters, 1, _PAGE_SIZE)
return templates.TemplateResponse(
request,
"_scopes_table_card.html",
{**guide, "can_write_scopes": _can_write_scopes(actor)},
{
**guide,
"pagination": pagination,
"can_write_scopes": _can_write_scopes(actor),
},
)