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:

  • 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,
    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 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.django = django
    self.docker = docker
    self.portal = portal
    self.postgres = postgres
    self.python = python

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 Django development server on localhost.

This command ensures the database container is running and then starts the Django development server on 127.0.0.1:8000.

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 Django development server on localhost.

    This command ensures the database container is running and then
    starts the Django development server on 127.0.0.1:8000.
    """

    self.docker.ensure_container(recreate=False)
    self.django.run_django_server(ip_address='127.0.0.1', port=8000)

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)?')

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

        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.')

    self.python.clean_virtual_environment()
    self.docker.ensure_container(recreate=True)
    self.postgres.create_postgres_superuser()

    self.postgres.setup_django_databases()
    self.python.setup_venv()
    self.django.run_django_migrate(apps)

    if seeding:
        self.django.run_django_seeding()

    if superuser:
        self.django.run_django_create_user()

    if not seeding and is_dump_environment_variables():
        self.postgres.dump_cloud_to_docker_database(redump, reuse)

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.
    """

    self.docker.ensure_container(recreate=False)
    self.django.run_django_make_migrations()

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

install_dependencies

A command that installs and upgrades Python dependencies.

This command runs the Python service's install_dependencies method to update all dependencies defined in requirements files.

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 runs the Python service's install_dependencies method
    to update all dependencies defined in requirements files.
    """

    self.python.install_dependencies()

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:
        self.docker.ensure_container(recreate=False)
        self.django.run_django_flush()
        self.django.run_django_seeding()

    if superuser:
        self.django.run_django_create_user()

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 = CONTEXT.configuration.get_project_name()

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

        log.debug(message)

        return

    self.portal.download_environment_variables(project)