Demande par discord au coach pour confirmer la rencontre Ajout de message automatisé discord pour avertir des nouveaux match et rappel 24h a l'avance
104 lines
3.1 KiB
Python
104 lines
3.1 KiB
Python
"""Team Tryouts Application - Flask Application Factory.
|
|
|
|
This module provides the application factory for creating and configuring
|
|
the Flask application instance.
|
|
"""
|
|
|
|
import os
|
|
from flask import Flask
|
|
from extensions import db, login_manager, csrf, hash_password, check_password
|
|
from sqlalchemy import text
|
|
import markupsafe
|
|
|
|
|
|
def nl2br(value):
|
|
"""Convert newlines to HTML line breaks.
|
|
|
|
Args:
|
|
value: String value to convert.
|
|
|
|
Returns:
|
|
Markup: HTML-safe string with line breaks.
|
|
"""
|
|
if value:
|
|
return markupsafe.Markup('<br>'.join(str(value).splitlines()))
|
|
return ''
|
|
|
|
|
|
def create_app():
|
|
"""Create and configure the Flask application.
|
|
|
|
Initializes Flask with:
|
|
- Secret key for session security
|
|
- SQLite database configuration
|
|
- CSRF protection
|
|
- Login manager
|
|
- All route blueprints
|
|
|
|
Handles database initialization and seeding with sample data if empty.
|
|
|
|
Returns:
|
|
Flask: Configured Flask application instance.
|
|
"""
|
|
app = Flask(__name__)
|
|
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', 'team-tryouts-secret-key-change-in-production')
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///team_tryouts.db'
|
|
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
|
app.config['WTF_CSRF_ENABLED'] = True
|
|
|
|
db.init_app(app)
|
|
login_manager.init_app(app)
|
|
csrf.init_app(app)
|
|
|
|
from routes.auth import auth_bp
|
|
from routes.tryouts import tryouts_bp
|
|
from routes.evaluations import evaluations_bp
|
|
from routes.users import users_bp
|
|
from routes.main import main_bp
|
|
from routes.teams import teams_bp
|
|
from routes.matches import matches_bp
|
|
|
|
app.register_blueprint(auth_bp)
|
|
app.register_blueprint(tryouts_bp)
|
|
app.register_blueprint(evaluations_bp)
|
|
app.register_blueprint(users_bp)
|
|
app.register_blueprint(main_bp)
|
|
app.register_blueprint(teams_bp)
|
|
app.register_blueprint(matches_bp)
|
|
|
|
# Register custom Jinja filters
|
|
app.jinja_env.filters['nl2br'] = nl2br
|
|
|
|
with app.app_context():
|
|
import models
|
|
from models import User, MatchParticipant
|
|
try:
|
|
# Check if the database schema is up to date by testing a query
|
|
# that uses all model columns including team_side for match participants
|
|
db.session.execute(text('SELECT games, team_side FROM match_participants LIMIT 1'))
|
|
db.create_all()
|
|
except Exception:
|
|
# If there's a schema mismatch, drop and recreate all tables
|
|
db.session.rollback()
|
|
db.drop_all()
|
|
db.create_all()
|
|
|
|
# Seed database if empty
|
|
if User.query.count() == 0:
|
|
from seed import seed_database
|
|
seed_database()
|
|
|
|
# Start the Discord bot for notifications
|
|
try:
|
|
from discord_bot import start_bot
|
|
start_bot()
|
|
except Exception as e:
|
|
import logging
|
|
logging.getLogger(__name__).warning(f"Could not start Discord bot: {e}")
|
|
|
|
return app
|
|
|
|
|
|
if __name__ == '__main__':
|
|
app = create_app()
|
|
app.run(debug=True, host='0.0.0.0', port=5000) |