Apps

Every Smarter Django app’s AppConfig subclasses SmarterAppConfig. An app’s apps.py declares only its dotted name and its name in the Django admin, both from its const.py, and, for a SAM resource app, its feature flag. The app’s presentation_app_name is marked with gettext_noop in const.py, and translated where it is shown, so the Django admin shows the app’s name in the user’s language.

from django.utils.text import format_lazy
from django.utils.translation import gettext_lazy as _

from smarter.common.conf import smarter_settings
from smarter.lib.django.apps import SmarterAppConfig

from .const import namespace, presentation_app_name


class GuardrailConfig(SmarterAppConfig):
    name = f"smarter.apps.{namespace}"
    verbose_name = format_lazy("{} {}", smarter_settings.platform_branding_app_name, _(presentation_app_name))
    enabled_setting = "app_guardrail_enable"

Its ready() imports the app’s receivers and signals modules, or the ready_modules that the app names, and logs that the app is ready. If the app’s feature flag is false, it imports nothing, so none of its receivers are connected, and logs the environment variable that turns it on. An app that has more to do once it is ready overrides on_ready(). See Resource Types’ Feature Flags.

The base class of every Smarter Django app’s AppConfig.

Each app’s apps.py declares what is particular to it, and SmarterAppConfig does the rest:

class GuardrailConfig(SmarterAppConfig):
    name = "smarter.apps.guardrail"
    verbose_name = format_lazy("{} {}", smarter_settings.platform_branding_app_name, _("Guardrail"))
    enabled_setting = "app_guardrail_enable"

Its ready() imports the app’s ready_modules, which connects their signal receivers, and logs that the app is ready. A SAM resource app whose feature flag is false, e.g. SMARTER_APP_GUARDRAIL_ENABLE=false, imports nothing and logs how to enable it. See smarter.lib.manifest.sam_apps.

class smarter.lib.django.apps.SmarterAppConfig(app_name, app_module)[source]

A Smarter app’s configuration: the modules that its ready() imports, and its feature flag.

It doesn’t mix in SmarterHelperMixin, whose ready property would conflict with ready().

default = False

Django uses an apps.py’s AppConfig only if it holds exactly one, not counting those whose default is.

False. An app’s apps.py imports this class too, so it isn’t a default, and each subclass is (__init_subclass__()).

enabled_setting: str | None = None

The smarter_settings flag that enables a SAM resource app, e.g. app_guardrail_enable, or None for a core app,.

which is always enabled.

is_enabled()[source]

Whether the app is enabled.

Return type:

bool

Returns:

The value of the app’s feature flag, or True for a core app, which has none.

on_ready()[source]

Do what is particular to the app, after its modules are imported.

Does nothing, unless overridden.

Return type:

None

ready()[source]

Import the app’s modules, which connects their signal receivers, unless its feature flag is false.

Return type:

None

ready_module_name(module)[source]

Return the dotted path of one of ready_modules.

Parameters:

module (str) – The module’s name, e.g. receivers or smarter.lib.django.checks.

Return type:

str

Returns:

Its dotted path, e.g. smarter.apps.guardrail.receivers.

ready_modules: tuple[str, ...] = ('receivers', 'signals')

The modules that ready() imports, which connects their signal receivers.

A name is relative to the app’s package, e.g. receivers, unless it is a dotted path from smarter., e.g. smarter.lib.django.checks.