Skip to content

django

dev_tool.services.django

__all__ = ['DjangoService'] module-attribute

DjangoService

Bases: BaseService

A service class for Django operations.

This class provides methods for various Django management commands and operations.

The constructor for the DjangoService class.

Parameters:

Source code in dev_tool/services/django/service.py
def __init__(self, strategy: ExecutionStrategyProtocol) -> None:
    """
    The constructor for the DjangoService class.

    :param strategy: The execution strategy for running commands.
    """

    super().__init__()

    self._strategy = strategy

strategy property

A property that returns the ExecutionStrategy.

Returns:

create_gunicorn_runner

A method that creates a Gunicorn server runner.

Parameters:

  • port (int) –

    The port number to bind to.

  • wsgi_app (str) –

    The WSGI application module path in gunicorn format.

  • environment (dict[str, str] | None, default: None ) –

    Optional environment variables to pass to the server process.

Returns:

Source code in dev_tool/services/django/service.py
def create_gunicorn_runner(self, port: int, wsgi_app: str, environment: dict[str, str] | None = None) -> GunicornServerRunner:
    """
    A method that creates a Gunicorn server runner.

    :param port: The port number to bind to.
    :param wsgi_app: The WSGI application module path in gunicorn format.
    :param environment: Optional environment variables to pass to the server process.
    :return: A Gunicorn server runner instance.
    """

    project_name = CONTEXT.configuration.get_project_name()

    module, _, callable_name = wsgi_app.partition(':')

    wrapper_content = self._load_python_template(
        'gunicorn_wsgi.py.template',
        module=module,
        callable=callable_name,
    )

    runner = GunicornServerRunner(
        project_name=project_name,
        port=port,
        wsgi_app=wsgi_app,
        wrapper_content=wrapper_content,
    )

    if environment:
        runner.environment = environment

    return runner

create_runner

A method that creates a Django server runner.

Parameters:

  • ip_address (str) –

    The IP address to bind to.

  • port (int) –

    The port number to bind to.

  • environment (dict[str, str] | None, default: None ) –

    Optional environment variables to pass to the server process.

Returns:

Source code in dev_tool/services/django/service.py
def create_runner(self, ip_address: str, port: int, environment: dict[str, str] | None = None) -> DjangoServerRunner:
    """
    A method that creates a Django server runner.

    :param ip_address: The IP address to bind to.
    :param port: The port number to bind to.
    :param environment: Optional environment variables to pass to the server process.
    :return: A server runner instance appropriate for the execution strategy.
    """

    project_name = CONTEXT.configuration.get_project_name()
    runner = DjangoServerRunner(project_name=project_name, ip_address=ip_address, port=port)

    if environment:
        runner.environment = environment

    return runner

run_constrained_server

A method that runs a Gunicorn server with resource constraints applied to the Docker containers. Reverts constraints by recreating the containers via ensure_database on shutdown.

Parameters:

Source code in dev_tool/services/django/service.py
@is_django_environment_variables
def run_constrained_server(self, port: int, profile: ConstraintProfileType) -> None:
    """
    A method that runs a Gunicorn server with resource constraints applied
    to the Docker containers. Reverts constraints by recreating the containers
    via ensure_database on shutdown.

    :param port: The port number to bind to.
    :param profile: The constraint profile to apply.
    """

    project_name = CONTEXT.configuration.get_project_name()
    app_name = f'{project_name}{DockerContainerDefault.APP_SUFFIX}'
    db_name = f'{project_name}{DockerContainerDefault.DB_SUFFIX}'

    self._ensure_gunicorn()

    client = create_docker_client()
    constraint = ContainerConstraint(client=client, app_name=app_name, db_name=db_name)

    message = 'Applying resource constraints...'
    emit_normal(message)

    if not constraint.apply(profile):
        message = 'Failed to apply resource constraints.'
        emit_error(message)

        return

    app = profile['app']
    db = profile['db']

    message = (
        f'Constraints applied: '
        f'app={app["mem_limit"]} RAM / {app["cpu_quota"] / app["cpu_period"]:.0%} CPU, '
        f'db={db["mem_limit"]} RAM / {db["cpu_quota"] / db["cpu_period"]:.0%} CPU'
    )

    emit_normal(message)

    wsgi_app = self._detect_wsgi_application()

    if not wsgi_app:
        message = 'Could not detect WSGI_APPLICATION from Django settings.'
        emit_error(message)

        return

    runner = self.create_gunicorn_runner(port=port, wsgi_app=wsgi_app)
    runner.run_and_wait()

