135 lines
5.7 KiB
Python
135 lines
5.7 KiB
Python
from flask import Blueprint, render_template, redirect, url_for, flash, request, jsonify
|
|
from flask_login import login_required, current_user
|
|
from extensions import db
|
|
from models import User, Tryout, Evaluation, TryoutRegistration
|
|
from sqlalchemy import func
|
|
|
|
evaluations_bp = Blueprint('evaluations', __name__, url_prefix='/evaluations')
|
|
|
|
@evaluations_bp.route('')
|
|
@login_required
|
|
def list_evaluations():
|
|
user = current_user
|
|
|
|
if user.role == 'president':
|
|
evaluations = Evaluation.query.order_by(Evaluation.created_at.desc()).all()
|
|
avg_scores = db.session.query(
|
|
Evaluation.player_id,
|
|
func.count(Evaluation.id).label('eval_count'),
|
|
func.avg(Evaluation.overall_score).label('avg_score')
|
|
).group_by(Evaluation.player_id).all()
|
|
player_scores = {}
|
|
for row in avg_scores:
|
|
p = User.query.get(row.player_id)
|
|
if p:
|
|
player_scores[p.id] = {'player': p, 'count': row.eval_count, 'avg': round(row.avg_score, 1) if row.avg_score else 0}
|
|
|
|
elif user.can_evaluate():
|
|
evaluations = Evaluation.query.filter_by(evaluator_id=user.id).order_by(Evaluation.created_at.desc()).all()
|
|
player_scores = {}
|
|
else:
|
|
evaluations = Evaluation.query.filter_by(player_id=user.id).order_by(Evaluation.created_at.desc()).all()
|
|
player_scores = {}
|
|
|
|
return render_template('pages/evaluations.html', evaluations=evaluations, player_scores=player_scores)
|
|
|
|
@evaluations_bp.route('/<int:tryout_id>/<int:player_id>', methods=['GET', 'POST'])
|
|
@login_required
|
|
def evaluate_player(tryout_id, player_id):
|
|
if not current_user.can_evaluate():
|
|
flash('You do not have permission to evaluate players.', 'danger')
|
|
return redirect(url_for('main.dashboard'))
|
|
|
|
tryout = Tryout.query.get_or_404(tryout_id)
|
|
player = User.query.get_or_404(player_id)
|
|
|
|
if player.role != 'player':
|
|
flash('Can only evaluate players.', 'danger')
|
|
return redirect(url_for('tryouts.view_tryout', tryout_id=tryout_id))
|
|
|
|
existing_eval = Evaluation.query.filter_by(
|
|
tryout_id=tryout_id,
|
|
player_id=player_id,
|
|
evaluator_id=current_user.id
|
|
).first()
|
|
|
|
if request.method == 'POST':
|
|
speed = request.form.get('speed_score')
|
|
agility = request.form.get('agility_score')
|
|
technique = request.form.get('technique_score')
|
|
teamwork = request.form.get('teamwork_score')
|
|
attitude = request.form.get('attitude_score')
|
|
comments = request.form.get('comments')
|
|
position = request.form.get('position_recommendation')
|
|
|
|
scores = []
|
|
if speed: scores.append(int(speed))
|
|
if agility: scores.append(int(agility))
|
|
if technique: scores.append(int(technique))
|
|
if teamwork: scores.append(int(teamwork))
|
|
if attitude: scores.append(int(attitude))
|
|
|
|
overall = sum(scores) / len(scores) if scores else None
|
|
|
|
if existing_eval:
|
|
existing_eval.speed_score = int(speed) if speed else None
|
|
existing_eval.agility_score = int(agility) if agility else None
|
|
existing_eval.technique_score = int(technique) if technique else None
|
|
existing_eval.teamwork_score = int(teamwork) if teamwork else None
|
|
existing_eval.attitude_score = int(attitude) if attitude else None
|
|
existing_eval.overall_score = overall
|
|
existing_eval.comments = comments
|
|
existing_eval.position_recommendation = position
|
|
flash('Evaluation updated!', 'success')
|
|
else:
|
|
evaluation = Evaluation(
|
|
tryout_id=tryout_id,
|
|
player_id=player_id,
|
|
evaluator_id=current_user.id,
|
|
speed_score=int(speed) if speed else None,
|
|
agility_score=int(agility) if agility else None,
|
|
technique_score=int(technique) if technique else None,
|
|
teamwork_score=int(teamwork) if teamwork else None,
|
|
attitude_score=int(attitude) if attitude else None,
|
|
overall_score=overall,
|
|
comments=comments,
|
|
position_recommendation=position
|
|
)
|
|
db.session.add(evaluation)
|
|
flash('Evaluation submitted successfully!', 'success')
|
|
|
|
db.session.commit()
|
|
return redirect(url_for('evaluations.list_evaluations'))
|
|
|
|
evaluators = None
|
|
if current_user.role == 'president':
|
|
all_evaluations = Evaluation.query.filter_by(tryout_id=tryout_id, player_id=player_id).all()
|
|
evaluators = [{'evaluator': User.query.get(e.evaluator_id), 'eval': e} for e in all_evaluations]
|
|
|
|
return render_template('pages/evaluate_player.html',
|
|
tryout=tryout,
|
|
player=player,
|
|
existing_eval=existing_eval,
|
|
evaluators=evaluators)
|
|
|
|
@evaluations_bp.route('/<int:tryout_id>/players')
|
|
@login_required
|
|
def players_to_evaluate(tryout_id):
|
|
if not current_user.can_evaluate():
|
|
flash('Permission denied.', 'danger')
|
|
return redirect(url_for('main.dashboard'))
|
|
|
|
tryout = Tryout.query.get_or_404(tryout_id)
|
|
registrations = TryoutRegistration.query.filter_by(tryout_id=tryout_id).all()
|
|
players = []
|
|
for reg in registrations:
|
|
p = User.query.get(reg.player_id)
|
|
if p and p.role == 'player':
|
|
existing = Evaluation.query.filter_by(
|
|
tryout_id=tryout_id,
|
|
player_id=p.id,
|
|
evaluator_id=current_user.id
|
|
).first()
|
|
players.append({'player': p, 'evaluated': existing is not None, 'registration': reg})
|
|
|
|
return render_template('pages/players_to_evaluate.html', tryout=tryout, players=players) |