From a776ac2383fe44cf03b0e0baf3c9d1ec0f1a53b7 Mon Sep 17 00:00:00 2001 From: Bryce Date: Fri, 7 Nov 2025 16:50:11 -0800 Subject: [PATCH] pagination --- app.py | 48 ++++++++++++++++++++----- templates/_pagination.html | 74 ++++++++++++++++++++++++++++++++++++++ templates/dashboard.html | 4 +++ 3 files changed, 117 insertions(+), 9 deletions(-) create mode 100644 templates/_pagination.html diff --git a/app.py b/app.py index 6977031..8b9a9ff 100644 --- a/app.py +++ b/app.py @@ -537,8 +537,9 @@ def fetch_collection(bearer: str, project_id_native: int, collection: str): @app.route("/dashboard") +@app.route("/dashboard/") @login_required -def dashboard(): +def dashboard(page=1): uid = session.get("uid") profile = get_user_profile(uid) if not profile.get("enabled"): @@ -547,18 +548,47 @@ def dashboard(): if not case_email: return redirect(url_for("welcome")) - # Read projects directly from Firestore - projects_ref = db.collection("projects") - docs = projects_ref.stream() - detailed_rows = [] + # Pagination settings + per_page = 25 + offset = (page - 1) * per_page + + # Get total count efficiently using a count aggregation query + try: + # Firestore doesn't have a direct count() method, so we need to count documents + import time + start_time = time.time() + projects_ref = db.collection("projects") + total_projects = projects_ref.count().get()[0][0].value + end_time = time.time() + print(f"Total projects count: {total_projects} (took {end_time - start_time:.2f}s)") + except Exception as e: + print(f"[WARN] Failed to get total count: {e}") + total_projects = 0 + + # Calculate pagination + total_pages = (total_projects + per_page - 1) // per_page # Ceiling division + + # Read only the current page from Firestore using limit() and offset() + import time + start_time = time.time() + projects_ref = db.collection("projects").order_by("matter_description").limit(per_page).offset(offset) + docs = projects_ref.stream() + paginated_rows = [] for doc in docs: - detailed_rows.append(doc.to_dict()) + paginated_rows.append(doc.to_dict()) - print(f"Retrieved {len(detailed_rows)} projects from Firestore") + end_time = time.time() + print(f"Retrieved {len(paginated_rows)} projects from Firestore (page {page} of {total_pages}) in {end_time - start_time:.2f}s") - # Render table - return render_template("dashboard.html", rows=detailed_rows, case_email=case_email) + # Render table with pagination data + return render_template("dashboard.html", + rows=paginated_rows, + case_email=case_email, + current_page=page, + total_pages=total_pages, + total_projects=total_projects, + per_page=per_page) # GAE compatibility diff --git a/templates/_pagination.html b/templates/_pagination.html new file mode 100644 index 0000000..231e1ea --- /dev/null +++ b/templates/_pagination.html @@ -0,0 +1,74 @@ +
+
+ Showing {{ ((current_page - 1) * per_page) + 1 }} to + {% if current_page * per_page < total_projects %} + {{ current_page * per_page }} + {% else %} + {{ total_projects }} + {% endif %} + of {{ total_projects }} projects +
+ +
+ + {% if current_page > 1 %} + + Previous + + {% else %} + + Previous + + {% endif %} + + + {% set start_page = [1, current_page - 2]|max %} + {% set end_page = [total_pages, current_page + 2]|min %} + + {% if start_page > 1 %} + + 1 + + {% if start_page > 2 %} + ... + {% endif %} + {% endif %} + + {% for page_num in range(start_page, end_page + 1) %} + {% if page_num == current_page %} + + {{ page_num }} + + {% else %} + + {{ page_num }} + + {% endif %} + {% endfor %} + + {% if end_page < total_pages %} + {% if end_page < total_pages - 1 %} + ... + {% endif %} + + {{ total_pages }} + + {% endif %} + + + {% if current_page < total_pages %} + + Next + + {% else %} + + Next + + {% endif %} +
+
\ No newline at end of file diff --git a/templates/dashboard.html b/templates/dashboard.html index bf68a87..6a960b6 100644 --- a/templates/dashboard.html +++ b/templates/dashboard.html @@ -300,6 +300,10 @@ + + +{% include '_pagination.html' %} +