From fd69f9bc98e730664c4a1c64a095e08c83acf1a3 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Wed, 11 Jun 2025 15:32:35 -0500 Subject: Limit returned results Limit the number of results returned to make page more responsive. This is buggy at this point — it doesn't do exactly what I expect, e.g. when I attempt to scroll, or use pagination, it doesn't actually display all the pages as expected. --- uploader/publications/views.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'uploader/publications/views.py') diff --git a/uploader/publications/views.py b/uploader/publications/views.py index 63acf1b..8a65ff2 100644 --- a/uploader/publications/views.py +++ b/uploader/publications/views.py @@ -38,6 +38,10 @@ def index(): def list_publications(): # request breakdown: # https://datatables.net/manual/server-side + _page = int(request.args.get("draw")) + _length = int(request.args.get("length") or '-1') + _start = int(request.args.get("start") or '0') + _search = request.args["search[value]"] with (database_connection(app.config["SQL_URI"]) as conn, conn.cursor(cursorclass=DictCursor) as cursor): cursor.execute("SELECT COUNT(*) FROM Publication") @@ -46,12 +50,17 @@ def list_publications(): **row, "index": idx } for idx,row in enumerate( fetch_publications( - conn, request.args["search[value]"]), start=1)) + conn, + _search, + offset=_length * (_page-1), + limit=_length), + start=(_start + 1))) return json.dumps({ - "draw": request.args.get("draw"), + "draw": _page, "recordsTotal": _totalrows, - "recordsFiltered": len(_publications), + "recordsFiltered": ( + len(_publications) if bool(_search) else _totalrows), "publications": _publications, "status": "success" }) -- cgit v1.2.3