Download app/__init__.py from rastof9/fb: direct link, hf CLI and curl.
- Browser
- Download file 5.59 kB
-
https://huggingface.co/spaces/rastof9/fb/resolve/main/app/__init__.py
- Command line
-
hf download hf://spaces/rastof9/fb/app/__init__.py
-
curl -L -o __init__.py https://huggingface.co/spaces/rastof9/fb/resolve/main/app/__init__.py
5.59 kB
| from flask import Flask, redirect, url_for | |
| from flask_sqlalchemy import SQLAlchemy | |
| from flask_migrate import Migrate | |
| from flask_login import LoginManager, current_user | |
| from celery import Celery | |
| from config import Config | |
| import redis | |
| import os | |
| import logging | |
| import sys | |
| from pathlib import Path | |
| # Configure logging | |
| logging.basicConfig( | |
| level=logging.DEBUG, | |
| format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', | |
| handlers=[logging.StreamHandler(sys.stdout)] | |
| ) | |
| logger = logging.getLogger(__name__) | |
| # Initialize extensions | |
| db = SQLAlchemy() | |
| migrate = Migrate() | |
| login = LoginManager() | |
| login.login_view = 'auth.login' | |
| celery = Celery(__name__, broker=Config.CELERY_BROKER_URL) | |
| cache = None # Initialize later when app context is available | |
| # Set up user loader for Flask-Login | |
| def load_user(user_id): | |
| from .models import User | |
| return User.query.get(int(user_id)) | |
| def create_app(config_class=Config): | |
| logger.info("Starting application initialization...") | |
| # Create the Flask app | |
| app = Flask(__name__) | |
| logger.info("Flask app created") | |
| # Load configuration | |
| logger.info(f"Loading configuration from {config_class}...") | |
| app.config.from_object(config_class) | |
| # Ensure instance path exists | |
| logger.info(f"Ensuring instance path exists: {app.config['INSTANCE_PATH']}") | |
| Path(app.config['INSTANCE_PATH']).mkdir(parents=True, exist_ok=True) | |
| app.instance_path = app.config['INSTANCE_PATH'] | |
| # Initialize extensions | |
| logger.info("Initializing SQLAlchemy...") | |
| db.init_app(app) | |
| migrate.init_app(app, db) | |
| logger.info("Initializing LoginManager...") | |
| login.init_app(app) | |
| # Configure Celery | |
| logger.info("Configuring Celery...") | |
| celery.conf.update(app.config) | |
| # Initialize Redis cache | |
| try: | |
| logger.info(f"Initializing Redis cache with URL: {app.config['CELERY_BROKER_URL']}") | |
| global cache | |
| # Only try to connect to Redis if the URL starts with 'redis://' | |
| if app.config['CELERY_BROKER_URL'].startswith('redis://'): | |
| cache = redis.Redis.from_url(app.config['CELERY_BROKER_URL']) | |
| logger.info("Redis cache initialized successfully") | |
| else: | |
| logger.info("Not using Redis cache as broker URL is not Redis") | |
| cache = None | |
| except Exception as e: | |
| logger.error(f"Error initializing Redis cache: {e}") | |
| # Continue without Redis | |
| cache = None | |
| logger.info("Continuing without Redis cache") | |
| # Register test blueprint first (this should always work) | |
| logger.info("Registering test blueprint...") | |
| try: | |
| from .routes.test import test_bp | |
| app.register_blueprint(test_bp) | |
| logger.info("Test blueprint registered successfully") | |
| except Exception as e: | |
| logger.error(f"Error registering test blueprint: {e}") | |
| # Register other blueprints | |
| logger.info("Registering other blueprints...") | |
| # Try to register each blueprint individually | |
| try: | |
| logger.info("Importing and registering dashboard blueprint...") | |
| from .routes.dashboard import dashboard_bp | |
| app.register_blueprint(dashboard_bp, url_prefix='/dashboard') | |
| logger.info("Dashboard blueprint registered successfully") | |
| except Exception as e: | |
| logger.error(f"Error registering dashboard blueprint: {e}") | |
| try: | |
| logger.info("Importing and registering API blueprint...") | |
| from .routes.api import api_bp | |
| app.register_blueprint(api_bp) | |
| logger.info("API blueprint registered successfully") | |
| except Exception as e: | |
| logger.error(f"Error registering API blueprint: {e}") | |
| try: | |
| logger.info("Importing and registering compliance blueprint...") | |
| from .routes.compliance import compliance_bp | |
| app.register_blueprint(compliance_bp) | |
| logger.info("Compliance blueprint registered successfully") | |
| except Exception as e: | |
| logger.error(f"Error registering compliance blueprint: {e}") | |
| try: | |
| logger.info("Importing and registering Google Ads blueprint...") | |
| from .routes.google_ads import google_ads_bp | |
| app.register_blueprint(google_ads_bp) | |
| logger.info("Google Ads blueprint registered successfully") | |
| except Exception as e: | |
| logger.error(f"Error registering Google Ads blueprint: {e}") | |
| try: | |
| logger.info("Importing and registering auth blueprint...") | |
| from .routes.auth import auth_bp | |
| app.register_blueprint(auth_bp) | |
| logger.info("Auth blueprint registered successfully") | |
| except Exception as e: | |
| logger.error(f"Error registering auth blueprint: {e}") | |
| try: | |
| logger.info("Importing and registering Facebook Ads blueprint...") | |
| from .routes.facebook_ads import facebook_ads_bp | |
| app.register_blueprint(facebook_ads_bp) | |
| logger.info("Facebook Ads blueprint registered successfully") | |
| except Exception as e: | |
| logger.error(f"Error registering Facebook Ads blueprint: {e}") | |
| # Add a simple route directly to the app | |
| def index(): | |
| if current_user.is_authenticated: | |
| try: | |
| return redirect(url_for('dashboard.index')) | |
| except: | |
| # Fallback to facebook_ads if dashboard is not available | |
| return redirect(url_for('facebook_ads.index')) | |
| else: | |
| return redirect(url_for('auth.login')) | |
| logger.info("Application initialization complete") | |
| return app |