From 2a68cc998962a49a6a675f44bdc27adb098f4f54 Mon Sep 17 00:00:00 2001 From: fadingNA Date: Thu, 7 Nov 2024 23:21:06 -0500 Subject: [PATCH] calculate totalPage, totalDoc(futreUse) --- application/api/user/routes.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/application/api/user/routes.py b/application/api/user/routes.py index 5295c2ee..e5ad68ae 100644 --- a/application/api/user/routes.py +++ b/application/api/user/routes.py @@ -2,6 +2,7 @@ import datetime import os import shutil import uuid +import math from bson.binary import Binary, UuidRepresentation from bson.dbref import DBRef @@ -435,8 +436,8 @@ class CombinedJson(Resource): user = "local" sort_field = request.args.get('sort', 'date') # Default to 'date' sort_order = request.args.get('order', "desc") # Default to 'desc' - page = request.args.get('page', 1) # Default to 1 - rows_per_page = request.args.get('rows', 10) # Default to 10 + page = int(request.args.get('page', 1)) # Default to 1 + rows_per_page = int(request.args.get('rows', 10)) # Default to 10 data = [ { "name": "default", @@ -487,15 +488,23 @@ class CombinedJson(Resource): "retriever": "brave_search", } ) + total_documents = len(data) + total_pages = max(1, math.ceil(total_documents / rows_per_page)) - first_index = (int(page) - 1) * int(rows_per_page) - last_index = first_index + int(rows_per_page) + first_index = (page - 1) * rows_per_page + last_index = first_index + rows_per_page paginated_docs = data[first_index:last_index] except Exception as err: return make_response(jsonify({"success": False, "error": str(err)}), 400) - - return make_response(jsonify(paginated_docs), 200) + + response = { + "paginated_docs": paginated_docs, + "totalDocuments": total_documents, + "totalPages": total_pages, + "documents":data + } + return make_response(jsonify(response), 200) @user_ns.route("/api/docs_check")