Account

Smarter API Account Manifest handler

class smarter.apps.account.manifest.brokers.account.AccountSerializer(*args, **kwargs)[source]

Bases: ModelSerializer

Account serializer for smarter api.

class smarter.apps.account.manifest.brokers.account.SAMAccountBroker(*args, **kwargs)[source]

Bases: AbstractBroker

Handles Smarter API Account Manifest operations, including loading, validating, and parsing YAML manifests, and mapping them to Django ORM and Pydantic models. This broker transforms between Django ORM and Pydantic models, ensuring data consistency for serialization and API responses.

This broker is responsible for:
  • Loading and validating Smarter API Account manifests.

  • Initializing the corresponding Pydantic model from manifest data.

  • Creating, updating, deleting, and querying Django ORM models representing account manifests.

  • Transforming Django ORM models into Pydantic models for serialization and deserialization.

Parameters:
  • _manifest (Optional[SAMAccount]) – The current manifest instance (SAMAccount), if loaded.

  • _pydantic_model (Type[SAMAccount]) – The Pydantic model class used for manifests.

  • _account (Optional[Account]) – The Django ORM Account instance associated with the manifest.

Note

The manifest must be explicitly initialized with manifest data, typically using **data from the manifest loader.

Warning

If the manifest loader or manifest metadata is missing, or if the account is not set, the manifest will not be initialized and may return None or raise an exception.

Example usage:

broker = SAMAccountBroker()
manifest = broker.manifest
if manifest:
    print(manifest.apiVersion, manifest.kind)

See also

  • SAMAccount

  • Account

  • SAMAccountMetadata

  • SAMAccountSpec

Added in version 1.0.0: Initial implementation of the Smarter API Account Manifest Broker.

property ORMMetaModelClass: Type[Account]

Return the Django ORM meta model class for the broker.

Returns:

The Django ORM meta model class definition for the broker.

Return type:

Type[Account]

property ORMModelClass: Type[Account]

Get the Django ORM model class for the Smarter API Account.

Returns:

The Django ORM Account model class.

Return type:

Type[Account]

property SAMModelClass: Type[SAMAccount]

Return the Pydantic model class for the broker.

Returns:

The Pydantic model class definition for the broker.

Return type:

Type[SAMAccount]

property SerializerClass: Type[AccountSerializer]

Get the Django REST Framework serializer class for the Smarter API Account.

Returns:

The AccountSerializer class.

Return type:

Type[ModelSerializer]

__init__(*args, **kwargs)[source]

Initialize the SAMAccountBroker instance.

This constructor initializes the broker by calling the parent class’s constructor, which will attempt to bootstrap the class instance with any combination of raw manifest data (in JSON or YAML format), a manifest loader, or existing Django ORM models. If a manifest loader is provided and its kind matches the expected kind for this broker, the manifest is initialized using the loader’s data.

This class can bootstrap itself in any of the following ways:

  • request.body (yaml or json string)

  • name + account (determined via authentication of the request object)

  • SAMLoader instance

  • manifest instance

  • filepath to a manifest file

If raw manifest data is provided, whether as a string or a dictionary, or a SAMLoader instance, the base class constructor will only goes as far as initializing the loader. The actual manifest model initialization is deferred to this constructor, which checks the loader’s kind.

Parameters:
  • args – Positional arguments passed to the parent constructor.

  • kwargs – Keyword arguments passed to the parent constructor.

Example:

broker = SAMAccountBroker(loader=loader, plugin_meta=plugin_meta)
apply(request, *args, **kwargs)[source]

Applies the manifest by copying its data to the Django ORM Account model and saving the model to the database.

Note

tags are handled separately because they are of type TaggableManager and require a different method to set them.

Parameters:
  • request ("HttpRequest") – The HTTP request object.

  • args – Additional positional arguments.

  • kwargs – Additional keyword arguments.

Returns:

A JSON response indicating the result of the apply operation.

Return type:

SmarterJournaledJsonResponse

Raises:

Important

Calls super().apply() to ensure the manifest is loaded and validated before applying changes.

Caution

Fields that are not editable (such as id, created_at, updated_at, and account_number) are removed from the data before saving.

Example usage:

response = broker.apply(request)
print(response.status_code, response.data)
property brokered_account: Account | None

In order to disambiguate between the AccountMixin.account (the authenticated account making the request) and the Account resource being brokered, we use the term “brokered_account”.

Get the Django ORM Account instance associated with this broker.

Returns:

The Account instance if set, otherwise None.

Return type:

Optional[Account]

cache_invalidations()[source]

Handle broker specific cache invalidation logic. Invalidates the cache for the Account and UserProfile models.

Return type:

None

chat(request, *args, **kwargs)[source]

Chat functionality is not implemented for the Smarter API Account.

Parameters:
  • request ("HttpRequest") – The HTTP request object.

  • args – Additional positional arguments.

  • kwargs – Additional keyword arguments.

Raises:

SAMBrokerErrorNotImplemented – Always raised to indicate that chat is not implemented.

Returns:

A JSON response indicating that chat is not implemented.

Return type:

SmarterJournaledJsonResponse

delete(request, *args, **kwargs)[source]

Attention

Delete functionality is not implemented for the Smarter API Account.

Parameters:
  • request ("HttpRequest") – The HTTP request object.

  • args – Additional positional arguments.

  • kwargs – Additional keyword arguments.

Raises:

SAMBrokerErrorNotImplemented – Always raised to indicate that delete is not implemented.

Returns:

A JSON response indicating that delete is not implemented.

