pagination
This commit is contained in:
46
app.py
46
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")
|
||||
# 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()
|
||||
detailed_rows = []
|
||||
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
|
||||
|
||||
74
templates/_pagination.html
Normal file
74
templates/_pagination.html
Normal file
@@ -0,0 +1,74 @@
|
||||
<div class="flex justify-between items-center mt-4 px-2">
|
||||
<div class="text-sm text-gray-700">
|
||||
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
|
||||
</div>
|
||||
|
||||
<div class="flex space-x-2">
|
||||
<!-- Previous Button -->
|
||||
{% if current_page > 1 %}
|
||||
<a href="{{ url_for('dashboard', page=current_page - 1) }}"
|
||||
class="px-3 py-1 text-sm bg-gray-100 text-gray-700 rounded hover:bg-gray-200 transition-colors">
|
||||
Previous
|
||||
</a>
|
||||
{% else %}
|
||||
<span class="px-3 py-1 text-sm text-gray-400 bg-gray-50 rounded cursor-not-allowed">
|
||||
Previous
|
||||
</span>
|
||||
{% endif %}
|
||||
|
||||
<!-- Page Numbers -->
|
||||
{% set start_page = [1, current_page - 2]|max %}
|
||||
{% set end_page = [total_pages, current_page + 2]|min %}
|
||||
|
||||
{% if start_page > 1 %}
|
||||
<a href="{{ url_for('dashboard', page=1) }}"
|
||||
class="px-3 py-1 text-sm bg-gray-100 text-gray-700 rounded hover:bg-gray-200 transition-colors">
|
||||
1
|
||||
</a>
|
||||
{% if start_page > 2 %}
|
||||
<span class="px-3 py-1 text-sm text-gray-400">...</span>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
|
||||
{% for page_num in range(start_page, end_page + 1) %}
|
||||
{% if page_num == current_page %}
|
||||
<span class="px-3 py-1 text-sm bg-blue-600 text-white rounded">
|
||||
{{ page_num }}
|
||||
</span>
|
||||
{% else %}
|
||||
<a href="{{ url_for('dashboard', page=page_num) }}"
|
||||
class="px-3 py-1 text-sm bg-gray-100 text-gray-700 rounded hover:bg-gray-200 transition-colors">
|
||||
{{ page_num }}
|
||||
</a>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
|
||||
{% if end_page < total_pages %}
|
||||
{% if end_page < total_pages - 1 %}
|
||||
<span class="px-3 py-1 text-sm text-gray-400">...</span>
|
||||
{% endif %}
|
||||
<a href="{{ url_for('dashboard', page=total_pages) }}"
|
||||
class="px-3 py-1 text-sm bg-gray-100 text-gray-700 rounded hover:bg-gray-200 transition-colors">
|
||||
{{ total_pages }}
|
||||
</a>
|
||||
{% endif %}
|
||||
|
||||
<!-- Next Button -->
|
||||
{% if current_page < total_pages %}
|
||||
<a href="{{ url_for('dashboard', page=current_page + 1) }}"
|
||||
class="px-3 py-1 text-sm bg-gray-100 text-gray-700 rounded hover:bg-gray-200 transition-colors">
|
||||
Next
|
||||
</a>
|
||||
{% else %}
|
||||
<span class="px-3 py-1 text-sm text-gray-400 bg-gray-50 rounded cursor-not-allowed">
|
||||
Next
|
||||
</span>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
@@ -300,6 +300,10 @@
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<!-- Pagination -->
|
||||
{% include '_pagination.html' %}
|
||||
|
||||
<script>
|
||||
function columnConfig() {
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user