#!/usr/bin/env python """Django's command-line utility for administrative tasks.""" import os import sys from pathlib import Path # Add the backend directory to Python path so 'api' module can be found backend_dir = Path(__file__).resolve().parent if str(backend_dir) not in sys.path: sys.path.insert(0, str(backend_dir)) def main(): """Run administrative tasks.""" # Auto-detect environment based on command line arguments and environment variables settings_module = detect_settings_module() os.environ.setdefault("DJANGO_SETTINGS_MODULE", settings_module) try: from django.core.management import execute_from_command_line except ImportError as exc: print( "Error: Couldn't import Django. Make sure Django is installed and " "available on your PYTHONPATH environment variable. " "Did you forget to activate a virtual environment?" ) print("\nTo set up your development environment, try:") print(" python manage.py setup_dev") raise ImportError( "Couldn't import Django. Are you sure it's installed and " "available on your PYTHONPATH environment variable? Did you " "forget to activate a virtual environment?" ) from exc execute_from_command_line(sys.argv) def detect_settings_module(): """Auto-detect the appropriate settings module based on context.""" # Check if DJANGO_SETTINGS_MODULE is already set if "DJANGO_SETTINGS_MODULE" in os.environ: return os.environ["DJANGO_SETTINGS_MODULE"] # Test environment detection if "test" in sys.argv: if "accounts" in sys.argv: return "config.django.test_accounts" return "config.django.test" # Check for production indicators production_indicators = [ "DYNO", # Heroku "AWS_EXECUTION_ENV", # AWS Lambda "KUBERNETES_SERVICE_HOST", # Kubernetes "DOCKER_CONTAINER", # Docker ] if any(indicator in os.environ for indicator in production_indicators): return "config.django.production" # Check DEBUG environment variable debug = os.environ.get("DEBUG", "").lower() if debug in ("false", "0", "no"): return "config.django.production" # Default to local development return "config.django.local" if __name__ == "__main__": main()