run_gunicorn_server

A method that runs a Gunicorn server without resource constraints.

Parameters:

  • port (int) –

    The port number to bind to.

Source code in dev_tool/services/django/service.py
@is_django_environment_variables
def run_gunicorn_server(self, port: int) -> None:
    """
    A method that runs a Gunicorn server without resource constraints.

    :param port: The port number to bind to.
    """

    self._ensure_gunicorn()

    wsgi_app = self._detect_wsgi_application()

    if not wsgi_app:
        message = 'Could not detect WSGI_APPLICATION from Django settings.'
        emit_error(message)

        return

    runner = self.create_gunicorn_runner(port=port, wsgi_app=wsgi_app)
    runner.run_and_wait()

run_django_create_superuser

A method that creates default Django superuser accounts, or resets them to an active superuser with the default password if they exist.

This method requires Django environment variables to be properly set.

Source code in dev_tool/services/django/service.py
@is_django_environment_variables
def run_django_create_superuser(self) -> None:
    """
    A method that creates default Django superuser accounts, or resets
    them to an active superuser with the default password if they exist.

    This method requires Django environment variables to be properly set.
    """

    superusers = [
        {
            'username': 'stratus',
            'email': '[email protected]',
            'password': 'stratus',
            'first_name': 'Stratus',
            'last_name': 'Adv'
        },
    ]

    for superuser in superusers:
        username = superuser['username']
        email = superuser['email']
        password = superuser['password']
        first_name = superuser.get('first_name', '')
        last_name = superuser.get('last_name', '')

        script = self._load_python_template(
            'ensure_superuser.py',
            username=username,
            email=email,
            password=password,
            first_name=first_name,
            last_name=last_name,
        )

        result = self.strategy.run_shell_script(script, check=False)

        if result.returncode != 0:
            message = f'Failed to create or reset superuser "{username}"'
            raise DjangoSuperuserError(message)

        message = f'Superuser "{username}" created or reset.'
        emit_normal(message)

        log.debug(message)

run_django_create_test_user

A method that creates a normal Django user account.

Source code in dev_tool/services/django/service.py
@is_django_environment_variables
def run_django_create_test_user(self) -> None:
    """A method that creates a normal Django user account."""

    users = [
        {
            'username': 'test',
            'first_name': 'Bobert',
            'last_name': 'Bobertson',
            'email': '[email protected]',
            'password': 'test',
        },
    ]

    for user_data in users:
        username = user_data['username']
        first_name = user_data['first_name']
        last_name = user_data['last_name']

        script = self._load_python_template(
            'create_user.py',
            username=username,
            first_name=first_name,
            last_name=last_name,
            email=user_data['email'],
            password=user_data['password']
        )

        try:
            self.strategy.run_shell_script(script, check=True)

            message = f'User "{username}" created successfully.'
            emit_normal(message)

            log.debug(message)
        except subprocess.CalledProcessError as exception:
            message = f'Failed to create user "{username}"'
            log.exception(message)

            raise DjangoCommandError(message) from exception

run_django_create_user

A method that creates superusers and test users.

Source code in dev_tool/services/django/service.py
def run_django_create_user(self) -> None:
    """A method that creates superusers and test users."""

    self.run_django_create_superuser()
    self.run_django_create_test_user()

run_django_flush

A method that flushes the Django database.

Source code in dev_tool/services/django/service.py
@is_django_environment_variables
def run_django_flush(self) -> None:
    """A method that flushes the Django database."""

    message = 'Flushing the database...'
    emit_normal(message)

    log.debug(message)

    try:
        self.strategy.run_django_command(['flush', '--no-input'], check=True)

        message = 'Database flushed successfully.'
        emit_normal(message)

        log.debug(message)
    except subprocess.CalledProcessError as exception:
        message = 'Failed to flush the database'
        log.exception(message)

        raise DjangoCommandError(message) from exception

