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:
@@ -39,6 +39,7 @@ DB_SQLITE3_PATH = Path(default("DB_SQLITE3_PATH", "bans.db"))
|
|||||||
|
|
||||||
HTTP_DOMAIN = required("HTTP_DOMAIN")
|
HTTP_DOMAIN = required("HTTP_DOMAIN")
|
||||||
HTTP_ROOT = default("HTTP_ROOT", r"")
|
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_HANDLER = one_of_default("STATIC_HANDLER", ("remote", "local"), "local")
|
||||||
STATIC_LOCAL_PATH = Path(default("STATIC_LOCAL_PATH", "static"))
|
STATIC_LOCAL_PATH = Path(default("STATIC_LOCAL_PATH", "static"))
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ def search_db(
|
|||||||
time_after: int = 0,
|
time_after: int = 0,
|
||||||
md5: str = "",
|
md5: str = "",
|
||||||
limit: int = 100,
|
limit: int = 100,
|
||||||
page: int = 0,
|
offset: int = 0,
|
||||||
):
|
):
|
||||||
db = get_db()
|
db = get_db()
|
||||||
result = db.execute(
|
result = db.execute(
|
||||||
@@ -99,7 +99,7 @@ def search_db(
|
|||||||
"md5": md5,
|
"md5": md5,
|
||||||
"md5_like": f"%{md5}%",
|
"md5_like": f"%{md5}%",
|
||||||
"limit": limit,
|
"limit": limit,
|
||||||
"offset": limit * page,
|
"offset": offset,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
import functools
|
import functools
|
||||||
import json
|
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 aiohttp import web
|
||||||
from jinja2 import Environment, PackageLoader, select_autoescape
|
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
|
# 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"
|
assert url == "" or url[0] == "/", "URL must be blank or start with a slash"
|
||||||
if url in ("", "/"):
|
if url in ("", "/"):
|
||||||
return config.HTTP_ROOT
|
base = config.HTTP_ROOT
|
||||||
else:
|
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):
|
def static_url(resource: str):
|
||||||
@@ -105,6 +113,8 @@ class IndexView(TemplateView):
|
|||||||
query = self.get_search_query()
|
query = self.get_search_query()
|
||||||
ctx["posts"] = search_db(**query)
|
ctx["posts"] = search_db(**query)
|
||||||
ctx["query"] = 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
|
return ctx
|
||||||
|
|
||||||
# Query parameters:
|
# Query parameters:
|
||||||
@@ -117,7 +127,7 @@ class IndexView(TemplateView):
|
|||||||
# &time_before=123456
|
# &time_before=123456
|
||||||
# &time_after=123456
|
# &time_after=123456
|
||||||
# &md5=md5_sum
|
# &md5=md5_sum
|
||||||
# &page=0
|
# &offset=0
|
||||||
|
|
||||||
def get_search_query(self):
|
def get_search_query(self):
|
||||||
allowed_keys = (
|
allowed_keys = (
|
||||||
@@ -130,13 +140,21 @@ class IndexView(TemplateView):
|
|||||||
"time_before",
|
"time_before",
|
||||||
"time_after",
|
"time_after",
|
||||||
"md5",
|
"md5",
|
||||||
"page",
|
"offset",
|
||||||
)
|
)
|
||||||
query = {
|
query = {
|
||||||
key: value
|
key: value
|
||||||
for key, value in self.request.query.items()
|
for key, value in self.request.query.items()
|
||||||
if key in allowed_keys and key in self.request.query
|
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:
|
if "time_before" in query:
|
||||||
try:
|
try:
|
||||||
query["time_before"] = int(query["time_before"])
|
query["time_before"] = int(query["time_before"])
|
||||||
|
|||||||
@@ -15,6 +15,14 @@
|
|||||||
<input type="submit" value="Search" />
|
<input type="submit" value="Search" />
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</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">
|
<table id="bans-table">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
@@ -66,4 +74,11 @@
|
|||||||
{% endfor %}
|
{% endfor %}
|
||||||
</table>
|
</table>
|
||||||
{# pagination #}
|
{# 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 %}
|
{% endblock main %}
|
||||||
|
|||||||
@@ -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.
|
# If you host other things on your domain, you may want to update this to be "/bans" or something.
|
||||||
#HTTP_ROOT=/
|
#HTTP_ROOT=/
|
||||||
|
|
||||||
|
# The number of results to show per page. Defaults to "100"
|
||||||
|
#HTTP_RESULTS_PER_PAGE=100
|
||||||
|
|
||||||
# Static files handler. Defaults to "local"
|
# Static files handler. Defaults to "local"
|
||||||
# Valid values are:
|
# Valid values are:
|
||||||
# * local
|
# * local
|
||||||
|
|||||||
Reference in New Issue
Block a user