Prefill dashboard filter start/end date with first and last readings date

This commit is contained in:
Peter Stockings
2024-12-26 00:36:45 +11:00
parent 83c4ff7a7a
commit 923d996fe4
2 changed files with 23 additions and 2 deletions

View File

@@ -2,6 +2,7 @@ import csv
from io import StringIO
import io
from flask import Blueprint, Response, make_response, render_template, redirect, request, send_file, url_for, flash
from sqlalchemy import func
from werkzeug.security import generate_password_hash, check_password_hash
from werkzeug.http import http_date
from app.models import Profile, Reading, db, User
@@ -56,9 +57,29 @@ def landing():
@main.route('/dashboard', methods=['GET', 'POST'])
@login_required
def dashboard():
# Default values
# Initialize start_date and end_date
start_date = None
end_date = None
# Retrieve the first and last timestamps in a single query
first_last_readings = (
db.session.query(
func.min(Reading.timestamp).label('first'),
func.max(Reading.timestamp).label('last')
)
.filter(Reading.user_id == current_user.id)
.first()
)
# Extract the first and last timestamps
first_reading_timestamp = first_last_readings.first
last_reading_timestamp = first_last_readings.last
# Default to first and last reading dates if not provided
if not start_date and first_reading_timestamp:
start_date = first_reading_timestamp.strftime('%Y-%m-%d')
if not end_date and last_reading_timestamp:
end_date = last_reading_timestamp.strftime('%Y-%m-%d')
# Default to all readings
readings_query = Reading.query.filter_by(user_id=current_user.id)

View File

@@ -125,7 +125,7 @@
</div>
</div>
<div x-data="{ open: {{ 'true' if start_date or end_date else 'false' }} }"
<div x-data="{ open: {{ 'true' if request.method == 'POST' else 'false' }} }"
class="p-4 bg-white rounded-lg shadow-md">
<!-- Collapsible Header -->
<div class="flex justify-between items-center">