mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
Introduces `aurweb.defaults` and `aurweb.filters`. `aurweb.filters` is a location developers can put their additional Jinja2 filters and/or functions. We should slowly move all of our filters over here, where it makes sense. `aurweb.defaults` is a new module which hosts some default constants and utility functions, starting with offsets (O) and per page values (PP). As far as the new GET /requests is concerned, we match up here to PHP's implementation, with some minor improvements: Improvements: * PP on this page is now configurable: 50 (default), 100, or 250. * Example: `https://localhost:8444/requests?PP=250` Modifications: * The pagination is a bit different, but serves the exact same purpose. * "Last" no longer goes to an empty page. * Closes: https://gitlab.archlinux.org/archlinux/aurweb/-/issues/14 Signed-off-by: Kevin Morris <kevr@0cost.org>
60 lines
1.6 KiB
Python
60 lines
1.6 KiB
Python
from typing import Any, Dict
|
||
|
||
import paginate
|
||
|
||
from jinja2 import pass_context
|
||
|
||
from aurweb import config, util
|
||
from aurweb.templates import register_filter, register_function
|
||
|
||
|
||
@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=" ",
|
||
format="$link_first $link_previous ~5~ $link_next $link_last",
|
||
symbol_first="« First",
|
||
symbol_previous="‹ Previous",
|
||
symbol_next="Next ›",
|
||
symbol_last="Last »")
|
||
|
||
|
||
@register_function("config_getint")
|
||
def config_getint(section: str, key: str) -> int:
|
||
return config.getint(section, key)
|
||
|
||
|
||
@register_function("round")
|
||
def do_round(f: float) -> int:
|
||
return round(f)
|