feat(FastAPI): add templates/partials/widgets/pager.html

A pager that can be used for paginated result tables.

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-09-15 11:31:55 -07:00
parent 741cbfaa4e
commit 6298b1228a
No known key found for this signature in database
GPG key ID: F7E46DED420788F3
2 changed files with 76 additions and 0 deletions

50
aurweb/filters.py Normal file
View file

@ -0,0 +1,50 @@
from typing import Any, Dict
import paginate
from jinja2 import pass_context
from aurweb import util
from aurweb.templates import register_filter
@register_filter("pager_nav")
@pass_context
def pager_nav(context: Dict[str, Any],
page: int, total: int, prefix: str) -> str:
page = int(page) # Make sure this is an int.
pp = context.get("PP", 50)
# Setup a local query string dict, optionally passed by caller.
q = context.get("q", dict())
search_by = context.get("SeB", None)
if search_by:
q["SeB"] = search_by
sort_by = context.get("SB", None)
if sort_by:
q["SB"] = sort_by
def create_url(page: int):
nonlocal q
offset = max(page * pp - pp, 0)
qs = util.to_qs(util.extend_query(q, ["O", offset]))
return f"{prefix}?{qs}"
# Use the paginate module to produce our linkage.
pager = paginate.Page([], page=page + 1,
items_per_page=pp,
item_count=total,
url_maker=create_url)
return pager.pager(
link_attr={"class": "page"},
curpage_attr={"class": "page"},
separator="&nbsp",
format="$link_first $link_previous ~5~ $link_next $link_last",
symbol_first="« First",
symbol_previous=" Previous",
symbol_next="Next ",
symbol_last="Last »")

View file

@ -0,0 +1,26 @@
{# A pager widget that can be used for navigation of a number of results.
Inputs required:
prefix: Request URI prefix used to produce navigation offsets
singular: Singular sentence to be translated via tn
plural: Plural sentence to be translated via tn
PP: The number of results per page
O: The current offset value
total: The total number of results
#}
{% set page = ((O / PP) | int) %}
{% set pages = ((total / PP) | ceil) %}
<div class="pkglist-stats">
<p>
{{ total | tn(singular, plural) | format(total) }}
{{ "Page %d of %d." | tr | format(page + 1, pages) }}
</p>
{% if pages > 1 %}
<p class="pkglist-nav">
{{ page | pager_nav(total, prefix) | safe }}
<p>
{% endif %}
</div>