Files
rekushi 57f1343c7c feat(dsi): display paginated search results
Render Flipnote and creator results while preserving the query and scope between pages.
2026-10-29 04:42:51 +00:00

42 lines
2.1 KiB
Python

from twisted.web import resource
from DB import Database
from hatena import request_args
from reximemo import dsi_url, flipnote_buttons, make_menu, page_number, pagination_buttons
class PyResource(resource.Resource):
isLeaf = True
def render_GET(self, request):
args = request_args(request)
query = str((args.get("q") or [""])[0]).strip()[:100]
kind = "creators" if (args.get("kind") or [""])[0] == "creators" else "flipnotes"
start = [
{"label": "Search Flipnotes", "url": dsi_url(request, "search/flipnotes.kbd"), "icon": 112},
{"label": "Search Creators", "url": dsi_url(request, "search/creators.kbd"), "icon": 112},
]
if query:
total = Database.count_creators(query) if kind == "creators" else Database.count_flipnotes(q=query)
page = page_number(request, total, 25)
if kind == "creators":
rows = Database.search_creators(query, page=page, per_page=25)
results = [{"label": row["display_name"], "url": dsi_url(request, f"creator/{row['id']}/profile.htm"), "icon": 100}
for row in rows]
else:
results = flipnote_buttons(request, Database.list_flipnotes(q=query, page=page, per_page=25))
pager = pagination_buttons(request, "search.uls", page, total, 25, {"q": query, "kind": kind})
before = [button for button in pager if button["label"].startswith("Previous")]
after = [button for button in pager if button["label"].startswith("Next")]
buttons = start + before + results + after
subtitle = ("Creators: " if kind == "creators" else "Flipnotes: ") + query[:35]
if not total:
subtitle = "No matching " + kind + ". Try another search."
else:
total = 0
buttons = start
subtitle = "Choose what to search for."
request.setHeader(b"content-type", b"text/plain")
return make_menu(layout=(2, 1), title="Search", subtitle_top=subtitle,
left="Results", right=str(total), buttons=buttons)