Adding pervasive column sorting

This commit is contained in:
Ian Roddis committed 2026-07-31 09:52:09 -03:00
1 parent 34679d53e8
commit 2fb662080b
27 files changed
+614 -111

No files matched your search

+204 -63
View File
@@ -53,6 +53,11 @@ 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,
@@ -66,10 +71,9 @@ def _paginate(
skip = (page - 1) * page_size
def _url(p: int) -> str:
params = {k: v for k, v in filter_params.items() if v not in (None, "", 0)}
params["page"] = p
params["page_size"] = page_size
return f"{base_path}?{urllib.parse.urlencode(params)}"
return _query_url(
base_path, {**filter_params, "page": p, "page_size": page_size}
)
return {
"page": page,
@@ -85,6 +89,33 @@ def _paginate(
}
def _sort_headers(
base_path: str,
filter_params: dict,
columns: list[str],
sort: str,
direction: str,
target_id: str,
) -> dict:
"""Per-column header link context for click-to-sort table headers.
``filter_params`` should NOT include "sort"/"dir"/"page" — those are added here.
"""
headers = {}
for key in columns:
next_dir = "desc" if sort == key and direction == "asc" else "asc"
headers[key] = {
"url": _query_url(
base_path,
{**filter_params, "sort": key, "dir": next_dir, "page": 1},
),
"active": sort == key,
"dir": direction if sort == key else None,
"target_id": target_id,
}
return headers
def web_require(*perms: str):
"""Web dependency: allow superusers or holders of all given permissions."""
@@ -1206,10 +1237,50 @@ async def delete_sub_part_type(
# --- Imported Parts ----------------------------------------------------------
_IMPORTED_PARTS_SORT_COLUMNS = [
"InventoryID",
"Description",
"TBXColour",
"ProductMaxThreadDepthmm",
"ProductMaxThreadDepthin",
"ProductODmm",
"ProductODin",
"TBXThreadAttribute",
]
async def _default_imported_parts_table_ctx(db: AsyncSession) -> dict:
"""Table-card context (default sort, no filter, page 1) after a refresh."""
total = await imported_part_crud.count(db)
pagination = _paginate(
"/admin/imported-parts/filter",
{},
1,
_PAGE_SIZE,
total,
"#imported-parts-table-card",
)
return {
"parts": await imported_part_crud.list_filtered(db, limit=_PAGE_SIZE),
"pagination": pagination,
"sort_headers": _sort_headers(
"/admin/imported-parts/filter",
{},
_IMPORTED_PARTS_SORT_COLUMNS,
"InventoryID",
"asc",
"#imported-parts-table-card",
),
"sort": "InventoryID",
"dir": "asc",
}
@router.get("/imported-parts", response_class=HTMLResponse)
async def imported_parts_page(
request: Request,
sort: str = "InventoryID",
dir: str = "asc",
db: AsyncSession = Depends(get_db),
user: User = Depends(web_require("imported_parts:read")),
):
@@ -1220,19 +1291,37 @@ async def imported_parts_page(
"has_password": bool(config and config.password),
}
total = await imported_part_crud.count(db)
parts = await imported_part_crud.list_filtered(db, limit=_PAGE_SIZE)
parts = await imported_part_crud.list_filtered(
db, sort=sort, direction=dir, limit=_PAGE_SIZE
)
pagination = _paginate(
"/admin/imported-parts/filter",
{},
{"sort": sort, "dir": dir},
1,
_PAGE_SIZE,
total,
"#imported-parts-table-card",
)
sort_headers = _sort_headers(
"/admin/imported-parts/filter",
{},
_IMPORTED_PARTS_SORT_COLUMNS,
sort,
dir,
"#imported-parts-table-card",
)
return templates.TemplateResponse(
request,
"admin/imported_parts.html",
{"user": user, "config": config_view, "parts": parts, "pagination": pagination},
{
"user": user,
"config": config_view,
"parts": parts,
"pagination": pagination,
"sort_headers": sort_headers,
"sort": sort,
"dir": dir,
},
)
@@ -1274,19 +1363,8 @@ async def refresh_imported_parts(
config = await imported_part_config_crud.get(db)
async def _imported_table_ctx(sync_result=None, sync_error=None):
total = await imported_part_crud.count(db)
parts = await imported_part_crud.list_filtered(db, limit=_PAGE_SIZE)
pagination = _paginate(
"/admin/imported-parts/filter",
{},
1,
_PAGE_SIZE,
total,
"#imported-parts-table-card",
)
return {
"parts": parts,
"pagination": pagination,
**await _default_imported_parts_table_ctx(db),
"sync_result": sync_result,
"sync_error": sync_error,
}
@@ -1329,15 +1407,18 @@ async def refresh_imported_parts(
async def filter_imported_parts(
request: Request,
description: str = "",
sort: str = "InventoryID",
dir: str = "asc",
page: int = 1,
page_size: int = _PAGE_SIZE,
db: AsyncSession = Depends(get_db),
_: User = Depends(web_require("imported_parts:read")),
):
total = await imported_part_crud.count_filtered(db, description=description)
filter_params = {"description": description}
pagination = _paginate(
"/admin/imported-parts/filter",
{"description": description},
{**filter_params, "sort": sort, "dir": dir},
page,
page_size,
total,
@@ -1346,13 +1427,29 @@ async def filter_imported_parts(
parts = await imported_part_crud.list_filtered(
db,
description=description,
sort=sort,
direction=dir,
skip=pagination["skip"],
limit=page_size,
)
sort_headers = _sort_headers(
"/admin/imported-parts/filter",
filter_params,
_IMPORTED_PARTS_SORT_COLUMNS,
sort,
dir,
"#imported-parts-table-card",
)
return templates.TemplateResponse(
request,
"admin/_imported_parts_table_card.html",
{"parts": parts, "pagination": pagination},
{
"parts": parts,
"pagination": pagination,
"sort_headers": sort_headers,
"sort": sort,
"dir": dir,
},
)
@@ -1431,18 +1528,22 @@ async def _augmented_part_form_ctx(db: AsyncSession) -> dict:
}
@router.get("/augmented-parts", response_class=HTMLResponse)
async def augmented_parts_page(
request: Request,
db: AsyncSession = Depends(get_db),
user: User = Depends(web_require("augmented_parts:read")),
):
ctx = await _augmented_part_form_ctx(db)
_AUGMENTED_PARTS_SORT_COLUMNS = [
"id",
"sku",
"description",
"colour",
"manufacturer",
"scope_model",
"part_type",
"sub_part_type",
]
async def _default_augmented_parts_table_ctx(db: AsyncSession) -> dict:
"""Table-card context (default sort, no filters, page 1) after create/sync actions."""
total = await augmented_part_crud.count_filtered(db)
ctx["augmented_parts"] = await augmented_part_crud.list_filtered(
db, limit=_PAGE_SIZE
)
ctx["pagination"] = _paginate(
pagination = _paginate(
"/admin/augmented-parts/filter",
{},
1,
@@ -1450,6 +1551,55 @@ async def augmented_parts_page(
total,
"#augmented-parts-table-card",
)
return {
"augmented_parts": await augmented_part_crud.list_filtered(
db, limit=_PAGE_SIZE
),
"pagination": pagination,
"sort_headers": _sort_headers(
"/admin/augmented-parts/filter",
{},
_AUGMENTED_PARTS_SORT_COLUMNS,
"description",
"asc",
"#augmented-parts-table-card",
),
"sort": "description",
"dir": "asc",
}
@router.get("/augmented-parts", response_class=HTMLResponse)
async def augmented_parts_page(
request: Request,
sort: str = "description",
dir: str = "asc",
db: AsyncSession = Depends(get_db),
user: User = Depends(web_require("augmented_parts:read")),
):
ctx = await _augmented_part_form_ctx(db)
total = await augmented_part_crud.count_filtered(db)
ctx["augmented_parts"] = await augmented_part_crud.list_filtered(
db, sort=sort, direction=dir, limit=_PAGE_SIZE
)
ctx["pagination"] = _paginate(
"/admin/augmented-parts/filter",
{"sort": sort, "dir": dir},
1,
_PAGE_SIZE,
total,
"#augmented-parts-table-card",
)
ctx["sort_headers"] = _sort_headers(
"/admin/augmented-parts/filter",
{},
_AUGMENTED_PARTS_SORT_COLUMNS,
sort,
dir,
"#augmented-parts-table-card",
)
ctx["sort"] = sort
ctx["dir"] = dir
ctx["user"] = user
return templates.TemplateResponse(request, "admin/augmented_parts.html", ctx)
@@ -1488,25 +1638,10 @@ async def create_augmented_part(
),
)
form_ctx = await _augmented_part_form_ctx(db)
total = await augmented_part_crud.count_filtered(db)
pagination = _paginate(
"/admin/augmented-parts/filter",
{},
1,
_PAGE_SIZE,
total,
"#augmented-parts-table-card",
)
return templates.TemplateResponse(
request,
"admin/_augmented_parts_table.html",
{
**form_ctx,
"augmented_parts": await augmented_part_crud.list_filtered(
db, limit=_PAGE_SIZE
),
"pagination": pagination,
},
{**form_ctx, **await _default_augmented_parts_table_ctx(db)},
)
@@ -1543,24 +1678,12 @@ async def sync_augmented_parts(
sync_result = None
sync_error = f"Sync failed: {exc}"
form_ctx = await _augmented_part_form_ctx(db)
total = await augmented_part_crud.count_filtered(db)
pagination = _paginate(
"/admin/augmented-parts/filter",
{},
1,
_PAGE_SIZE,
total,
"#augmented-parts-table-card",
)
return templates.TemplateResponse(
request,
"admin/_augmented_parts_table.html",
{
**form_ctx,
"augmented_parts": await augmented_part_crud.list_filtered(
db, limit=_PAGE_SIZE
),
"pagination": pagination,
**await _default_augmented_parts_table_ctx(db),
"sync_result": sync_result,
"sync_error": sync_error,
},
@@ -1576,6 +1699,8 @@ async def filter_augmented_parts(
scope_model_id: str = "",
part_type_id: str = "",
sub_part_type_id: str = "",
sort: str = "description",
dir: str = "asc",
page: int = 1,
page_size: int = _PAGE_SIZE,
db: AsyncSession = Depends(get_db),
@@ -1605,7 +1730,7 @@ async def filter_augmented_parts(
}
pagination = _paginate(
"/admin/augmented-parts/filter",
filter_params,
{**filter_params, "sort": sort, "dir": dir},
page,
page_size,
total,
@@ -1619,13 +1744,29 @@ async def filter_augmented_parts(
scope_model_id=scope_model_id_int,
part_type_id=part_type_id_int,
sub_part_type_id=sub_part_type_id_int,
sort=sort,
direction=dir,
skip=pagination["skip"],
limit=page_size,
)
sort_headers = _sort_headers(
"/admin/augmented-parts/filter",
filter_params,
_AUGMENTED_PARTS_SORT_COLUMNS,
sort,
dir,
"#augmented-parts-table-card",
)
return templates.TemplateResponse(
request,
"admin/_augmented_parts_table_card.html",
{"augmented_parts": parts, "pagination": pagination},
{
"augmented_parts": parts,
"pagination": pagination,
"sort_headers": sort_headers,
"sort": sort,
"dir": dir,
},
)