run_django_interactive_shell

A method that runs an interactive Django command prompt.

Source code in dev_tool/services/django/service.py
@is_django_environment_variables
def run_django_interactive_shell(self) -> None:
    """A method that runs an interactive Django command prompt."""

    user_input = input('Enter your Django command: ').strip()
    args = user_input.split()

    if not args:
        message = 'No command entered. Please try again.'
        emit_warning(message)

        log.debug(message)
        return

    self.run_django_management_command(args)

run_django_make_migrations

A method that creates new Django migrations.

Source code in dev_tool/services/django/service.py
@is_django_environment_variables
def run_django_make_migrations(self) -> None:
    """A method that creates new Django migrations."""

    message = 'Creating migrations...'
    emit_normal(message)

    log.debug(message)

    try:
        self.strategy.run_django_command(['makemigrations'], check=True)

        message = 'Migrations created successfully.'
        emit_normal(message)

        log.debug(message)
    except subprocess.CalledProcessError as exception:
        message = 'Failed to create migrations'
        log.exception(message)

        raise DjangoMigrationError(message) from exception

run_django_management_command

A method that runs a Django management command.

Parameters:

  • args (list[str]) –

    The command arguments.

Source code in dev_tool/services/django/service.py
@is_django_environment_variables
def run_django_management_command(self, args: list[str]) -> None:
    """
    A method that runs a Django management command.

    :param args: The command arguments.
    """

    try:
        self.strategy.run_django_command(args, check=True)
    except subprocess.CalledProcessError as exception:
        message = f'Django command failed: {" ".join(args)}'
        log.exception(message)

        raise DjangoCommandError(message) from exception

run_django_migrate

A method that applies Django migrations.

Parameters:

  • apps (str) –

    The specific apps to migrate, or empty string for all apps.

Source code in dev_tool/services/django/service.py
@is_django_environment_variables
def run_django_migrate(self, apps: str) -> None:
    """
    A method that applies Django migrations.

    :param apps: The specific apps to migrate, or empty string for all apps.
    """

    command = ['migrate']

    if apps:
        message = f'Running migrations for {apps}'
        emit_normal(message)

        log.debug(message)
        command.append(apps)
    else:
        message = 'Running migrations for default database.'
        emit_normal(message)

        log.debug(message)

    try:
        self.strategy.run_django_command(command, check=True)
    except subprocess.CalledProcessError as exception:
        apps = apps or 'all apps'

        message = f'Failed to run Django migrations for {apps}'
        log.exception(message)

        raise DjangoMigrationError(message) from exception

run_django_seeding

A method that runs database seeding for Django.

Source code in dev_tool/services/django/service.py
@is_django_environment_variables
def run_django_seeding(self) -> None:
    """A method that runs database seeding for Django."""

    seed_script = BASE / 'seed.py'

    if seed_script.exists():
        message = 'Running seed script...'
        emit_normal(message)

        log.debug(message)

        try:
            self.strategy.run_seed(seed_script)
        except subprocess.CalledProcessError as exception:
            message = 'Django seeding script failed'
            log.exception(message)

            raise DjangoSeedError(message) from exception
    else:
        message = 'No seed script found.'
        emit_warning(message)

        log.debug(message)

run_django_server

A method that starts a Django development server.

Parameters:

  • ip_address (str) –

    The IP address to bind the server to.

  • port (int) –

    The port number to bind the server to.

Source code in dev_tool/services/django/service.py
@is_django_environment_variables
def run_django_server(self, ip_address: str, port: int) -> None:
    """
    A method that starts a Django development server.

    :param ip_address: The IP address to bind the server to.
    :param port: The port number to bind the server to.
    """

    try:
        self.create_runner(ip_address=ip_address, port=port).run_and_wait()
    except Exception as exception:
        message = 'Failed to start Django server'
        log.exception(message)

        raise DjangoServerError(message) from exception