Return type:

SmarterJournaledJsonResponse

deploy(request, *args, **kwargs)[source]

Attention

Deploy functionality is not implemented for the Smarter API Account.

Parameters:
  • request ("HttpRequest") – The HTTP request object.

  • args – Additional positional arguments.

  • kwargs – Additional keyword arguments.

Raises:

SAMBrokerErrorNotImplemented – Always raised to indicate that deploy is not implemented.

Returns:

A JSON response indicating that deploy is not implemented.

Return type:

SmarterJournaledJsonResponse

describe(request, *args, **kwargs)[source]

Describe the manifest for the Smarter API Account.

Parameters:
  • request ("HttpRequest") – The HTTP request object.

  • args – Additional positional arguments.

  • kwargs – Additional keyword arguments.

Raises:

SAMBrokerErrorNotReady – Raised when no account is found.

Returns:

A JSON response with the manifest description.

Return type:

SmarterJournaledJsonResponse

django_orm_to_manifest_dict()[source]

Converts a Django ORM Account model instance into a Pydantic-compatible Smarter API Account manifest dictionary.

Returns:

Dictionary formatted for Pydantic model consumption, suitable for serialization and API responses.

Return type:

dict

Raises:

Note

The output uses camelCase keys for compatibility with Pydantic models and API consumers.

Example usage:

manifest_dict = broker.django_orm_to_manifest_dict()
print(manifest_dict["apiVersion"], manifest_dict["kind"])

See also

Changed in version 1.0.0: Method now ensures camelCase conversion and excludes the primary key field.

example_manifest(request, *args, **kwargs)[source]

Return an example manifest for the Smarter API Account.

Returns:

A JSON response containing an example Smarter API Account manifest.

Return type:

SmarterJournaledJsonResponse

See also

  • SAMAccount

  • SAMAccountMetadata

  • SAMAccountSpec

  • SAMKeys

  • SAMMetadataKeys

  • SAMAccountSpecKeys

property formatted_class_name: str

Returns a formatted class name string for use in logging, providing a more readable identifier for this broker class.

Returns:

The formatted class name, including the parent class and SAMAccountBroker().

Return type:

str

Example usage:

logger.info(broker.formatted_class_name)
get(request, *args, **kwargs)[source]

get the manifest(s) for the Smarter API Account.

Parameters:
  • request ("HttpRequest") – The HTTP request object.

  • args – Additional positional arguments.

  • kwargs – Additional keyword arguments.

Returns:

A JSON response containing the Smarter API Account manifest(s).

Return type:

SmarterJournaledJsonResponse

Raises:
property kind: str

Get the manifest kind for the Smarter API Account.

Returns:

The manifest kind string for the Smarter API Account.

Return type:

str

logs(request, *args, **kwargs)[source]

Attention

Logs functionality is not implemented for the Smarter API Account.

Parameters:
  • request ("HttpRequest") – The HTTP request object.

  • args – Additional positional arguments.

  • kwargs – Additional keyword arguments.

Returns:

A JSON response indicating that logs is not implemented.

Return type:

SmarterJournaledJsonResponse

property manifest: SAMAccount | None

Get the manifest for the Smarter API Account as a Pydantic model.

Returns:

A SAMAccount Pydantic model instance representing the Smarter API Account manifest, or None if not initialized.

Note

The top-level manifest model (SAMAccount) must be explicitly initialized with manifest data, typically using **data from the manifest loader.

Tip

Child models within the manifest are automatically cascade-initialized by Pydantic, passing **data to each child’s constructor.

Warning

If the manifest loader or manifest metadata is missing, or if the account is not set, the manifest will not be initialized and None may be returned or an exception raised.

Example usage:

# Access the manifest property
manifest = broker.manifest
if manifest:
    print(manifest.apiVersion, manifest.kind)
manifest_to_django_orm()[source]

Transform the Smarter API Account manifest into a Django ORM model.

Return type:

dict

property name: str | None

Get the name of the Smarter API Account.

Returns:

The name of the Smarter API Account, or None if not set.

Return type:

Optional[str]

property orm_instance: Account | None

Return the Django ORM model instance for the broker.

Returns:

The Django ORM model instance for the broker.

Return type:

Optional[TimestampedModel]

orm_meta_instance_setter()[source]

Override of parent method to initialize the Django ORM meta model instance for the broker.

Return type:

None

property ready: bool

Check if the broker is ready for operations.

This property determines whether the broker has been properly initialized and is ready to perform its functions. A broker is considered ready if it has a valid manifest loaded, either from raw data, a loader, or existing Django ORM models.

Returns:

True if the broker is ready, False otherwise.

Return type:

bool

undeploy(request, *args, **kwargs)[source]

Attention

Undeploy functionality is not implemented for the Smarter API Account.

Parameters:
  • request ("HttpRequest") – The HTTP request object.

  • args – Additional positional arguments.

  • kwargs – Additional keyword arguments.

Raises:

SAMBrokerErrorNotImplemented – Always raised to indicate that undeploy is not implemented.

Returns:

A JSON response indicating that undeploy is not implemented.

Return type:

SmarterJournaledJsonResponse

exception smarter.apps.account.manifest.brokers.account.SAMAccountBrokerError(message=None, thing=None, command=None, stack_trace=None)[source]

Bases: SAMBrokerError

Base exception for Smarter API Account Broker handling.

property get_formatted_err_message
smarter.apps.account.manifest.brokers.account.should_log(level)[source]

Check if logging should be done based on the waffle switch.