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]
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:
- 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>
- 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:
- Returns:
None
- property is_authenticated: bool
Returns True if the user is authenticated and is associated with an Account.
- 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:
- 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