diff --git a/chanbans/config.py b/chanbans/config.py index 122f418..d202b11 100644 --- a/chanbans/config.py +++ b/chanbans/config.py @@ -39,6 +39,7 @@ DB_SQLITE3_PATH = Path(default("DB_SQLITE3_PATH", "bans.db")) HTTP_DOMAIN = required("HTTP_DOMAIN") HTTP_ROOT = default("HTTP_ROOT", r"") +HTTP_RESULTS_PER_PAGE = default("HTTP_RESULTS_PER_PAGE", 100) STATIC_HANDLER = one_of_default("STATIC_HANDLER", ("remote", "local"), "local") STATIC_LOCAL_PATH = Path(default("STATIC_LOCAL_PATH", "static")) diff --git a/chanbans/db.py b/chanbans/db.py index 900e5a6..c1206cf 100644 --- a/chanbans/db.py +++ b/chanbans/db.py @@ -61,7 +61,7 @@ def search_db( time_after: int = 0, md5: str = "", limit: int = 100, - page: int = 0, + offset: int = 0, ): db = get_db() result = db.execute( @@ -99,7 +99,7 @@ def search_db( "md5": md5, "md5_like": f"%{md5}%", "limit": limit, - "offset": limit * page, + "offset": offset, }, ) diff --git a/chanbans/http.py b/chanbans/http.py index ba8b235..5c674a6 100644 --- a/chanbans/http.py +++ b/chanbans/http.py @@ -1,7 +1,8 @@ from collections import defaultdict import functools import json -from typing import Any, MutableMapping, Optional, Sequence +from typing import Any, Mapping, MutableMapping, Optional, Sequence +import urllib.parse from aiohttp import web from jinja2 import Environment, PackageLoader, select_autoescape @@ -14,12 +15,19 @@ from .db import get_db, search_db # 2023-07-31 - In a meeting. Hope I don't get caught -def route_url(url: str): +def route_url(url: str, args: Optional[Mapping[str, Any]] = None): + if args is None: + args = dict() assert url == "" or url[0] == "/", "URL must be blank or start with a slash" if url in ("", "/"): - return config.HTTP_ROOT + base = config.HTTP_ROOT else: - return f"{config.HTTP_ROOT}{url}" + base = f"{config.HTTP_ROOT}{url}" + + if args: + return base + "?" + urllib.parse.urlencode(args) + else: + return base def static_url(resource: str): @@ -105,6 +113,8 @@ class IndexView(TemplateView): query = self.get_search_query() ctx["posts"] = search_db(**query) ctx["query"] = query + ctx["next_page"] = route_url("/", {**query, "offset": query["offset"] + 100}) + ctx["prev_page"] = route_url("/", {**query, "offset": query["offset"] - 100}) return ctx # Query parameters: @@ -117,7 +127,7 @@ class IndexView(TemplateView): # &time_before=123456 # &time_after=123456 # &md5=md5_sum - # &page=0 + # &offset=0 def get_search_query(self): allowed_keys = ( @@ -130,13 +140,21 @@ class IndexView(TemplateView): "time_before", "time_after", "md5", - "page", + "offset", ) query = { key: value for key, value in self.request.query.items() if key in allowed_keys and key in self.request.query } + if "offset" in query: + try: + query["offset"] = int(query["offset"]) + except ValueError: + query["offset"] = 0 + else: + query["offset"] = 0 + if "time_before" in query: try: query["time_before"] = int(query["time_before"]) diff --git a/chanbans/templates/index.html b/chanbans/templates/index.html index 44b26a4..f946d60 100644 --- a/chanbans/templates/index.html +++ b/chanbans/templates/index.html @@ -15,6 +15,14 @@ + {# pagination #} + {% if query['offset'] != 0 %} + Prev + {% endif %} + | + {% if posts|length == config.HTTP_RESULTS_PER_PAGE %} + Next + {% endif %}