Combine dashboard filter route into dashboard route
This commit is contained in:
@@ -42,14 +42,29 @@ def logout():
|
|||||||
return redirect(url_for('auth.login')) # Redirect to login page or home page
|
return redirect(url_for('auth.login')) # Redirect to login page or home page
|
||||||
|
|
||||||
|
|
||||||
@main.route('/')
|
@main.route('/', methods=['GET', 'POST'])
|
||||||
@login_required
|
@login_required
|
||||||
def dashboard():
|
def dashboard():
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
profile = current_user.profile
|
|
||||||
|
|
||||||
# Get all readings for the user
|
# Default to all readings
|
||||||
readings = Reading.query.filter_by(user_id=current_user.id).order_by(Reading.timestamp.asc()).all()
|
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)
|
# Weekly summary (last 7 days)
|
||||||
one_week_ago = datetime.now() - timedelta(days=7)
|
one_week_ago = datetime.now() - timedelta(days=7)
|
||||||
@@ -76,32 +91,20 @@ def dashboard():
|
|||||||
# Pass the delete form to the template
|
# Pass the delete form to the template
|
||||||
delete_form = DeleteForm()
|
delete_form = DeleteForm()
|
||||||
|
|
||||||
return render_template('dashboard.html', readings=readings, profile=profile, badges=badges,
|
return render_template(
|
||||||
systolic_avg=systolic_avg, diastolic_avg=diastolic_avg, heart_rate_avg=heart_rate_avg,
|
'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,
|
delete_form=delete_form,
|
||||||
timestamps=timestamps,
|
timestamps=timestamps,
|
||||||
systolic=systolic,
|
systolic=systolic,
|
||||||
diastolic=diastolic,
|
diastolic=diastolic,
|
||||||
heart_rate=heart_rate)
|
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)
|
|
||||||
|
|
||||||
@main.route('/add-reading', methods=['GET', 'POST'])
|
@main.route('/add-reading', methods=['GET', 'POST'])
|
||||||
@login_required
|
@login_required
|
||||||
|
|||||||
@@ -141,7 +141,7 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Collapsible Content -->
|
<!-- 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">
|
class="mt-4">
|
||||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
|
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||||
<div>
|
<div>
|
||||||
|
|||||||
Reference in New Issue
Block a user