import os from flask import Flask from markupsafe import Markup from config import Config from models import db, bcrypt, login_manager from flask_mail import Mail # Initialize extensions outside create_app so blueprints can import them if needed # (Though best practice is usually to init in create_app, we'll keep the existing structure and add mail) mail = Mail() def create_app(config_class=Config): app = Flask(__name__) app.config.from_object(config_class) # Mail configuration explicitly set to prevent crashing if config.py is sparse app.config['MAIL_SERVER'] = os.environ.get('MAIL_SERVER', 'smtp.googlemail.com') app.config['MAIL_PORT'] = int(os.environ.get('MAIL_PORT', 587)) app.config['MAIL_USE_TLS'] = os.environ.get('MAIL_USE_TLS', 'true').lower() in ['true', 'on', '1'] app.config['MAIL_USERNAME'] = os.environ.get('MAIL_USERNAME', 'your_email@gmail.com') app.config['MAIL_PASSWORD'] = os.environ.get('MAIL_PASSWORD', 'your_app_password') # Initialize extensions db.init_app(app) bcrypt.init_app(app) login_manager.init_app(app) mail.init_app(app) login_manager.login_view = 'auth.login' login_manager.login_message_category = 'info' # Create upload directory if it doesn't exist os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True) # Register blueprints (we will create these next) from routes.auth import auth_bp from routes.dashboard import dashboard_bp from routes.feedback import feedback_bp from routes.reports import reports_bp from routes.upload import upload_bp from routes.admin import admin_bp from routes.profile import profile_bp from routes.training import training_bp app.register_blueprint(auth_bp, url_prefix='/auth') app.register_blueprint(dashboard_bp, url_prefix='/dashboard') app.register_blueprint(feedback_bp, url_prefix='/feedback') app.register_blueprint(reports_bp, url_prefix='/reports') app.register_blueprint(upload_bp, url_prefix='/upload') app.register_blueprint(admin_bp, url_prefix='/admin') app.register_blueprint(profile_bp, url_prefix='/profile') app.register_blueprint(training_bp, url_prefix='/admin') # Root route redirects to dashboard or login @app.route('/') def index(): from flask import redirect, url_for from flask_login import current_user if current_user.is_authenticated: return redirect(url_for('dashboard.index')) return redirect(url_for('auth.login')) # Custom Jinja filters @app.template_filter('sentiment_color') def sentiment_color(sentiment): return { 'Positive': 'text-green-500', 'Negative': 'text-red-500', 'Neutral': 'text-gray-500' }.get(sentiment, 'text-gray-500') @app.template_filter('sentiment_badge') def sentiment_badge(sentiment): colors = { 'Positive': 'bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-300', 'Negative': 'bg-red-100 text-red-800 dark:bg-red-900 dark:text-red-300', 'Neutral': 'bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-300' } classes = colors.get(sentiment, colors['Neutral']) return Markup(f'{sentiment}') return app if __name__ == '__main__': app = create_app() with app.app_context(): # Create database tables if they don't exist db.create_all() app.run(debug=True, port=5000)