Smarter Account Mixin

class smarter.apps.account.mixins.AccountMixin(*args, user=None, account=None, user_profile=None, account_number=None, api_token=None, **kwargs)[source]

Bases: SmarterHelperMixin

Provides consistent initialization and short-lived caching of the account, user, and user_profile properties using various sources, such as direct arguments, request objects, or API tokens. Also handles API token authentication when a request object with an Authorization header is provided.

Initialization priority:

  1. API token authentication if provided.

  2. Explicit account_number, account, or user arguments.

  3. Request object (from kwargs or positional args), extracting user and account info.

  4. Lazy loading from existing user or user_profile.

  5. User and Account parameters passed directly to the constructor.

Parameters:
  • args – Positional arguments, may include a request object.

  • account_number (Optional[str]) – Unique account identifier (optional).

  • account (Optional[Account]) – Account instance (optional).

  • user (Union[AnonymousUser, User, None]) – Django user instance (optional).

  • api_token (Optional[bytes]) – API token for authentication (optional).

  • kwargs – Additional keyword arguments, may include request.

The constructor attempts to resolve and cache the account and user information, logging relevant events and warnings if data cannot be resolved.

__init__(*args, user=None, account=None, user_profile=None, account_number=None, api_token=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

authenticate(api_token)[source]

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

property formatted_class_name: str

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

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()[source]

Logs the ready status of the AccountMixin.

property ready: bool

Returns True if the account and user are set.

property ready_state: str

Returns a string representation of the ready state.

to_json()[source]

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

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