File: lazy_django.py

package info (click to toggle)
pytest-django 4.11.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 604 kB
  • sloc: python: 4,006; makefile: 39; sh: 17
file content (40 lines) | stat: -rw-r--r-- 1,052 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""
Helpers to load Django lazily when Django settings can't be configured.
"""

from __future__ import annotations

import os
import sys
from typing import Any

import pytest


def skip_if_no_django() -> None:
    """Raises a skip exception when no Django settings are available"""
    if not django_settings_is_configured():
        pytest.skip("no Django settings")


def django_settings_is_configured() -> bool:
    """Return whether the Django settings module has been configured.

    This uses either the DJANGO_SETTINGS_MODULE environment variable, or the
    configured flag in the Django settings object if django.conf has already
    been imported.
    """
    ret = bool(os.environ.get("DJANGO_SETTINGS_MODULE"))

    if not ret and "django.conf" in sys.modules:
        django_conf: Any = sys.modules["django.conf"]
        ret = django_conf.settings.configured

    return ret


def get_django_version() -> tuple[int, int, int, str, int]:
    import django

    version: tuple[int, int, int, str, int] = django.VERSION
    return version