From 7d18320575d6813f20342a2fbbafc5bb4b42fb96 Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Tue, 24 Dec 2024 20:15:21 +1100 Subject: [PATCH] Combine dashboard filter route into dashboard route --- app/routes.py | 59 +++++++++++++++++++----------------- app/templates/dashboard.html | 2 +- 2 files changed, 32 insertions(+), 29 deletions(-) diff --git a/app/routes.py b/app/routes.py index ccd4496..78dc2d4 100644 --- a/app/routes.py +++ b/app/routes.py @@ -42,14 +42,29 @@ def logout(): return redirect(url_for('auth.login')) # Redirect to login page or home page -@main.route('/') +@main.route('/', methods=['GET', 'POST']) @login_required def dashboard(): from datetime import datetime, timedelta - profile = current_user.profile - # Get all readings for the user - readings = Reading.query.filter_by(user_id=current_user.id).order_by(Reading.timestamp.asc()).all() + # Default to all readings + readings_query = Reading.query.filter_by(user_id=current_user.id) + + # Handle filtering if it's a POST request + if request.method == 'POST': + start_date = request.form.get('start_date') + end_date = request.form.get('end_date') + + if start_date and end_date: + start_date = datetime.strptime(start_date, '%Y-%m-%d') + end_date = datetime.strptime(end_date, '%Y-%m-%d') + readings_query = readings_query.filter( + Reading.timestamp >= start_date, + Reading.timestamp <= end_date + ) + + # Fetch and order readings + readings = readings_query.order_by(Reading.timestamp.desc()).all() # Weekly summary (last 7 days) one_week_ago = datetime.now() - timedelta(days=7) @@ -66,7 +81,7 @@ def dashboard(): badges.append("100 Readings Milestone") if len(weekly_readings) >= 7: badges.append("Logged Readings for 7 Days") - + # Prepare data for the graphs timestamps = [reading.timestamp.strftime('%b %d') for reading in readings] systolic = [reading.systolic for reading in readings] @@ -76,32 +91,20 @@ def dashboard(): # Pass the delete form to the template delete_form = DeleteForm() - return render_template('dashboard.html', readings=readings, profile=profile, badges=badges, - systolic_avg=systolic_avg, diastolic_avg=diastolic_avg, heart_rate_avg=heart_rate_avg, - delete_form=delete_form, + return render_template( + 'dashboard.html', + readings=readings, + profile=current_user.profile, + badges=badges, + systolic_avg=systolic_avg, + diastolic_avg=diastolic_avg, + heart_rate_avg=heart_rate_avg, + delete_form=delete_form, timestamps=timestamps, systolic=systolic, diastolic=diastolic, - heart_rate=heart_rate) - -@main.route('/dashboard/filter', methods=['POST']) -@login_required -def filter_dashboard(): - from datetime import datetime - start_date = request.form.get('start_date') - end_date = request.form.get('end_date') - - # Parse dates and filter readings - readings = Reading.query.filter( - Reading.user_id == current_user.id, - Reading.timestamp >= datetime.strptime(start_date, '%Y-%m-%d'), - Reading.timestamp <= datetime.strptime(end_date, '%Y-%m-%d') - ).order_by(Reading.timestamp.asc()).all() - - # Pass the delete form to the template - delete_form = DeleteForm() - - return render_template('dashboard.html', readings=readings, delete_form=delete_form) + heart_rate=heart_rate + ) @main.route('/add-reading', methods=['GET', 'POST']) @login_required diff --git a/app/templates/dashboard.html b/app/templates/dashboard.html index 66d672e..3ac410f 100644 --- a/app/templates/dashboard.html +++ b/app/templates/dashboard.html @@ -141,7 +141,7 @@ -