Changement du refresh handler, soft reset d'ajouter sur beaucoup de formulaire de modification
CI - Security, Lint & Tests / validate (push) Failing after 1m3s
CI - Security, Lint & Tests / validate (push) Failing after 1m3s
This commit is contained in:
+66
-37
@@ -9,19 +9,35 @@ import os
|
||||
from datetime import datetime
|
||||
|
||||
from flask import (
|
||||
Blueprint, render_template, redirect, url_for, flash, request,
|
||||
Blueprint,
|
||||
flash,
|
||||
redirect,
|
||||
render_template,
|
||||
request,
|
||||
send_file,
|
||||
url_for,
|
||||
)
|
||||
from flask_login import login_required, current_user
|
||||
from flask_login import current_user, login_required
|
||||
|
||||
from app.extensions import db
|
||||
from app.models import (
|
||||
Admin, User, Tryout, OrgTeam, TeamPlayer, TeamMatch,
|
||||
TeamMatchParticipant, AppSettings, BackupRecord, AuditLog,
|
||||
Admin,
|
||||
AppSettings,
|
||||
AuditLog,
|
||||
BackupRecord,
|
||||
OrgTeam,
|
||||
TeamMatch,
|
||||
TeamMatchParticipant,
|
||||
TeamPlayer,
|
||||
Tryout,
|
||||
User,
|
||||
)
|
||||
from app.responders import respond_after_change
|
||||
from app.supporting_scripts.backup import (
|
||||
BACKUP_DIR, BackupError, backup_database, backup_documents,
|
||||
create_backup_dir, parse_database_url, verify_backup,
|
||||
BackupError,
|
||||
backup_database,
|
||||
create_backup_dir,
|
||||
parse_database_url,
|
||||
)
|
||||
|
||||
admin_bp = Blueprint('admin', __name__, url_prefix='/admin')
|
||||
@@ -56,6 +72,7 @@ def _log(action, details=None):
|
||||
# Dashboard
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@admin_bp.route('')
|
||||
@login_required
|
||||
def dashboard():
|
||||
@@ -96,6 +113,7 @@ def dashboard():
|
||||
# Backups
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _create_backup_record(backup_type='manual', notes=None):
|
||||
"""Run a database backup and return a BackupRecord, or raise BackupError."""
|
||||
create_backup_dir()
|
||||
@@ -130,11 +148,11 @@ def create_backup():
|
||||
except BackupError as e:
|
||||
flash(f'Backup failed: {e}', 'danger')
|
||||
_log('backup_failed', f'Error: {e}')
|
||||
return redirect(url_for('admin.dashboard'))
|
||||
return respond_after_change(url_for('admin.dashboard'))
|
||||
|
||||
_log('backup_created', f'File: {record.filename} ({record.size_bytes} bytes)')
|
||||
flash(f'Backup created successfully: {record.filename}', 'success')
|
||||
return redirect(url_for('admin.dashboard'))
|
||||
return respond_after_change(url_for('admin.dashboard'))
|
||||
|
||||
|
||||
@admin_bp.route('/backup/<int:backup_id>/download')
|
||||
@@ -147,7 +165,7 @@ def download_backup(backup_id):
|
||||
record = BackupRecord.query.get_or_404(backup_id)
|
||||
if not os.path.exists(record.file_path):
|
||||
flash('Backup file is missing from disk.', 'danger')
|
||||
return redirect(url_for('admin.dashboard'))
|
||||
return respond_after_change(url_for('admin.dashboard'))
|
||||
|
||||
_log('backup_downloaded', f'File: {record.filename}')
|
||||
return send_file(record.file_path, as_attachment=True, download_name=record.filename)
|
||||
@@ -166,13 +184,13 @@ def delete_backup(backup_id):
|
||||
os.remove(record.file_path)
|
||||
except OSError as e:
|
||||
flash(f'Could not remove backup file: {e}', 'danger')
|
||||
return redirect(url_for('admin.dashboard'))
|
||||
return respond_after_change(url_for('admin.dashboard'))
|
||||
|
||||
_log('backup_deleted', f'File: {record.filename}')
|
||||
db.session.delete(record)
|
||||
db.session.commit()
|
||||
flash(f'Backup {record.filename} deleted.', 'success')
|
||||
return redirect(url_for('admin.dashboard'))
|
||||
return respond_after_change(url_for('admin.dashboard'))
|
||||
|
||||
|
||||
@admin_bp.route('/backup/<int:backup_id>/restore', methods=['POST'])
|
||||
@@ -189,7 +207,7 @@ def restore_backup(backup_id):
|
||||
record = BackupRecord.query.get_or_404(backup_id)
|
||||
if not os.path.exists(record.file_path):
|
||||
flash('Backup file is missing from disk.', 'danger')
|
||||
return redirect(url_for('admin.dashboard'))
|
||||
return respond_after_change(url_for('admin.dashboard'))
|
||||
|
||||
# Create a safety backup of the current state before restoring.
|
||||
try:
|
||||
@@ -199,22 +217,31 @@ def restore_backup(backup_id):
|
||||
)
|
||||
except BackupError as e:
|
||||
flash(f'Could not create safety backup, restore aborted: {e}', 'danger')
|
||||
return redirect(url_for('admin.dashboard'))
|
||||
return respond_after_change(url_for('admin.dashboard'))
|
||||
|
||||
# Restore using pg_restore
|
||||
import subprocess
|
||||
|
||||
from app.supporting_scripts.backup import (
|
||||
PG_RESTORE, dump_environment, parse_database_url,
|
||||
PG_RESTORE,
|
||||
dump_environment,
|
||||
parse_database_url,
|
||||
)
|
||||
|
||||
conn = parse_database_url(os.getenv('DATABASE_URL'))
|
||||
cmd = [
|
||||
PG_RESTORE,
|
||||
'--host', conn['host'],
|
||||
'--port', conn['port'],
|
||||
'--username', conn['user'],
|
||||
'--dbname', conn['dbname'],
|
||||
'--clean', '--if-exists', '--no-owner',
|
||||
'--host',
|
||||
conn['host'],
|
||||
'--port',
|
||||
conn['port'],
|
||||
'--username',
|
||||
conn['user'],
|
||||
'--dbname',
|
||||
conn['dbname'],
|
||||
'--clean',
|
||||
'--if-exists',
|
||||
'--no-owner',
|
||||
record.file_path,
|
||||
]
|
||||
try:
|
||||
@@ -230,20 +257,21 @@ def restore_backup(backup_id):
|
||||
except (subprocess.TimeoutExpired, FileNotFoundError, RuntimeError) as e:
|
||||
flash(f'Restore failed: {e}', 'danger')
|
||||
_log('backup_restore_failed', f'File: {record.filename}, Error: {e}')
|
||||
return redirect(url_for('admin.dashboard'))
|
||||
return respond_after_change(url_for('admin.dashboard'))
|
||||
|
||||
_log('backup_restored', f'File: {record.filename}')
|
||||
flash(
|
||||
f'Backup {record.filename} restored successfully. The database has been rolled back.',
|
||||
'success',
|
||||
)
|
||||
return redirect(url_for('admin.dashboard'))
|
||||
return respond_after_change(url_for('admin.dashboard'))
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Tryouts open/close toggle
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@admin_bp.route('/toggle-tryouts', methods=['POST'])
|
||||
@login_required
|
||||
def toggle_tryouts():
|
||||
@@ -258,13 +286,14 @@ def toggle_tryouts():
|
||||
state = 'opened' if new_value else 'closed'
|
||||
_log('tryouts_toggled', f'Tryouts {state}')
|
||||
flash(f'Tryouts are now {state}.', 'success')
|
||||
return redirect(url_for('admin.dashboard'))
|
||||
return respond_after_change(url_for('admin.dashboard'))
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Season lifecycle
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@admin_bp.route('/season/start', methods=['POST'])
|
||||
@login_required
|
||||
def start_season():
|
||||
@@ -274,19 +303,19 @@ def start_season():
|
||||
|
||||
if AppSettings.get_bool('season_active', default=False):
|
||||
flash('A season is already active. End it before starting a new one.', 'danger')
|
||||
return redirect(url_for('admin.dashboard'))
|
||||
return respond_after_change(url_for('admin.dashboard'))
|
||||
|
||||
name = request.form.get('season_name', '').strip()
|
||||
start_date = request.form.get('season_start', '').strip()
|
||||
if not start_date:
|
||||
flash('A season start date is required.', 'danger')
|
||||
return redirect(url_for('admin.dashboard'))
|
||||
return respond_after_change(url_for('admin.dashboard'))
|
||||
|
||||
try:
|
||||
datetime.strptime(start_date, '%Y-%m-%d')
|
||||
except ValueError:
|
||||
flash('Invalid season start date format.', 'danger')
|
||||
return redirect(url_for('admin.dashboard'))
|
||||
return respond_after_change(url_for('admin.dashboard'))
|
||||
|
||||
AppSettings.set('season_name', name or 'Untitled Season')
|
||||
AppSettings.set('season_start', start_date)
|
||||
@@ -295,7 +324,7 @@ def start_season():
|
||||
|
||||
_log('season_started', f'Season: {name or "Untitled Season"}, Start: {start_date}')
|
||||
flash(f'Season "{name or "Untitled Season"}" has begun.', 'success')
|
||||
return redirect(url_for('admin.dashboard'))
|
||||
return respond_after_change(url_for('admin.dashboard'))
|
||||
|
||||
|
||||
@admin_bp.route('/season/end', methods=['POST'])
|
||||
@@ -307,18 +336,18 @@ def end_season():
|
||||
|
||||
if not AppSettings.get_bool('season_active', default=False):
|
||||
flash('No season is currently active.', 'danger')
|
||||
return redirect(url_for('admin.dashboard'))
|
||||
return respond_after_change(url_for('admin.dashboard'))
|
||||
|
||||
end_date = request.form.get('season_end', '').strip()
|
||||
if not end_date:
|
||||
flash('A season end date is required.', 'danger')
|
||||
return redirect(url_for('admin.dashboard'))
|
||||
return respond_after_change(url_for('admin.dashboard'))
|
||||
|
||||
try:
|
||||
datetime.strptime(end_date, '%Y-%m-%d')
|
||||
except ValueError:
|
||||
flash('Invalid season end date format.', 'danger')
|
||||
return redirect(url_for('admin.dashboard'))
|
||||
return respond_after_change(url_for('admin.dashboard'))
|
||||
|
||||
name = AppSettings.get('season_name')
|
||||
AppSettings.set('season_end', end_date)
|
||||
@@ -326,7 +355,9 @@ def end_season():
|
||||
|
||||
_log('season_ended', f'Season: {name}, End: {end_date}')
|
||||
flash(f'Season "{name}" has ended.', 'success')
|
||||
return redirect(url_for('admin.dashboard'))
|
||||
return respond_after_change(url_for('admin.dashboard'))
|
||||
|
||||
|
||||
@admin_bp.route('/toggle-document-uploads', methods=['POST'])
|
||||
@login_required
|
||||
def toggle_document_uploads():
|
||||
@@ -341,16 +372,14 @@ def toggle_document_uploads():
|
||||
state = 'enabled' if new_value else 'disabled'
|
||||
_log('document_uploads_toggled', f'Document uploads {state}')
|
||||
flash(f'Document uploads are now {state}.', 'success')
|
||||
return redirect(url_for('admin.dashboard'))
|
||||
|
||||
|
||||
|
||||
return respond_after_change(url_for('admin.dashboard'))
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Wipe teams for new season
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@admin_bp.route('/teams/wipe', methods=['POST'])
|
||||
@login_required
|
||||
def wipe_teams():
|
||||
@@ -366,7 +395,7 @@ def wipe_teams():
|
||||
confirm = request.form.get('confirm', '').strip()
|
||||
if confirm != 'WIPE':
|
||||
flash("Type 'WIPE' in the confirmation box to proceed.", 'danger')
|
||||
return redirect(url_for('admin.dashboard'))
|
||||
return respond_after_change(url_for('admin.dashboard'))
|
||||
|
||||
# Safety backup before destructive operation
|
||||
try:
|
||||
@@ -376,7 +405,7 @@ def wipe_teams():
|
||||
)
|
||||
except BackupError as e:
|
||||
flash(f'Wipe aborted — could not create safety backup: {e}', 'danger')
|
||||
return redirect(url_for('admin.dashboard'))
|
||||
return respond_after_change(url_for('admin.dashboard'))
|
||||
|
||||
roster_count = TeamPlayer.query.count()
|
||||
match_count = TeamMatch.query.count()
|
||||
@@ -393,4 +422,4 @@ def wipe_teams():
|
||||
f'and {match_count} regular-season matches.',
|
||||
'success',
|
||||
)
|
||||
return redirect(url_for('admin.dashboard'))
|
||||
return respond_after_change(url_for('admin.dashboard'))
|
||||
|
||||
Reference in New Issue
Block a user