Skip to content

django

dev_tool.commands.django

log = logging.getLogger(__name__) module-attribute

DjangoCommandGroup

Bases: CommandGroup

A command group for Django operations.

This class provides commands for running Django servers, managing migrations, creating superusers, and executing tests and coverage analysis.

The constructor for the DjangoCommandGroup class.

This method initializes the command group with required services and registers all command methods.

Parameters:

  • coverage (CoverageService) –

    The coverage service for code coverage analysis.

  • django (DjangoService) –

    The Django service for Django operations.

  • docker (DockerService) –

    The Docker service for container management.

  • postgres (PostgresService) –

    The PostgreSQL service for database operations.

  • profiling (ProfilingService) –

    The Profiling service for testing performance.

  • unittest (UnitTestService) –

    The unittest service for running tests.

Source code in dev_tool/commands/django.py
def __init__(
    self,
    coverage: CoverageService,
    django: DjangoService,
    docker: DockerService,
    postgres: PostgresService,
    profiling: ProfilingService,
    unittest: UnitTestService
) -> None:
    """
    The constructor for the DjangoCommandGroup class.

    This method initializes the command group with required services
    and registers all command methods.

    :param coverage: The coverage service for code coverage analysis.
    :param django: The Django service for Django operations.
    :param docker: The Docker service for container management.
    :param postgres: The PostgreSQL service for database operations.
    :param profiling: The Profiling service for testing performance.
    :param unittest: The unittest service for running tests.
    """

    super().__init__()

    self.coverage = coverage
    self.django = django
    self.docker = docker
    self.postgres = postgres
    self.profiling = profiling
    self.unittest = unittest

coverage = coverage instance-attribute

django = django instance-attribute

docker = docker instance-attribute

postgres = postgres instance-attribute

profiling = profiling instance-attribute

unittest = unittest instance-attribute

__str__

The string representation of the command group.

This method returns the category name for the command group.

Returns:

  • str

    The category name as a string.

Source code in dev_tool/commands/django.py
def __str__(self) -> str:
    """
    The string representation of the command group.

    This method returns the category name for the command group.

    :return: The category name as a string.
    """

    return 'Django'

run_server

A command that opens a submenu for server options.

This method returns a menu for selecting different server configurations.

Returns:

  • Menu

    A submenu for selecting server options.

Source code in dev_tool/commands/django.py
@ordered_submenu(label='Run a Django server')
def run_server(self) -> Menu:
    """
    A command that opens a submenu for server options.

    This method returns a menu for selecting different server configurations.

    :return: A submenu for selecting server options.
    """

    items: list[Menu | MenuItem] = [
        MenuItem(
            label='Run on localhost',
            action=CommandAction(command=self._run_localhost)
        ),
        MenuItem(
            label='Run on localhost with custom port',
            action=CommandAction(command=self._run_custom_server)
        ),
        MenuItem(
            label='Run on localhost with profiling',
            action=CommandAction(command=self._run_localhost_with_profiling)
        ),
        MenuItem(
            label='Run on local IP',
            action=CommandAction(command=self._run_local_ip)
        )
    ]

    return Menu(title='Run a Django server', items=items)

run_command

A command that opens a submenu for common Django commands.

This method returns a menu for frequently used Django management commands.

Returns:

  • Menu

    A submenu for Django commands.

Source code in dev_tool/commands/django.py
@ordered_submenu(label='Run a Django command')
def run_command(self) -> Menu:
    """
    A command that opens a submenu for common Django commands.

    This method returns a menu for frequently used Django management commands.

    :return: A submenu for Django commands.
    """

    items: list[Menu | MenuItem] = [
        MenuItem(
            label='makemigrations',
            action=CommandAction(command=self._make_migrations)
        ),
        MenuItem(
            label='migrate',
            action=CommandAction(command=self._run_migrations)
        ),
        MenuItem(
            label='spire_startapp',
            action=CommandAction(command=self._spire_startapp)
        ),
        MenuItem(
            label='createsuperuser',
            action=CommandAction(command=self._create_superuser)
        ),
        MenuItem(
            label='check',
            action=CommandAction(command=self._check)
        ),
        MenuItem(
            label='shell',
            action=CommandAction(command=self._shell)
        ),
        MenuItem(
            label='showmigrations',
            action=CommandAction(command=self._showmigrations)
        ),
        MenuItem(
            label='custom',
            action=CommandAction(command=self._run_custom_command)
        ),
    ]

    return Menu(title='Run a Django command', items=items)

testing

A command that opens a submenu for testing options.

This method returns a menu for test-related commands.

Returns:

  • Menu

    A submenu for testing options.

Source code in dev_tool/commands/django.py
@ordered_submenu(label='Test a Django project')
def testing(self) -> Menu:
    """
    A command that opens a submenu for testing options.

    This method returns a menu for test-related commands.

    :return: A submenu for testing options.
    """

    items: list[Menu | MenuItem] = [
        MenuItem(
            label='Run unit tests',
            action=CommandAction(command=self._django_unittest)
        ),
        MenuItem(
            label='Run coverage',
            action=CommandAction(command=self._django_coverage)
        )
    ]

    return Menu(title='Test a Django project', items=items)