Skip to content

home

dev_tool.commands.home

log = logging.getLogger(__name__) module-attribute

HomeCommandGroup

Bases: CommandGroup

A command group for the most common development commands.

This class provides quick access to the most frequently used commands across Django, Python, and database operations.

The constructor for the HomeCommandGroup class.

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

Parameters:

  • bun (BunService) –

    The Bun service for Bun operations.

  • django (DjangoService) –

    The Django service for Django operations.

  • docker (DockerService) –

    The Docker service for container management.

  • portal (PortalService) –

    The Portal service for API integration.

  • postgres (PostgresService) –

    The PostgreSQL service for database operations.

  • python (PythonService) –

    The Python service for Python operations.

Source code in dev_tool/commands/home.py
def __init__(
    self,
    bun: BunService,
    django: DjangoService,
    docker: DockerService,
    portal: PortalService,
    postgres: PostgresService,
    python: PythonService
) -> None:
    """
    The constructor for the HomeCommandGroup class.

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

    :param bun: The Bun service for Bun operations.
    :param django: The Django service for Django operations.
    :param docker: The Docker service for container management.
    :param portal: The Portal service for API integration.
    :param postgres: The PostgreSQL service for database operations.
    :param python: The Python service for Python operations.
    """

    super().__init__()

    self.bun = bun
    self.django = django
    self.docker = docker
    self.portal = portal
    self.postgres = postgres
    self.python = python

bun = bun instance-attribute

django = django instance-attribute

docker = docker instance-attribute

portal = portal instance-attribute

postgres = postgres instance-attribute

python = python 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/home.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 'Home'

run_server

A command that starts the development environment.

Source code in dev_tool/commands/home.py
@ordered_command(label='Run server on localhost')
def run_server(self) -> None:
    """A command that starts the development environment."""

    try:
        self.docker.ensure_container(recreate=False)
    except Exception as e:
        self.emit_error(f'Failed to ensure Docker container: {e}')
        return

    orchestrator = DevelopmentRunner()

    django = self.django.create_runner(ip_address='127.0.0.1', port=8000)
    orchestrator.add_runner(django)

    bun = self.bun.create_runner()

    if bun:
        orchestrator.add_runner(bun)

    orchestrator.run()

rebuild_environment

A method that (re)builds the development environment.

This method sets up environment variables, rebuilds the Python virtual environment, ensures database containers are running, and configures the database with optional seeding and dumping.

Source code in dev_tool/commands/home.py
@ordered_command(label='(Re)build the development environment')
def rebuild_environment(self) -> None:
    """
    A method that (re)builds the development environment.

    This method sets up environment variables, rebuilds the Python
    virtual environment, ensures database containers are running,
    and configures the database with optional seeding and dumping.
    """

    seeding = get_user_confirmation('Do you want to run database seeding?')
    superuser = get_user_confirmation('Do you want to generate superuser(s)?')

    redump = False
    reuse = False

    if not seeding and is_dump_environment_variables():
        redump = get_user_confirmation('Do you want (re)dump the database?')

        if not redump and self.postgres.is_dump_file():
            reuse = get_user_confirmation('A dump file exists. Do you want to reuse this file?')

    apps = get_user_input('Which app(s) do you want to migrate? Leave blank for default migrations.')

    try:
        self.python.clean_virtual_environment()
    except Exception as e:
        self.emit_error(f'Failed to clean virtual environment: {e}')
        return

    try:
        self.docker.ensure_container(recreate=True)
    except Exception as e:
        self.emit_error(f'Failed to ensure Docker container: {e}')
        return

    try:
        self.postgres.create_postgres_superuser()
    except Exception as e:
        self.emit_error(f'Failed to create PostgreSQL superuser: {e}')
        return

    try:
        self.postgres.setup_django_databases()
    except Exception as e:
        self.emit_error(f'Failed to setup Django databases: {e}')
        return

    try:
        self.python.setup_venv()
    except Exception as e:
        self.emit_error(f'Failed to setup virtual environment: {e}')
        return

    try:
        self.django.run_django_migrate(apps)
    except Exception as e:
        self.emit_error(f'Failed to run Django migrations: {e}')
        return

    if seeding:
        try:
            self.django.run_django_seeding()
        except Exception as e:
            self.emit_error(f'Failed to run Django seeding: {e}')
            return

    if superuser:
        try:
            self.django.run_django_create_user()
        except Exception as e:
            self.emit_error(f'Failed to create Django superuser: {e}')
            return

    if not seeding and is_dump_environment_variables():
        try:
            self.postgres.dump_cloud_to_docker_database(redump, reuse)
        except Exception as e:
            self.emit_error(f'Failed to dump cloud database to Docker: {e}')

