Adding pagination to scopes page
This commit is contained in:
1 parent
7a15d9f7c0
commit
6d7c5c5604
6 files changed
+160
-51
No files matched your search
+7
-44
@@ -1,8 +1,5 @@
|
||||
"""HTMX-driven admin dashboard for users, roles, and thread standards."""
|
||||
|
||||
import math
|
||||
import urllib.parse
|
||||
|
||||
from fastapi import (
|
||||
APIRouter,
|
||||
Depends,
|
||||
@@ -17,9 +14,14 @@ from fastapi.responses import HTMLResponse, RedirectResponse, Response
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.core.database import AsyncSessionLocal, get_db
|
||||
from app.crud import attachment_point as attachment_point_crud
|
||||
from app.crud import attachment_point_name as apn_crud
|
||||
from app.crud import audit as audit_crud
|
||||
from app.crud import augmented_part as augmented_part_crud
|
||||
from app.crud import augmented_part_note as augmented_part_note_crud
|
||||
from app.crud import clamp_connection as clamp_connection_crud
|
||||
from app.crud import fit as fit_crud
|
||||
from app.crud import fit_note as fit_note_crud
|
||||
from app.crud import imported_part as imported_part_crud
|
||||
from app.crud import imported_part_config as imported_part_config_crud
|
||||
from app.crud import iso_thread as iso_thread_crud
|
||||
@@ -29,11 +31,6 @@ from app.crud import part_type as part_type_crud
|
||||
from app.crud import role as role_crud
|
||||
from app.crud import scope_info as scope_info_crud
|
||||
from app.crud import scope_model as scope_model_crud
|
||||
from app.crud import attachment_point as attachment_point_crud
|
||||
from app.crud import attachment_point_name as apn_crud
|
||||
from app.crud import clamp_connection as clamp_connection_crud
|
||||
from app.crud import fit as fit_crud
|
||||
from app.crud import fit_note as fit_note_crud
|
||||
from app.crud import static_connection as static_connection_crud
|
||||
from app.crud import sub_part_type as sub_part_type_crud
|
||||
from app.crud import unc_thread as unc_thread_crud
|
||||
@@ -48,6 +45,8 @@ from app.services import fit_matching as fit_matching_service
|
||||
from app.services import jobs
|
||||
from app.services import odata as odata_service
|
||||
from app.services import scope_import as scope_import_service
|
||||
from app.web.pagination import paginate as _paginate
|
||||
from app.web.pagination import query_url as _query_url
|
||||
from app.web.templates import templates
|
||||
|
||||
router = APIRouter(prefix="/admin", include_in_schema=False)
|
||||
@@ -55,42 +54,6 @@ router = APIRouter(prefix="/admin", include_in_schema=False)
|
||||
_PAGE_SIZE = 50
|
||||
|
||||
|
||||
def _query_url(base_path: str, params: dict) -> str:
|
||||
clean = {k: v for k, v in params.items() if v not in (None, "", 0)}
|
||||
return f"{base_path}?{urllib.parse.urlencode(clean)}"
|
||||
|
||||
|
||||
def _paginate(
|
||||
base_path: str,
|
||||
filter_params: dict,
|
||||
page: int,
|
||||
page_size: int,
|
||||
total: int,
|
||||
target_id: str,
|
||||
) -> dict:
|
||||
total_pages = max(1, math.ceil(total / page_size))
|
||||
page = max(1, min(page, total_pages))
|
||||
skip = (page - 1) * page_size
|
||||
|
||||
def _url(p: int) -> str:
|
||||
return _query_url(
|
||||
base_path, {**filter_params, "page": p, "page_size": page_size}
|
||||
)
|
||||
|
||||
return {
|
||||
"page": page,
|
||||
"page_size": page_size,
|
||||
"total": total,
|
||||
"total_pages": total_pages,
|
||||
"skip": skip,
|
||||
"showing_from": skip + 1 if total > 0 else 0,
|
||||
"showing_to": min(skip + page_size, total),
|
||||
"prev_url": _url(page - 1) if page > 1 else None,
|
||||
"next_url": _url(page + 1) if page < total_pages else None,
|
||||
"target_id": target_id,
|
||||
}
|
||||
|
||||
|
||||
def _sort_headers(
|
||||
base_path: str,
|
||||
filter_params: dict,
|
||||
|
||||
Reference in new issue
Block a user