diff --git a/hatenadir/ds/v2-xx/search.ugo.py b/hatenadir/ds/v2-xx/search.ugo.py new file mode 100644 index 0000000..949cca8 --- /dev/null +++ b/hatenadir/ds/v2-xx/search.ugo.py @@ -0,0 +1,41 @@ +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)