Combine dashboard filter route into dashboard route

This commit is contained in:
Peter Stockings
2024-12-24 20:15:21 +11:00
parent 7626ec4c94
commit 7d18320575
2 changed files with 32 additions and 29 deletions

View File

@@ -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

View File

@@ -141,7 +141,7 @@
</div>
<!-- Collapsible Content -->
<form method="POST" action="{{ url_for('main.filter_dashboard') }}" x-show="open" x-transition.duration.300ms
<form method="POST" action="{{ url_for('main.dashboard') }}" x-show="open" x-transition.duration.300ms
class="mt-4">
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<div>