Management Commands

Smarter subclasses Django’s management command framework in order to provide more consistent console output and app logging. SmarterCommand is the base class for all management commands in Smarter, and serves as a drop-in replacement for Django’s base command class.

For more details on the original Django management command interface, see the django.core.management.base.BaseCommand documentation.

Example

from smarter.lib.django.management.base import SmarterCommand

class Command(SmarterCommand):
    help = "My custom management command."

    def handle(self, *args, **options):
        self.stdout.write(self.style.SUCCESS("Hello from SmarterCommand!"))
class smarter.lib.django.management.base.SmarterCommand(*args, **kwargs)[source]

Bases: BaseCommand

Base class for custom Django management commands in the Smarter framework.

This class extends Django’s BaseCommand to provide a standardized interface and additional helper methods for writing robust, user-friendly management commands. It is intended to be subclassed by all custom management commands in the Smarter project.

Features:

  • Standardized output formatting for command start, success, and failure.

  • Optional display of Django settings at the start of command execution.

  • Enhanced error handling with clear messaging and exit codes.

  • Extensible argument parsing via create_parser.

Parameters:

All parameters accepted by Django’s BaseCommand are supported.

Command-Line Options:

  • --settings_output (bool): If specified, outputs Django settings at the beginning of the command’s console output.

Methods:

  • handle_begin(): Prints a formatted header indicating the start of the command.

  • handle_completed_success(msg: Optional[str] = None): Prints a formatted success message. If msg is provided, it is displayed; otherwise, a default message is shown.

  • handle_completed_failure(err: Optional[Exception] = None, msg: Optional[str] = None): Prints a formatted error message. If err is provided, the error details are included and the process exits with code 1.

  • create_parser(prog_name, subcommand, **kwargs): Extends the default argument parser to include the --settings_output option.

  • handle(*args, **options): Abstract method to be implemented by subclasses. Contains the main logic for the command.

Example Usage:

from smarter.smarter.lib.django.management.base import SmarterCommand

class MyCommand(SmarterCommand):
    help = "My custom command."

    def handle(self, *args, **options):
        self.handle_begin()
        # Command logic here
        self.handle_completed_success("MyCommand finished successfully.")

Notes:

  • Subclasses must implement the handle method.

  • Use the provided helper methods to ensure consistent output and error handling.

Warning:

  • If handle_completed_failure is called with an exception, the process will exit with code 1.

  • Do not override __init__ unless necessary; always call super().__init__.

__init__(*args, **kwargs)[source]
create_parser(prog_name, subcommand, **kwargs)[source]

Create and return the ArgumentParser which will be used to parse the arguments to this command.

handle(*args, **options)[source]

Handle the command execution.

handle_begin()[source]
handle_completed_failure(err=None, msg=None)[source]
handle_completed_success(msg=None)[source]