SimpleAES / views /dashboard.py
SFM2001's picture
gix
75fb515
from flask import render_template, redirect, url_for, request, flash
from flask_login import login_required, current_user
from . import dashboard_bp
from database import History, db
from create_app import *
@dashboard_bp.route('/dashboard', defaults={'page': 1})
@dashboard_bp.route('/dashboard/page/<int:page>')
@login_required # Use Flask-Login's decorator
def dashboard(page):
# Get user ID from current_user (Flask-Login)
user_id = current_user.id
per_page = 10
pagination = History.query.filter_by(user_id=user_id).order_by(History.created_at.desc()).paginate(
page=page, per_page=per_page, error_out=False
)
histories = pagination.items
return render_template(
'dashboard.html',
histories=histories,
pagination=pagination,
scores_tr=[h.score_tr for h in histories],
scores_cc=[h.score_cc for h in histories],
scores_lr=[h.score_lr for h in histories],
scores_gra=[h.score_gra for h in histories],
creation_times=[h.created_at.strftime('%Y-%m-%d %H:%M:%S') for h in histories]
)
@dashboard_bp.route('/delete_histories', methods=['POST'])
def delete_histories():
history_ids = request.form.getlist('history_ids')
History.query.filter(History.id.in_(history_ids), History.user_id == current_user.id).delete(synchronize_session=False)
db.session.commit()
flash('Selected histories deleted.')
return redirect(url_for('dashboard.dashboard'))