2026-08-14 18:02:25 +02:00
|
|
|
"""
|
|
|
|
|
Training Routes - GMAO Collège
|
|
|
|
|
Gestion des formations
|
|
|
|
|
"""
|
|
|
|
|
from flask import Blueprint, render_template, request, flash, redirect, url_for
|
|
|
|
|
from flask_login import login_required, current_user
|
2026-08-15 00:47:55 +02:00
|
|
|
from datetime import datetime
|
2026-08-14 18:02:25 +02:00
|
|
|
from app_new.extensions import db
|
|
|
|
|
from ..core.models.planning import Training, TrainingParticipant
|
|
|
|
|
|
|
|
|
|
training_bp = Blueprint('trainings', __name__)
|
|
|
|
|
|
|
|
|
|
@training_bp.route('/')
|
|
|
|
|
@login_required
|
|
|
|
|
def index():
|
|
|
|
|
"""Liste des formations."""
|
|
|
|
|
trainings = Training.query.order_by(Training.start_date).all()
|
|
|
|
|
return render_template('training/index.html', trainings=trainings)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@training_bp.route('/new', methods=['GET', 'POST'])
|
|
|
|
|
@login_required
|
|
|
|
|
def create():
|
|
|
|
|
"""Créer une formation."""
|
|
|
|
|
if request.method == 'POST':
|
2026-08-15 00:47:55 +02:00
|
|
|
start_date = _parse_date(request.form.get('start_date'))
|
|
|
|
|
end_date = _parse_date(request.form.get('end_date')) or start_date
|
|
|
|
|
if not start_date or end_date < start_date:
|
|
|
|
|
flash('La période de formation est invalide.', 'danger')
|
|
|
|
|
return render_template('training/new.html'), 400
|
2026-08-14 18:02:25 +02:00
|
|
|
training = Training(
|
2026-08-15 00:47:55 +02:00
|
|
|
name=(request.form.get('title') or '').strip(),
|
2026-08-14 18:02:25 +02:00
|
|
|
description=request.form.get('description'),
|
2026-08-15 00:47:55 +02:00
|
|
|
start_date=start_date,
|
|
|
|
|
end_date=end_date,
|
2026-08-14 18:02:25 +02:00
|
|
|
location=request.form.get('location'),
|
|
|
|
|
)
|
|
|
|
|
db.session.add(training)
|
2026-08-15 00:56:36 +02:00
|
|
|
db.session.flush()
|
|
|
|
|
db.session.add(TrainingParticipant(
|
|
|
|
|
training_id=training.id,
|
|
|
|
|
user_id=current_user.id,
|
|
|
|
|
is_confirmed=True,
|
|
|
|
|
notes='Créateur de la formation',
|
|
|
|
|
))
|
2026-08-14 18:02:25 +02:00
|
|
|
db.session.commit()
|
|
|
|
|
flash('Formation créée avec succès.', 'success')
|
|
|
|
|
return redirect(url_for('trainings.index'))
|
|
|
|
|
|
|
|
|
|
return render_template('training/new.html')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@training_bp.route('/<int:id>/register', methods=['POST'])
|
|
|
|
|
@login_required
|
|
|
|
|
def register(id):
|
|
|
|
|
"""S'inscrire à une formation."""
|
|
|
|
|
training = Training.query.get_or_404(id)
|
|
|
|
|
participant = TrainingParticipant.query.filter_by(
|
|
|
|
|
training_id=id, user_id=current_user.id
|
|
|
|
|
).first()
|
|
|
|
|
|
|
|
|
|
if participant:
|
|
|
|
|
flash('Déjà inscrit.', 'warning')
|
|
|
|
|
else:
|
|
|
|
|
participant = TrainingParticipant(
|
|
|
|
|
training_id=id,
|
|
|
|
|
user_id=current_user.id,
|
2026-08-15 00:47:55 +02:00
|
|
|
is_confirmed=True,
|
2026-08-14 18:02:25 +02:00
|
|
|
)
|
|
|
|
|
db.session.add(participant)
|
|
|
|
|
db.session.commit()
|
|
|
|
|
flash('Inscription réussie.', 'success')
|
|
|
|
|
|
|
|
|
|
return redirect(url_for('trainings.index'))
|
|
|
|
|
|
|
|
|
|
@training_bp.route('/my-trainings')
|
|
|
|
|
@login_required
|
|
|
|
|
def my_trainings():
|
|
|
|
|
"""Mes formations."""
|
|
|
|
|
participations = TrainingParticipant.query.filter_by(user_id=current_user.id).all()
|
2026-08-15 00:47:55 +02:00
|
|
|
return render_template('training/my_trainings.html', participations=participations)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _parse_date(value):
|
|
|
|
|
try:
|
|
|
|
|
return datetime.strptime(value, '%Y-%m-%d').date() if value else None
|
|
|
|
|
except (TypeError, ValueError):
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@training_bp.route('/<int:id>')
|
|
|
|
|
@login_required
|
|
|
|
|
def detail(id):
|
|
|
|
|
training = Training.query.get_or_404(id)
|
|
|
|
|
participants = TrainingParticipant.query.filter_by(training_id=id).all()
|
|
|
|
|
participant_users = {participant.user_id: participant for participant in participants}
|
|
|
|
|
return render_template('training/detail.html', training=training,
|
|
|
|
|
participants=participants, participant_users=participant_users)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@training_bp.route('/<int:id>/edit', methods=['GET', 'POST'])
|
|
|
|
|
@login_required
|
|
|
|
|
def edit(id):
|
|
|
|
|
training = Training.query.get_or_404(id)
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
start_date = _parse_date(request.form.get('start_date'))
|
|
|
|
|
end_date = _parse_date(request.form.get('end_date')) or start_date
|
|
|
|
|
if not start_date or end_date < start_date:
|
|
|
|
|
flash('La période de formation est invalide.', 'danger')
|
|
|
|
|
return render_template('training/new.html', training=training), 400
|
|
|
|
|
training.name = (request.form.get('title') or '').strip()
|
|
|
|
|
training.description = request.form.get('description')
|
|
|
|
|
training.start_date = start_date
|
|
|
|
|
training.end_date = end_date
|
|
|
|
|
training.location = request.form.get('location')
|
|
|
|
|
db.session.commit()
|
|
|
|
|
flash('Formation mise à jour.', 'success')
|
|
|
|
|
return redirect(url_for('trainings.detail', id=id))
|
|
|
|
|
return render_template('training/new.html', training=training)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@training_bp.route('/<int:id>/unregister', methods=['POST'])
|
|
|
|
|
@login_required
|
|
|
|
|
def unregister(id):
|
|
|
|
|
participant = TrainingParticipant.query.filter_by(
|
|
|
|
|
training_id=id, user_id=current_user.id
|
|
|
|
|
).first_or_404()
|
|
|
|
|
db.session.delete(participant)
|
|
|
|
|
db.session.commit()
|
|
|
|
|
flash('Inscription annulée.', 'success')
|
|
|
|
|
return redirect(url_for('trainings.detail', id=id))
|