pagination
This commit is contained in:
48
app.py
48
app.py
@@ -537,8 +537,9 @@ def fetch_collection(bearer: str, project_id_native: int, collection: str):
|
||||
|
||||
|
||||
@app.route("/dashboard")
|
||||
@app.route("/dashboard/<int:page>")
|
||||
@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
|
||||
|
||||
Reference in New Issue
Block a user