lazy_django.py 977 B

123456789101112131415161718192021222324252627282930313233343536
  1. """
  2. Helpers to load Django lazily when Django settings can't be configured.
  3. """
  4. import os
  5. import sys
  6. from typing import Any, Tuple
  7. import pytest
  8. def skip_if_no_django() -> None:
  9. """Raises a skip exception when no Django settings are available"""
  10. if not django_settings_is_configured():
  11. pytest.skip("no Django settings")
  12. def django_settings_is_configured() -> bool:
  13. """Return whether the Django settings module has been configured.
  14. This uses either the DJANGO_SETTINGS_MODULE environment variable, or the
  15. configured flag in the Django settings object if django.conf has already
  16. been imported.
  17. """
  18. ret = bool(os.environ.get("DJANGO_SETTINGS_MODULE"))
  19. if not ret and "django.conf" in sys.modules:
  20. django_conf = sys.modules["django.conf"] # type: Any
  21. return django_conf.settings.configured
  22. return ret
  23. def get_django_version() -> Tuple[int, int, int, str, int]:
  24. import django
  25. return django.VERSION