Utils
Account Utilities.
This module provides foundational utilities for accessing, managing, and caching account and user data in the Smarter platform. It is the base model for all Django ORM operations in the project, and is designed for both performance and reliability.
Caching Overview
Two caching strategies are used:
LRU In-Memory Caching: Fast, per-process caching for frequently accessed objects such as User, Account, and UserProfile. Scope: Only available within the current process; short-lived.
Redis-Based ORM Caching: Persistent, cross-process caching for Django ORM objects. Scope: Shared across all processes; cache lifetime is controlled by expiration settings.
- smarter.apps.account.utils.ACCOUNT_ADMIN_USERNAME_SUFFIX = '-admin'
The username of the admin user that
get_cached_admin_user_for_account()creates is its account number plus this.
- smarter.apps.account.utils.account_admin_username(account_number)[source]
The username of the admin user that
get_cached_admin_user_for_account()creates for an account.
- smarter.apps.account.utils.account_number_from_url(url=None, invalidate=False)[source]
Extract the account number from a Smarter platform URL, using caching for performance.
- Parameters:
- Return type:
- Returns:
The extracted account number as a string, or None if not found.
Note
The function validates the URL format before extraction.
Warning
If the URL does not contain a valid account number, None is returned.
Tip
Use
invalidate=Trueafter updating URLs or account number patterns to ensure cache consistency.Example usage:
# Extract account number from a URL account_number = account_number_from_url(url="https://hr.3141-5926-5359.alpha.api.example.com/") # Result: '3141-5926-5359' # Invalidate cache before fetching account_number = account_number_from_url(url="https://hr.3141-5926-5359.alpha.api.example.com/", invalidate=True)
- smarter.apps.account.utils.delete_created_admin_user(account_number)[source]
Delete the admin user that
get_cached_admin_user_for_account()created for an account, once it has no profile.That user exists only for its account, and is a superuser. When the account is deleted, its profile goes with it, but the user would stay: a superuser that belongs to nothing.
- smarter.apps.account.utils.get_cached_account_for_user(invalidate=False, user=None)[source]
Locate the Account associated with a given user, using caching for performance.
The account is that of the UserProfile that the user chose in the web console’s user menu, for this request (see
UserProfile.get_selected_object()), else the default account, if the user belongs to it, else the account of the user’s first profile.- Parameters:
user (
User|AnonymousUser|None) – User instance. The user whose account should be located. Any other user, such as an AnonymousUser, has no account.invalidate (
bool|None) – Boolean, optional. If True, invalidates the cache before fetching.
- Return type:
- Returns:
Account instance if found, otherwise None.
Warning
If no account is found for the user, None is returned and a warning is logged.
Tip
Use
invalidate=Trueafter updating user or account data to ensure cache consistency.Example usage:
# Locate account for a user account = get_cached_account_for_user(user) # Invalidate cache before fetching account = get_cached_account_for_user(user, invalidate=True)
- smarter.apps.account.utils.get_cached_admin_user_for_account(account, invalidate=False)[source]
Retrieve the admin user for a given account, creating one if necessary.
- Parameters:
- Return type:
- Returns:
User instance representing the account admin.
Important
If no admin user exists for the account, a new staff user and UserProfile will be created automatically.
Warning
If the account is missing or misconfigured, an exception is raised.
Tip
Use
invalidate=Trueafter updating admin user data to ensure cache consistency.Example usage:
# Retrieve the admin user for an account admin_user = get_cached_admin_user_for_account(account=account) # Invalidate cache before fetching admin_user = get_cached_admin_user_for_account(invalidate=True, account=account)
- smarter.apps.account.utils.get_cached_default_account(invalidate=False)[source]
Retrieve the default account instance, using caching for performance.
- Parameters:
invalidate (
bool) – Boolean, optional. If True, invalidates the cache before fetching.- Return type:
- Returns:
The default Account instance.
Important
The default account is determined by the
is_default_account=Trueflag in the database.Warning
If no default account exists, an exception may be raised.
Example usage:
# Get the default account default_account = get_cached_default_account() # Invalidate cache before fetching default_account = get_cached_default_account(invalidate=True)
- smarter.apps.account.utils.get_cached_smarter_admin_user_profile()[source]
Retrieve the admin UserProfile for the smarter account, using caching for performance.
- Return type:
- Returns:
UserProfile instance for the smarter admin user.
Note
The smarter admin user is typically a superuser or staff user associated with the platform’s main account.
Example usage:
# Retrieve the smarter admin user profile admin_profile = get_cached_smarter_admin_user_profile() # Invalidate cache before fetching admin_profile = get_cached_smarter_admin_user_profile(invalidate=True)
- smarter.apps.account.utils.get_cached_user_for_user_id(invalidate=False, user_id=None)[source]
Retrieve a User instance by its primary key, using caching for performance.
- Parameters:
- Return type:
- Returns:
User instance if found, otherwise None.
Warning
If no user exists for the given ID, None is returned and an error is logged.
Tip
Use
invalidate=Trueafter updating user data to ensure cache consistency.Example usage:
# Retrieve user by ID user = get_cached_user_for_user_id(user_id=123) # Invalidate cache before fetching user = get_cached_user_for_user_id(user_id=123, invalidate=True)
- smarter.apps.account.utils.get_cached_user_for_username(invalidate=False, username=None)[source]
Retrieve a User instance by its username, using caching for performance.
- Parameters:
- Return type:
- Returns:
User instance if found, otherwise None.
Warning
If no user exists for the given username, a User.DoesNotExist exception is raised and an error is logged.
Tip
Use
invalidate=Trueafter updating user data to ensure cache consistency.Example usage:
# Retrieve user by username user = get_cached_user_for_username("johndoe") # Invalidate cache before fetching user = get_cached_user_for_username("johndoe", invalidate=True)
- smarter.apps.account.utils.get_user_profiles_for_account(account)[source]
Retrieve a list of user profiles associated with a given account.
- Parameters:
account (
Account) – Account instance. The account for which to retrieve user profiles.- Return type:
- Returns:
List of UserProfile instances, or None if no profiles exist.
Important
The account parameter is required. If not provided, an exception is raised.
Warning
If the account has no associated user profiles, None is returned.
Example usage:
# Get all user profiles for an account profiles = get_user_profiles_for_account(account)
- smarter.apps.account.utils.get_users_for_account(account)[source]
Retrieve a list of users associated with a given account.
- Parameters:
account (
Account) – Account instance. The account for which to retrieve users.- Return type:
- Returns:
List of User instances.
Important
The account parameter is required. If not provided, an exception is raised.
Warning
If the account has no associated users, an empty list is returned.
Example usage:
# Get all users for an account users = get_users_for_account(account)
- smarter.apps.account.utils.is_platform_staff(user)[source]
Return whether a user is one of the platform’s own staff: an active staff user of the Smarter account.
User.is_staffalone means that a user administers each account they belong to, so many customer accounts’ admins have it, and some of them are superusers too. (An account admin may instead haveUserProfile.is_account_admin, which administers one account and is never platform staff.) The platform’s staff are the staff users of the Smarter account (SMARTER_ACCOUNT_NUMBER), which runs the platform. Users who sign up themselves also join the Smarter account, but not as staff.This is a security check, so it is never cached: a user who stops being staff, or leaves the Smarter account, loses access at once.
Side effects: one database query, for the user’s profile in the Smarter account.
- smarter.apps.account.utils.set_account_admin(user_profile, is_account_admin=True)[source]
Make the profile’s user the administrator of its account, and only of that account.
It grants the role,
UserProfile.is_account_admin, and never takes it away:is_account_admin=Falseleaves the profile as it is. An account’s administrator is never made a superuser, who may read every account.- Parameters:
user_profile (
UserProfile) – The user’s profile in the account.is_account_admin (
bool) – Whether to make them its administrator.
- Return type:
- smarter.apps.account.utils.valid_resource_owners_for_user(user_profile)[source]
Get a list of valid owners for the given user profile.
This function retrieves all user profiles associated with the same account as the provided user profile. These profiles are considered valid owners for ORM resources created by the user.
- Parameters:
user_profile (
UserProfile|None) – The UserProfile instance representing the user.- Returns:
A list of UserProfile instances that are valid ORM resource owners.
- Return type:
See also
UserProfile
Example usage:
from smarter.apps.account.models import UserProfile from smarter.apps.account.utils import valid_resource_owners_for_user user_profile = UserProfile.objects.get(user__username="exampleuser") owners = valid_resource_owners_for_user(user_profile) print("Valid resource owners:", [owner.user.username for owner in owners])