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]

Note: this needs to exist.

something in the Python MRO requires it, even if it does nothing. If you remove this, you will get a mysterious error about something downstream expecting exactly one object.

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_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 am_ready: bool

Returns True if the AccountMixin is am_ready to be used.

This is a convenience property that checks if the account and user are initialized. AccountMixin is considered am_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 am_ready to be used.

Return type:

bool

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

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

An optional method that can be called after initialization to perform any additional setup.

This is separate from __init__ to allow for more flexible initialization patterns.

Parameters:
  • args – Positional arguments passed to the init method.

  • kwargs – Keyword arguments passed to the init method.

Return type:

None

Returns:

None

property is_authenticated: bool

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

log_ready_status()[source]

Logs the ready status of the view.

property ready_state: str

Returns a string representation of the am_ready state.

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

This method is called by Django views during initialization.

It attempts to resolve the account and user information from the request object if it hasn’t already been set.

Parameters:
  • args – Positional arguments passed to the view.

  • kwargs – Keyword arguments passed to the view, may include ‘request’.

Returns:

The result of the superclass setup method.

Return type:

None

to_json()[source]

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

property user: AnonymousUser | User | None

Returns the user.

Handle lazy instantiation from user_profile or account.

Returns:

The 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