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:
SmarterHelperMixinProvides consistent initialization and short-lived caching of the
account,user, anduser_profileproperties 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:
API token authentication if provided.
Explicit
account_number,account, oruserarguments.Request object (from
kwargsor positional args), extracting user and account info.Lazy loading from existing
useroruser_profile.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).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_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:
- 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>
- 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:
- property is_authenticated: bool
Returns True if the user is authenticated and is associated with an Account.
- 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