diff --git a/app/routes.py b/app/routes.py index a9b4d98..a5204b0 100644 --- a/app/routes.py +++ b/app/routes.py @@ -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) diff --git a/app/templates/dashboard.html b/app/templates/dashboard.html index 2234459..ba0457d 100644 --- a/app/templates/dashboard.html +++ b/app/templates/dashboard.html @@ -125,7 +125,7 @@ -