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
@@ -0,0 +1,40 @@
|
||||
"""Shared pagination helpers for HTMX table cards (admin screens, scopes page)."""
|
||||
|
||||
import math
|
||||
import urllib.parse
|
||||
|
||||
|
||||
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,
|
||||
}
|
||||
Reference in new issue
Block a user