2025-10-16 15:14:51 +02:00
|
|
|
"""Flask application factory"""
|
|
|
|
|
import os
|
|
|
|
|
from flask import Flask
|
|
|
|
|
from flask_login import LoginManager
|
|
|
|
|
from flask_limiter import Limiter
|
|
|
|
|
from flask_limiter.util import get_remote_address
|
|
|
|
|
from flask_wtf.csrf import CSRFProtect
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_app(config_name='development'):
|
|
|
|
|
"""Create and configure the Flask application
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
config_name: Configuration environment (development, production, testing)
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
Flask application instance
|
|
|
|
|
"""
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
# Load configuration
|
|
|
|
|
if config_name == 'production':
|
|
|
|
|
from config.production import ProductionConfig
|
|
|
|
|
app.config.from_object(ProductionConfig)
|
|
|
|
|
elif config_name == 'testing':
|
|
|
|
|
from config.testing import TestingConfig
|
|
|
|
|
app.config.from_object(TestingConfig)
|
|
|
|
|
else:
|
|
|
|
|
from config.development import DevelopmentConfig
|
|
|
|
|
app.config.from_object(DevelopmentConfig)
|
|
|
|
|
|
|
|
|
|
# Ensure data directory exists
|
|
|
|
|
os.makedirs(app.config['DATA_DIR'], exist_ok=True)
|
|
|
|
|
|
|
|
|
|
# Initialize Flask-WTF CSRF Protection
|
|
|
|
|
csrf = CSRFProtect()
|
|
|
|
|
csrf.init_app(app)
|
|
|
|
|
|
|
|
|
|
# Initialize Flask-Login
|
|
|
|
|
login_manager = LoginManager()
|
|
|
|
|
login_manager.init_app(app)
|
|
|
|
|
login_manager.login_view = 'auth.login'
|
|
|
|
|
login_manager.login_message = 'Please log in to access this page.'
|
|
|
|
|
|
|
|
|
|
@login_manager.user_loader
|
|
|
|
|
def load_user(user_id):
|
|
|
|
|
"""Load user by ID for Flask-Login"""
|
|
|
|
|
from app.models.user import User
|
|
|
|
|
return User.get_by_id(user_id)
|
|
|
|
|
|
|
|
|
|
# Initialize Flask-Limiter
|
|
|
|
|
limiter = Limiter(
|
|
|
|
|
app=app,
|
|
|
|
|
key_func=get_remote_address,
|
|
|
|
|
storage_uri=app.config['RATELIMIT_STORAGE_URL'],
|
|
|
|
|
default_limits=[f"{app.config['RATELIMIT_PER_HOUR']}/hour"] if app.config.get('RATELIMIT_ENABLED') else []
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Register blueprints
|
|
|
|
|
from app.routes import submission, dashboard, admin, auth
|
|
|
|
|
app.register_blueprint(submission.bp)
|
|
|
|
|
app.register_blueprint(dashboard.bp)
|
|
|
|
|
app.register_blueprint(admin.bp)
|
|
|
|
|
app.register_blueprint(auth.bp)
|
|
|
|
|
|
|
|
|
|
# Set index route
|
|
|
|
|
@app.route('/')
|
|
|
|
|
def index():
|
|
|
|
|
"""Welcome page"""
|
|
|
|
|
from flask import render_template
|
|
|
|
|
return render_template('index.html')
|
|
|
|
|
|
2025-10-16 20:11:16 +02:00
|
|
|
# Register error handlers
|
|
|
|
|
@app.errorhandler(403)
|
|
|
|
|
def forbidden(e):
|
|
|
|
|
"""Handle 403 Forbidden errors"""
|
|
|
|
|
from flask import render_template
|
|
|
|
|
return render_template('error_403.html'), 403
|
|
|
|
|
|
|
|
|
|
@app.errorhandler(404)
|
|
|
|
|
def not_found(e):
|
|
|
|
|
"""Handle 404 Not Found errors"""
|
|
|
|
|
from flask import render_template
|
|
|
|
|
return render_template('error_404.html'), 404
|
|
|
|
|
|
2025-10-16 15:14:51 +02:00
|
|
|
return app
|