Account
Smarter API Account Manifest handler.
- class smarter.apps.account.manifest.brokers.account.AccountSerializer(*args, **kwargs)[source]
Bases:
ModelSerializerAccount serializer for smarter api.
- class smarter.apps.account.manifest.brokers.account.SAMAccountBroker(*args, **kwargs)[source]
Bases:
AbstractBroker[Account,Account]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
**datafrom the client’s manifest data.Warning
If the manifest data or its metadata is missing, or if the account is not set, the manifest will not be initialized and may return
Noneor raise an exception.Example usage:
broker = SAMAccountBroker() manifest = broker.manifest if manifest: print(manifest.apiVersion, manifest.kind)
See also
SAMAccountAccountSAMAccountMetadataSAMAccountSpec
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 broker from a manifest, a manifest file, or the Django ORM.
A manifest takes precedence over a file path. With neither, the broker is initialized from the ORM instance named
nameand owned by the user profile, if there is one.- Parameters:
request – The request, or None. It may also be passed positionally in
args.args – Positional arguments, among which the request, user, account, user profile and manifest are found by their types.
name – The resource’s name.
kind – The resource’s SAM kind, e.g.
Guardrail.api_version – The manifest’s api version.
manifest – The manifest, as a Pydantic model or as a dict, e.g. from
parse_manifest().file_path – The path of a manifest file, read when no manifest is passed.
url – Accepted, for compatibility, but not used.
kwargs –
user,accountanduser_profile, and the rest, which are passed on toSmarterRequestMixin.
- Raises:
SmarterValueError – If the kind, name or manifest is invalid.
SAMBrokerError – If the manifest’s kind is not the broker’s kind.
- account_by_number(account_number, field_name)[source]
The account that
parentorshareScopenames.- Parameters:
- Return type:
- Returns:
The account, or None if the manifest omits the field.
- Raises:
SAMAccountBrokerError – if no account has that number.
- 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:
- Raises:
SAMBrokerErrorNotReady – If the broker’s account is not set.
SAMBrokerError – If an error occurs during the apply process.
Caution
Fields that are not editable (such as
id,created_at,updated_at, andaccount_number) are removed from the data before saving.Example usage:
response = broker.apply(request) print(response.status_code, response.data)
- apply_account_number(brokered_account, command)[source]
Give a new account the number that the manifest’s
metadata.accountNumbernames, if it names one.An organization’s manifests refer to its accounts by number, e.g. a User manifest’s
spec.accountsor a repository’sowners.yaml, so its accounts must have the same numbers on every platform that applies them. Only a superuser applies an Account manifest. An account’s number never changes, so a manifest whose number differs from an existing account’s is refused, as is a number that another account already has.- Parameters:
brokered_account (
Account) – The account, saved or new.command (
SmarterJournalCliCommands) – The command, for the error.
- Raises:
SAMAccountBrokerError – if the number belongs to another account, or differs from the account’s own.
- Return type:
- 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.
- cache_invalidations()[source]
Handle broker specific cache invalidation logic.
Invalidates the cache for the Account and UserProfile models.
- Return type:
- 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:
- dependencies()[source]
Return brokers for the Users that belong to this Account.
- Returns:
A User broker for each of the Account’s users.
- Return type:
- classmethod dependents(instance)[source]
Return the users that belong to an Account, as their UserProfiles.
A User has no
namefield, sodependencies()builds the User brokers itself, from these UserProfiles, with each user’s username. Don’t pass these rows todependency_brokers().
- 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:
- 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:
- 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:
- Raises:
SAMBrokerErrorNotReady – If the broker’s account is not set.
SAMAccountBrokerError – If the account data is invalid or cannot be converted.
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
SAMAccountAccount
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:
See also
SAMAccountSAMAccountMetadataSAMAccountSpecSAMKeysSAMMetadataKeysSAMAccountSpecKeys
- 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:
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:
- Raises:
SAMBrokerErrorNotReady – If the broker’s account is not set.
SAMAccountBrokerError – If there is an error retrieving or serializing the account data.
- property kind: str
Get the manifest kind for the Smarter API Account.
- Returns:
The manifest kind string for the Smarter API Account.
- Return type:
- 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:
- 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
**datafrom the client’s manifest data.Tip
Child models within the manifest are automatically cascade-initialized by Pydantic, passing
**datato each child’s constructor.Warning
If the manifest data or its 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.
- 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:
- prompt(request, *args, **kwargs)[source]
Prompt 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 prompt is not implemented.
- Returns:
A JSON response indicating that prompt is not implemented.
- Return type:
The account number of
parentorshare_scope, for a manifest.
- 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:
- exception smarter.apps.account.manifest.brokers.account.SAMAccountBrokerError(message=None, thing=None, command=None, stack_trace=None)[source]
Bases:
SAMBrokerErrorBase exception for Smarter API Account Broker handling.
- property get_formatted_err_message
The label of this error, used in its string form.