PluginController Model

class smarter.apps.plugin.manifest.controller.PluginController(account, user, *args, user_profile=None, manifest=None, plugin_meta=None, name=None, **kwargs)[source]

Bases: AbstractController

Provides a unified interface for mapping between Pydantic manifest models, plugin implementations, and Django ORM models within the Smarter platform.

The PluginController is responsible for orchestrating the instantiation and management of plugin objects based on manifest data, plugin metadata, or plugin names. It supports dynamic loading of plugin classes, validation of manifest kinds, and ensures that only valid plugin configurations are accepted. This controller acts as a bridge between the declarative plugin manifests (often defined in YAML or JSON), the underlying plugin Python classes, and the persistent plugin metadata stored in the database.

Key Responsibilities

  • Validates and processes plugin manifest data, ensuring compatibility with supported plugin kinds.

  • Dynamically selects and instantiates the appropriate plugin class (API, SQL, or Static) based on manifest or metadata.

  • Maintains references to the manifest, plugin instance, and plugin metadata for coordinated access.

  • Integrates with user and account context to support multi-tenant plugin management.

  • Provides error handling for invalid or ambiguous plugin initialization scenarios.

Model Relationships

Usage Example

# Initialize a PluginController with manifest data
my_user_profile = UserProfile.get_cached_object(user=admin_user)
controller = PluginController(
    account=my_account,
    user=admin_user,
    manifest=my_manifest,
    user_profile=my_user_profile
)
plugin_instance = controller.plugin

# Initialize with plugin metadata
my_plugin_meta = PluginMeta.objects.get(id=plugin_id)
controller = PluginController(
    account=my_account,
    user=admin_user,
    plugin_meta=my_plugin_meta,
    user_profile=my_user_profile
)
plugin_instance = controller.plugin

Notes

  • Only one of manifest, plugin_meta, or name should be provided during initialization.

  • The controller enforces validation of manifest kinds and plugin class compatibility.

  • Logging and error handling are integrated using the Smarter platform’s logging and exception infrastructure.

__init__(account, user, *args, user_profile=None, manifest=None, plugin_meta=None, name=None, **kwargs)[source]
property account: Account | None

Returns the account for the current user. Handle lazy instantiation from user or user_profile.

Returns:

The account for the current user.

Return type:

Account or None

property account_mixin_logger_prefix: str

Returns the logger prefix for the class.

property account_number: str | None

A helper function to get the account number from the account.

Returns:

The account number for the current account.

Return type:

str or None

property accountmixin_ready_state: str

Returns a string representation of the AccountMixin ready state.

Returns:

String representation of the AccountMixin ready state.

Return type:

str

property amnesty_urls: list[str]

Returns a list of URLs that are exempt from certain checks.

This is a placeholder and should be overridden in subclasses.

Returns:

List of URL path strings that are exempt.

Return type:

list[str]

authenticate(api_token)

Authenticate the user using the provided API token. The api_token will have been generated by the SmarterTokenAuthentication class and passed by the caller in the Authorization header of the request.

Example

Authorization: Token <api_token>

Parameters:

api_token (bytes) – The API token to authenticate with.

Returns:

True if authentication was successful, False otherwise.

Return type:

bool

data_to_dict(data)

Converts data to a dictionary, handling different types of input.

This method accepts either a dictionary or a string. If a string is provided, it will attempt to parse it as JSON first, and if that fails, as YAML. If parsing fails or the data type is unsupported, a SmarterValueError is raised.

Parameters:

data (dict or str) – The data to convert, either a dict or a JSON/YAML string.

Returns:

The data as a dictionary.

Return type:

dict

Raises:

SmarterValueError – If the data cannot be converted to a dictionary.

property formatted_class_name: str

Returns the class name in a formatted string along with the name of this mixin.

property formatted_state_not_ready: str

Returns the not-ready state formatted for logging.

Returns:

The formatted not-ready state as a string.

Return type:

str

property formatted_state_ready: str

Returns the readiness state formatted for logging.

Returns:

The formatted readiness state as a string.

Return type:

str

get_model_titles()[source]
Return type:

list[dict[str, str]]

property is_accountmixin_ready: bool

Returns True if the AccountMixin is ready to be used. This is a convenience property that checks if the account and user are initialized. AccountMixin is considered ready if: - self.user is an instance of User - self.user_profile is an instance of UserProfile - self.account is an instance of Account

Returns:

True if the AccountMixin is ready to be used.

Return type:

bool

property is_authenticated: bool

Returns True if the user is authenticated and is associated with an Account.

log_account_mixin_ready_status()

Logs the ready status of the AccountMixin.

property manifest: SAMPluginCommon | None
property map: Dict[str, type[ApiPlugin] | type[SqlPlugin] | type[StaticPlugin]][source]
mask_string(string='', mask_char='*', mask_length=4, string_length=8)

Masks a string for secure logging.

This utility function masks all but the last unmasked_chars characters of the input string, replacing them with asterisks. It is useful for logging sensitive information like API keys or passwords.

Parameters:
  • string (str) – The string to be masked.

  • mask_char (str) – The character used for masking.

  • mask_length (int) – The number of characters to mask.

  • string_length (int) – The length of the string to consider for masking.

Returns:

The masked string.

Return type:

str

model_dump_json()[source]
Return type:

Optional[dict]

property name: str | None
property obj: StaticPlugin | SqlPlugin | ApiPlugin | None
property plugin: StaticPlugin | SqlPlugin | ApiPlugin | None
property plugin_class: str | None

Returns the plugin class based on the manifest kind.

property plugin_meta: PluginMeta | None
property plugin_meta_class_map: Dict[str, type[ApiPlugin] | type[SqlPlugin] | type[StaticPlugin]][source]
property ready: bool

Returns True if the account and user are set.

property ready_state: str

Returns a string representation of the ready state.

property sam_map: Dict[str, type[SAMApiPlugin] | type[SAMSqlPlugin] | type[SAMStaticPlugin]][source]

Maps manifest kinds to their respective SAM plugin classes.

smarter_build_absolute_uri(request)

Attempts to get the absolute URI from a request object.

This utility function tries to retrieve the request URL from any valid child class of django.http.HttpRequest. It is especially useful in unit tests or scenarios where the request object may not implement build_absolute_uri().

Parameters:

request (Optional[HttpRequest]) – The request object.

Returns:

The absolute request URL.

Return type:

Optional[str]

Raises:

SmarterValueError – If the URI cannot be built from the request.

sorted_dict(data)

Returns a new dictionary with keys sorted.

Parameters:

data (dict) – The dictionary to sort.

Returns:

A new dictionary with sorted keys.

Return type:

dict

to_json()

Returns a JSON representation of the account, user, and user_profile.

property unformatted_class_name: str

Returns the raw class name without formatting.

Returns:

The unformatted class name as a string.

Return type:

str

This is useful for logging or serialization where the plain class name is needed.

property user: AnonymousUser | User | None

Returns the user for the current user. Handle lazy instantiation from user_profile or account.

Returns:

The user for the current user.

Return type:

User or None

property user_profile: UserProfile | None

Returns the user_profile for the current user. Handle lazy instantiation from user or account.

Returns:

The user_profile for the current user.

Return type:

UserProfile or None