run_migrations

A command that applies Django migrations.

This command ensures the database container is running and then runs the Django migrate commands for specified apps.

Source code in dev_tool/commands/home.py
@ordered_command(label='Run Django migrations')
def run_migrations(self) -> None:
    """
    A command that applies Django migrations.

    This command ensures the database container is running and then
    runs the Django migrate commands for specified apps.
    """

    try:
        self.docker.ensure_container(recreate=False)
    except Exception as e:
        self.emit_error(f'Failed to ensure Docker container: {e}')
        return

    try:
        self.django.run_django_make_migrations()
    except Exception as e:
        self.emit_error(f'Failed to create Django migrations: {e}')
        return

    apps = get_user_input('Which app(s) do you want to migrate? Leave blank for default migrations.')

    try:
        self.django.run_django_migrate(apps)
    except Exception as e:
        self.emit_error(f'Failed to run Django migrations: {e}')

install_dependencies

A command that installs and upgrades Python dependencies.

This command delegates to the Python service to install or upgrade the project dependencies from pyproject.toml.

Source code in dev_tool/commands/home.py
@ordered_command(label='Install and upgrade dependencies')
def install_dependencies(self) -> None:
    """
    A command that installs and upgrades Python dependencies.

    This command delegates to the Python service to install or upgrade
    the project dependencies from pyproject.toml.
    """

    try:
        self.python.install_dependencies()
    except Exception as e:
        self.emit_error(f'Failed to install dependencies: {e}')

flush_and_seed

A command that flushes and seeds the database.

This method flushes all data from the database and then runs the Django seeding process, optionally creating superusers afterward.

Source code in dev_tool/commands/home.py
@ordered_command(label='Flush and seed the database')
def flush_and_seed(self) -> None:
    """
    A command that flushes and seeds the database.

    This method flushes all data from the database and then runs the Django
    seeding process, optionally creating superusers afterward.
    """

    seeding = get_user_confirmation('Are you sure you want to flush and seed the database?')
    superuser = get_user_confirmation('Do you want to generate superuser(s)?')

    if seeding:
        try:
            self.docker.ensure_container(recreate=False)
        except Exception as e:
            self.emit_error(f'Failed to ensure Docker container: {e}')
            return

        try:
            self.django.run_django_flush()
        except Exception as e:
            self.emit_error(f'Failed to flush Django database: {e}')
            return

        try:
            self.django.run_django_seeding()
        except Exception as e:
            self.emit_error(f'Failed to run Django seeding: {e}')
            return

    if superuser:
        try:
            self.django.run_django_create_user()
        except Exception as e:
            self.emit_error(f'Failed to create Django superuser: {e}')

download_environment_variables

A command that downloads environment variables for the current project.

This method uses the project name from the project's pyproject.toml and downloads the environment variables from the portal as a background task.

Source code in dev_tool/commands/home.py
@ordered_command(label='Download latest environment variables')
def download_environment_variables(self) -> None:
    """
    A command that downloads environment variables for the current project.

    This method uses the project name from the project's pyproject.toml and
    downloads the environment variables from the portal as a background task.
    """

    project = self.configuration.get_project_name()

    if project == 'project':
        message = 'Project name is set to default "project". Please update your pyproject.toml.'
        self.emit_error(message)

        return

    try:
        self.portal.download_environment_variables(project)
    except Exception as e:
        self.emit_error(f'Failed to download environment variables: {e}')