Add "pagination"

Items per page are just handled by hard offset instead of pages. That's
all we really need

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2023-08-06 00:17:45 -07:00
parent 3f5bb7959a
commit 4439e14b6b
5 changed files with 45 additions and 8 deletions

View File

@@ -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"))

View File

@@ -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,
},
)

View File

@@ -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"])

View File

@@ -15,6 +15,14 @@
<input type="submit" value="Search" />
</form>
</div>
{# pagination #}
{% if query['offset'] != 0 %}
<a href="{{prev_page}}">Prev</a>
{% endif %}
|
{% if posts|length == config.HTTP_RESULTS_PER_PAGE %}
<a href="{{next_page}}">Next</a>
{% endif %}
<table id="bans-table">
<thead>
<tr>
@@ -66,4 +74,11 @@
{% endfor %}
</table>
{# pagination #}
{% if query['offset'] != 0 %}
<a href="{{prev_page}}">Prev</a>
{% endif %}
|
{% if posts|length == config.HTTP_RESULTS_PER_PAGE %}
<a href="{{next_page}}">Next</a>
{% endif %}
{% endblock main %}

View File

@@ -14,6 +14,9 @@ HTTP_DOMAIN=domain.com
# If you host other things on your domain, you may want to update this to be "/bans" or something.
#HTTP_ROOT=/
# The number of results to show per page. Defaults to "100"
#HTTP_RESULTS_PER_PAGE=100
# Static files handler. Defaults to "local"
# Valid values are:
# * local